Reputation: 141
I have an ImageButton and when I click on it I would like to change the image, but only if this is a special image (the default one in fact).
My xml :
<ImageButton
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/button1"
android:src="@drawable/imgdefault" />
And I have a listener on it :
public OnClickListener button1Listener = new OnClickListener() {
@Override
public void onClick(View v) {
if (button1.getDrawable().equals(getResources().getDrawable(R.drawable.imgdefault))) {
button1.setImageResource(R.drawable.newImg);
}
}
But it seems that my "if" is never true. So I can I check if this is still my default image on the ImageButton ?
Thank you for your help.
Upvotes: 1
Views: 6899
Reputation: 26978
There are many ways to do it, one that comes to my mind is:
Boolean clicked = new Boolean(false);
button1.setTag(clicked); // wasn't clicked
button1.setOnClickListener( new OnClickListener() {
@Override
public void onClick(View v) {
if( ((Boolean)button1.getTag())==false ){
button1.setImageResource(R.drawable.newImg);
button1.setTag(new Boolean(true));
}
}
});
Upvotes: 3