Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create custom shape button with selector in android?

Tags:

android

button

MOCK UP


Button

Requirement


I want to put custom button with selector.

Mock up is given above.

If anyone knows solution then share it.

Thank you.

like image 709
Darshak Avatar asked Nov 29 '22 02:11

Darshak


1 Answers

basically you will need to create some new XML files and apply them to your Button element. As i can see from the mockup you will need a stroke and the background color with some shading effect applied, you can research more into the shading thing but the background color and the stroke is pretty straight forward.

Here is an example, done_rounded_btn.xml:

   <?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item 
        android:state_pressed="true" 
        android:state_enabled="true" 
        android:drawable="@drawable/zzzzzzzzz_btn_orange" />
    <item 
        android:state_focused="true" 
        android:state_enabled="true"
        android:drawable="@drawable/zzzzzzzzz_btn_orange" />
    <item 
        android:state_focused="false" 
        android:state_enabled="false"
        android:drawable="@drawable/zzzzzzzzz_btn_inactiv" />
    <item android:drawable="@drawable/zzzzzzzzz_btn_black"/>
</selector>

for the selection part and then you create the custom drawables corresponding to the mockup.

An example, zzzzzzzzzz_btn_orange:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <solid
        android:color="@color/done_color">
    </solid>

    <corners
        android:bottomLeftRadius="3dp"
        android:bottomRightRadius="3dp"
        android:topLeftRadius="3dp"
        android:topRightRadius="3dp" />

</shape>

And then add it to your button as background, main.xml:

<Button
            android:id="@+id/registers_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginBottom="10dp"
            android:layout_marginLeft="15dp"
            android:layout_marginTop="10dp"
            android:background="@drawable/done_rounded_btn"
            android:text="@string/done_txt"
            android:textColor="@color/white"
            android:textSize="15sp" />

Hope this helps!

like image 167
Adrian Olar Avatar answered Dec 17 '22 20:12

Adrian Olar