Monday, 16 May 2011

Android Button Design and selector

For first The xml file for may main layout in android activity is following

main.xml
----------------------------------------------------------------------------------

<?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="fill_parent"
android:layout_height="wrap_content" android:text="@string/hello" />
<LinearLayout android:orientation="horizontal"
android:layout_width="wrap_content" android:layout_height="wrap_content">
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Done" android:id="@+id/donebtn"
android:padding="5dip" android:background="@drawable/button_view" />
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="back" android:id="@+id/backbtn"
android:padding="5dip" android:background="@drawable/button_view" />
</LinearLayout>
<LinearLayout android:orientation="horizontal"
android:layout_width="wrap_content" android:layout_height="wrap_content">
<Button android:text="button_selector" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:background="@drawable/selector" />
</LinearLayout>
</LinearLayout>
-------------------------------------------------------------------------------------------------------------

After declare main xml file we will make 


<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#453657" />
<stroke android:width="1dp" android:height="1dp"  android:color="#FFD40F"  />
<corners android:bottomLeftRadius="8dip"
android:topRightRadius="8dip" android:topLeftRadius="8dip"
android:bottomRightRadius="8dip" />
</shape>

this xml file in drawable folder of your project .
<solid/> this tag is for background color of shape
<stroke/> this tag is used for boarder of shape
<corners/> this tag is used for make the corner round at given value.

and now for changing on button clicked changes we define again new xml file like this below.

Selector.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/spinner_dropdown_background_down"
android:state_pressed="true" />
 
<item android:drawable="@drawable/spinner_dropdown_background_down" 
android:state_focused="true" />
<item android:drawable="@drawable/spinner_dropdown_background_up" />
</selector>




No comments:

Post a Comment