Reputation: 539
I have this little code here:
MainActivity.java:
public void Images(View v)
{
ImageButton i = (ImageButton) v;
i.setImageResource(R.drawable.c4_pressed_button);
}
MainActivity.xml:
<ImageButton
android:id="@+id/b1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="@android:color/transparent"
android:contentDescription="@string/contentDescription"
android:src="@drawable/c4_button"
android:onClick="Images"/>
All I want to do is to compare two images ('drawables'?) from the ImageButton, just like that:
public void Images(View v)
{
ImageButton i = (ImageButton) v;
if(xxxx == R.drawable.c4_pressed_button)
i.setImageResource(R.drawable.c4_button);
else i.setImageResource(R.drawable.c4_pressed_button);
}
And I don't know what I have to put in xxxx to make that comparison. I know R.drawable.c4_pressed_button is an Int, but I don't know how to get the ImageButton image to compare it to that. Any help?
Upvotes: 1
Views: 2137
Reputation: 3914
Use setTag()
and getTag()
method
public void Images(View v)
{
ImageButton i = (ImageButton) v;
i.setImageResource(R.drawable.c4_pressed_button);
i.setTag("R.drawable.c4_pressed_button");
}
And then do comparison
public void Images(View v)
{
ImageButton i = (ImageButton) v;
String imageName = (String) i.getTag();
if(imageName.equals("R.drawable.c4_pressed_button"))
i.setImageResource(R.drawable.c4_button);
else i.setImageResource(R.drawable.c4_pressed_button);
}
Upvotes: 2