adding a click effect to my button in android
24,082
here is the full XML code for button selector
button_normal.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@color/button_light_green"/>
<corners android:radius="5dp" />
</shape>
button_selected.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@color/button_light_green_selected"/>
<corners android:radius="5dp" />
</shape>
button_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true" android:drawable="@drawable/button_selected"/>
<item android:state_focused="true" android:drawable="@drawable/button_selected"/>
<item android:drawable="@drawable/button_normal"/>
</selector>
And Finally Add it into Your button background...
<Button
android:layout_marginTop="20dp"
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="150sp"
android:text="add"
android:gravity="center"
android:layout_centerHorizontal="true"
android:layout_below="@+id/spin"
android:background="@drawable/button_bg"/> <!-- Add Selector File here to have click effect -->
Author by
Farhan patel
Updated on July 13, 2020Comments
-
Farhan patel almost 4 years
i have made a custom button for my layout but the problem is it doesnt show me a onclick effect. here are my three xml files that ive build i have given button an oval shape can you also tell me how to make the button a circle and smaller in size?? thnx in advance
<TextView android:id="@+id/textView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="0" android:gravity="center" android:textSize="25sp" /> <Spinner android:id="@+id/spin" android:gravity="center" android:layout_width="match_parent" android:layout_height="wrap_content" android:spinnerMode="dropdown" android:text="heyyy" android:layout_below="@+id/textView1" /> <Button android:layout_marginTop="20dp" android:id="@+id/button1" android:layout_width="match_parent" android:layout_height="150sp" android:text="add" android:gravity="center" android:layout_centerHorizontal="true" android:layout_below="@+id/spin" android:background="@drawable/button_xml" /> <TextView android:id="@+id/textView2" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignLeft="@+id/textView3" android:layout_below="@+id/button1" android:gravity="left" android:textSize="25sp" android:text="add" /> <TextView android:id="@+id/textView3" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/button1" android:gravity="right" android:textSize="25sp" android:text="a" /> <Button android:layout_marginTop="20dp" android:id="@+id/button2" android:layout_width="match_parent" android:layout_height="50sp" android:text="reset" android:gravity="center" android:layout_centerHorizontal="true" android:layout_below="@+id/textView3"/> </RelativeLayout> </ScrollView>
button_xml.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval" > <solid android:color="#03A89E"/> <stroke android:width="2dp" android:color="#ffffff"/> </shape>
button_click.xml
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android" > <item android:state_pressed="true" android:drawable="@drawable/button_xml" /> </selector>