Home

Monday 18 May 2015

Creating Advance Custom Button

Creating Advance Custom Button :

For a better User Experience we need to show button responsive, for that we will change the button when the user presses it. To do that we need to create:

• button_bg_normal.xml  - For normal button.
• button_bg_pressed.xml - When the button is pressed.
• button_selector.xml - To switch between the backgrounds.


button_bg_normal.xml :

1
2
3
4
5
6
7
8
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="@android:color/holo_blue_dark" />
    <corners android:radius="25dp" />
    <stroke
        android:color="@android:color/holo_blue_bright"
        android:width="2dp" />
</shape>


button_bg_pressed.xml :

1
2
3
4
5
6
7
8
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="@android:color/holo_red_dark" />
    <corners android:radius="25dp" />
    <stroke
        android:color="@android:color/holo_red_light"
        android:width="2dp" />
</shape>


button_selector.xml :

1
2
3
4
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/button_bg_pressed" android:state_pressed="true" />
    <item android:drawable="@drawable/button_bg_normal" />
</selector>


activity_main.xml : 


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp"
    tools:context=".MainActivity">

    <Button
        android:gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This is a Custom Button"
        android:padding="10dp"
        android:textColor="@android:color/white"
        android:background="@drawable/button_selector"
        android:layout_centerInParent="true"/>

</RelativeLayout>

ScreenShots : 

 


No comments:

Post a Comment