Showing posts with label custom control. Show all posts
Showing posts with label custom control. Show all posts

Wednesday, April 18, 2012

Custom Toast

custom_toast_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/root"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@android:drawable/picture_frame"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_marginLeft="5dp"
        android:src="@drawable/inbox" />

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="15dp"
        android:text="Toast text here"
        android:textColor="#000000"
        android:textAppearance="?android:attr/textAppearanceSmall" />

</LinearLayout>


main.xml


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#454545"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Click here to see custom Toast" />

</RelativeLayout>

In java File


Button mButton1 = (Button) findViewById(R.id.button1);
  mButton1.setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    LayoutInflater inflater = getLayoutInflater();
    View layout = inflater.inflate(R.layout.custom_toast_layout,
      (ViewGroup) findViewById(R.id.root));

    TextView text = (TextView) layout.findViewById(R.id.text);
    text.setText("Message Sent Succesfully !!");

    Toast toast = new Toast(getApplicationContext());
    toast.setGravity(Gravity.BOTTOM, 10, 80);
    toast.setDuration(Toast.LENGTH_LONG);
    toast.setView(layout);
    toast.show();
   }
  });

Multiple Toast 



Toast m_currentToast;

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  Button mButton = (Button) findViewById(R.id.button);
  mButton.setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    showToast("5");
    showToast("4");
    showToast("3");
    showToast("2");
    showToast("1");
    showToast("Go go go...");

   }
  });

 }

 void showToast(String text) {
  if (m_currentToast != null) {
   m_currentToast.cancel();
  }
  LayoutInflater inflater = getLayoutInflater();
  View layout = inflater.inflate(R.layout.custom_toast_layout,
    (ViewGroup) findViewById(R.id.root));
  TextView text1 = (TextView) layout.findViewById(R.id.text);
  text1.setText(text);

  m_currentToast = new Toast(getApplicationContext());
  m_currentToast.setDuration(Toast.LENGTH_SHORT);
  m_currentToast.setView(layout);
  m_currentToast.setGravity(Gravity.TOP, 10, 180);
  m_currentToast.show();

 }

Saturday, April 7, 2012

Standard Android Button with a different color




Add this file into drawable folder in res folder 

red_button.xml

<?xml version="1.0" encoding="utf-8"?>
<selector
    xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_pressed="true" >
        <shape>
            <gradient
                android:startColor="#FFFFFF"
                android:endColor="#FFEEEE"
                android:angle="270" />
            <stroke
                android:width="2dp"
                android:color="#454545" />
            <corners
                android:radius="3dp" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
        </shape>
    </item>

    <item android:state_focused="true" >
        <shape>
            <gradient
                android:endColor="#FF0000"
                android:startColor="#AA0000"
                android:angle="270" />
            <stroke
                android:width="2dp"
                android:color="#FFFFFF" />
            <corners
                android:radius="3dp" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
        </shape>
    </item>

    <item>        
        <shape>
            <gradient
                android:endColor="#FF0000"
                android:startColor="#AA0000"
                android:angle="270" />
            <stroke
                android:width="2dp"
                android:color="#454545" />
            <corners
                android:radius="3dp" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
        </shape>
    </item>
</selector>

And set background to button like

<Button android:text="Click Me !" android:id="@+id/button1"
  android:background="@drawable/red_button" android:layout_margin="10dp"
  android:layout_width="fill_parent" android:layout_height="wrap_content"></Button>


That's it !!

Friday, March 30, 2012

Custom radio button and checkbox in android

Anybody know that real cool Android application must have not only excelent business logic. The one of the most important things what application must have is a nice UI.
If you want to create realy interesting application you need to create good UI. And …. hire a designer
Below i want to tell you how to create your own CheckBoxes and RadioButtons for your Android Applications. Sorry for this sample UI design. I didn’t hire a designer :).
First look at video of that:


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical" android:layout_width="fill_parent"
 android:layout_height="fill_parent">
 <TextView 
  android:layout_width="wrap_content"
  android:textAppearance="?android:attr/textAppearanceLarge" 
  android:id="@+id/textView1"
  android:layout_height="wrap_content" 
  android:text="Custom Radio Button "/>
 <RadioGroup 
  android:id="@+id/radioGroup1"
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content">
  <RadioButton 
   android:text="Cool" 
   android:button="@drawable/my_ratingbar"
   android:layout_height="wrap_content" 
   android:id="@+id/radio0"
   android:layout_width="134dp"/>
  <RadioButton 
   android:text="Sexy" 
   android:button="@drawable/my_ratingbar"
   android:layout_height="wrap_content" 
   android:id="@+id/radio1"
   android:checked="true" 
   android:layout_width="wrap_content"/>
  <RadioButton 
   android:text="Both" 
   android:button="@drawable/my_ratingbar"
   android:layout_height="wrap_content" 
   android:id="@+id/radio2"
   android:layout_width="wrap_content"/>
 </RadioGroup>
 <TextView 
  android:layout_width="wrap_content"
  android:textAppearance="?android:attr/textAppearanceLarge"
  android:layout_marginTop="20dp" 
  android:id="@+id/textView1"
  android:layout_height="wrap_content" 
  android:text="Custom Check Box "/>
 <CheckBox 
  android:text="CheckBox1" 
  android:id="@+id/checkBox1"
  android:checked="true" 
  android:button="@drawable/my_ratingbar"
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content"/>
 <CheckBox 
  android:text="CheckBox2" 
  android:id="@+id/checkBox2"
  android:button="@drawable/my_ratingbar" 
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"/>
 <TextView 
  android:layout_width="wrap_content"
  android:textAppearance="?android:attr/textAppearanceLarge"
  android:layout_marginTop="20dp" 
  android:id="@+id/textView1"
  android:layout_height="wrap_content" 
  android:text="http://developer-dot-android.blogspot.com"/>
</LinearLayout>




my_ratingbar.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
 <item android:state_checked="true" android:drawable="@drawable/red_holo" />
 <item android:state_checked="false" android:drawable="@drawable/red_full" />
</selector>

red_holo


red_full


Friday, March 16, 2012

Custom Seekbar Tutorial


seekbar_progress_bg.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item>
        <clip >
            <bitmap
                xmlns:android="http://schemas.android.com/apk/res/android"
                android:antialias="true"
                android:dither="false"
                android:filter="false"
                android:gravity="left"
                android:src="@drawable/green">
             </bitmap>
        </clip>
    </item>
</layer-list>



seekbar_progress.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <item android:id="@android:id/background">
         <nine-patch
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:dither="true"
            android:src="@drawable/whitepatch">
          </nine-patch>
    </item>
    <item android:id="@android:id/secondaryProgress">
           <clip >
               <shape >
                  <gradient
                    android:angle="270"
                    android:centerColor="#80127fb1"
                    android:centerY="0.75"
                    android:endColor="#a004638f"
                    android:startColor="#80028ac8">
                   </gradient>
              </shape>
           </clip>
     </item>
     <item
             android:id="@android:id/progress"
             android:drawable="@drawable/seekbar_progress_bg">
     </item>
</layer-list>





XML

<SeekBar
                                android:id="@+id/seekbar_bar"
                                style="?android:attr/progressBarStyleHorizontal"
                                android:layout_width="420dip"
                                android:layout_height="wrap_content"
                                android:layout_marginLeft="0dip"
                                android:layout_marginTop="12dip"
                                android:maxHeight="9dp"
                                android:minHeight="9dp"
                                android:progressDrawable="@drawable/seekbar_progress"
                                android:thumb="@drawable/shine_btn" />

green.png

shine_btn.png
whitepatch.9.png