How to create custom shape button with selector in android

Please follow and like us:

Android [Button/TextView] Selector With Rounded Corners and Border

Android Default Button Effect is good but not great, so we need something attractive or customization in our android button click effect, for this purpose we can create an Android custom selector and put it into background of Button or TextView so we can achieve our custom effect while press/unpress button. Using this custom selector we can add our custom colors for selected/unselected state, add rounded corners with border effect.

Create custom_selector.xml in drawable folder for android button selector

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
   <item android:drawable="@drawable/unselected" android:state_pressed="true" />
   <item android:drawable="@drawable/selected" />
</selector>

Create selected.xml shape in drawable folder for android button selector

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" android:padding="90dp">
   <solid android:color="@color/selected"/>
   <padding />
   <stroke android:color="#000" android:width="1dp"/>
   <corners android:bottomRightRadius="15dp" android:bottomLeftRadius="15dp" android:topLeftRadius="15dp" android:topRightRadius="15dp"/>
</shape>

Create unselected.xml shape in drawable folder for android button selector

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" android:padding="90dp">
   <solid android:color="@color/unselected"/>
   <padding />
   <stroke android:color="#000" android:width="1dp"/>
   <corners android:bottomRightRadius="15dp" android:bottomLeftRadius="15dp" android:topLeftRadius="15dp" android:topRightRadius="15dp"/>
</shape>

Add following colors for selected/unselected state in color.xml of values folder for android button selector

<color name="selected">#a8cf45</color>
<color name="unselected">#ff8cae3b</color>

Now, you can put custom_selector.xml as background for Button or TextView(you must add “clickable=true” for enable click effect in TextView)

Hope you liked our “Android Button Selector Tutorial“. If you face any problem while implementing Button Selector Example then contact me via comment here,

Stay connected for latest android development tutorials for beginners.

nirav kalola