android - Selector for a button which changes its background and text color -
i having grouping of 3 button , want alter color , text of particular button when clicked , alter 1 time again normal when button clicked.
i using selector first time , not clear states either can 1 help me out this.
here doing.
these 3 buttons
<linearlayout android:id="@+id/lineartab" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:gravity="center|center_horizontal" android:orientation="horizontal" android:weightsum="3" > <button android:id="@+id/allmessagesbt" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="@drawable/message_top_btn_selector" android:singleline="false" android:text="all \nmessages" android:textcolor="#7b818b" android:textsize="14sp" /> <imageview android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_marginbottom="3dp" android:layout_margintop="3dp" android:background="@drawable/line" /> <button android:id="@+id/createnewbt" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="@drawable/message_top_btn_selector" android:singleline="false" android:text="create \nnew" android:textcolor="#7b818b" android:textsize="14sp" /> <imageview android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_marginbottom="3dp" android:layout_margintop="3dp" android:background="@drawable/line" /> <button android:id="@+id/deletebt" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="@drawable/message_top_btn_selector" android:singleline="false" android:text="delete \nselected" android:textcolor="#7b818b" android:textsize="14sp" /> </linearlayout> and here selector xml
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/login_edittext" android:state_selected="true"></item> <item android:drawable="@drawable/btn_accept_normal" android:state_pressed="true</item> <item android:drawable="@drawable/btn_accept_hover"></item> </selector> i want alter background color 1 time button clicked , maintain way until button clicked
try devil abhi suggested , add together code required alter color , text of particular button when clicked , alter 1 time again normal when button clicked.
@override public void onclick(view v) { // todo auto-generated method stub switch(v.getid()){ case r.id.button1: b1.setbackgroundcolor(color.red); b1.settextcolor(color.white); b1.settypeface(null, typeface.bold); b2.setbackgroundcolor(*default background color*); b3.setbackgroundcolor(*default background color*); break; case r.id.button2: b2.setbackgroundcolor(color.green); b2.settextcolor(color.white); b2.settypeface(null, typeface.bold); b1.setbackgroundcolor(*default background color*); b3.setbackgroundcolor(*default background color*); break; case r.id.button3: b3.setbackgroundcolor(color.blue); b3.settextcolor(color.white); b3.settypeface(null, typeface.bold); b1.setbackgroundcolor(*default background color*); b2.setbackgroundcolor(*default background color*); break; } android button selector onclicklistener
No comments:
Post a Comment