Create custom button in android

Download Code
 
Create custom button in android

Maybe you would like to create custom button.
We can use custom android buttons for a better layout.
Here I’m going to show how to customize Android buttons.

In the following image you can see how our buttons will look like.



1) Create button_disable_yellow.xml file in drawable folder.
 
    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android" >
        <corners android:radius="25dp" />
        <solid android:color="#ACAFAF" />
    </shape>

2) Create button_enable_yellow.xml file in drawable folder.
 
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item><layer-list>
                <item android:right="1dp" android:top="1dp"><shape>
                        <corners android:radius="25dp" />
                        <solid android:color="#FEBF00" />
                    </shape></item>
                <item android:bottom="1dp" android:left="1dp"><shape>
                        <gradient android:angle="270" android:endColor="#F8EBC3" android:startColor="#FEBF00" />
                        <stroke android:width="1dp" android:color="#FEBF00" />
                        <corners android:radius="25dp" />
                    </shape></item>
        </layer-list></item>
    </selector>

3) Create button_focus_yellow.xml file in drawable folder.
 
    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android" >
        <corners android:radius="25dp" />
        <solid android:color="#FEBF00" />
    </shape>

4) Create button_pressed_yellow.xml file in drawable folder.
 
    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android" >
        <corners android:radius="25dp" />
        <solid android:color="#80FEBF00" />
    </shape>

5) Create button.xml file in drawable folder.
 
    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:drawable="@drawable/button_disable_yellow" android:state_enabled="false"/>
        <item android:drawable="@drawable/button_pressed_yellow" android:state_enabled="true" android:state_pressed="true"/>
        <item android:drawable="@drawable/button_focus_yellow" android:state_enabled="true" android:state_focused="true"/>
        <item android:drawable="@drawable/button_enable_yellow" android:state_enabled="true"/>
    </selector>

Now set custom button in layout.
 
<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin" >

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dip"
            android:background="@drawable/button_yellow"
            android:padding="10dip"
            android:text="@string/Yellow"
            android:textColor="#000" />
       
</LinearLayout>

I hope this post will help you…

Thanks!


Download Code

 

Comments

Popular posts from this blog

Android - Set cursor drawable programmatically

Android - Shared Preferences Tutorial

Create custom toast message in android