java - Clicking an ImageButton to change to a different image everytime -
i have imagebutton
, false
appears gray image
, while true
appears green image
. if use state_pressed="true"
, state_pressed="false"
function work, need hold left-click button
button change true
.
p.s: believe using wrong state
have tried others didn't work.
my question is: how keep image
true
clicking once on imagebutton
?
code:
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/green_clock" android:state_selected= "true"> </item> <item android:drawable="@drawable/grey_clock" android:state_selected="false"> </item> </selector>
java file:
public void onclickclock(view v) { imagebutton clockbtn = (imagebutton)findviewbyid(r.id.clock); clockbtn.setimageresource(r.drawable.clock); }
activity.xml:
<imagebutton android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/clock" android:src="@drawable/clock" android:background="@android:color/transparent" android:layout_alignparenttop="true" android:layout_alignparentright="true" android:layout_alignparentend="true" android:layout_margintop="73dp" android:onclick="onclickclock" />
thanks!
add setselected()
public void onclickclock(view v) { imagebutton clockbtn = (imagebutton)findviewbyid(r.id.clock); clockbtn.setselected(!clockbtn.isselected()); }
Comments
Post a Comment