android - How to highlight a button when is pressed? -
i'm making andorid quiz , want highlight button when it's clicked when user lets go of button turns in original colour. see i've set background of button buttons can rounded. i've set in drawable.
<button android:id="@+id/btn1" android:background="@drawable/roundedbutton" android:textcolor="#ffffff" android:textstyle="italic" android:layout_width="225sp" android:layout_margintop="23sp" android:layout_height="38sp" android:layout_alignleft="@+id/btn2" android:layout_below="@+id/textview1" android:text="button" />
roundedbutton.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" android:padding="10dp"> <solid android:color="#848482"/> <!-- 1 ths color of rounded button --> <corners android:bottomrightradius="6.5dp" android:bottomleftradius="6.5dp" android:topleftradius="6.5dp" android:toprightradius="6.5dp"/> </shape>
you can use ontouchlistener
or can use selector.
button.setontouchlistener(new ontouchlistener() { @override public boolean ontouch(view v, motionevent event) { if (event.getaction() == motionevent.action_down) { // change color } else if (event.getaction() == motionevent.action_up) { // set normal color } return true; } });
you can use selector also. borders , rounded rectangle. customize same.
bkg.xml in drawable folder
<?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/pressed" /> <item android:state_focused="false" android:drawable="@drawable/normal" /> </selector>
normal.xml in drawable folder
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <solid android:color="#0aecbf"/> <stroke android:width="3dp" android:color="#0fecff" /> <padding android:left="5dp" android:top="5dp" android:right="5dp" android:bottom="5dp"/> <corners android:bottomrightradius="7dp" android:bottomleftradius="7dp" android:topleftradius="7dp" android:toprightradius="7dp"/> </shape>
pressed.xml in drawable folder
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" > <solid android:color="#ff33ffff" /> <padding android:left="5dp" android:top="5dp" android:right="5dp" android:bottom="5dp"/> <corners android:bottomrightradius="7dp" android:bottomleftradius="7dp" android:topleftradius="7dp" android:toprightradius="7dp"/> </shape>
now set background fro button in xml
android:background="@drawable/bkg"
Comments
Post a Comment