Reputation: 73
I'm using a ImageButton which should change his image after every click. This is my code which doesn't run:
public class MainActivity extends Activity {
ImageButton button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (ImageButton)findViewById(R.id.imageButton1);
button.setOnClickListener(imgButtonHandler);
}
View.OnClickListener imgButtonHandler = new View.OnClickListener() {
public void onClick(View v) {
if(button.getBackground().equals(R.drawable.lok))
{
button.setBackgroundResource(0);
button.setBackgroundResource(R.drawable.lok2);
}
}
};}
I know that this part should be wrong: if(button.getBackground().equals(R.drawable.lok))
Someone has a other solution for it?
Thanks in advance
Upvotes: 2
Views: 3050
Reputation: 6738
Let's say you have 5 images to change then use an int i
variable
public class MainActivity extends Activity {
ImageButton button;
int i=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (ImageButton)findViewById(R.id.imageButton1);
button.setOnClickListener(imgButtonHandler);
}
View.OnClickListener imgButtonHandler = new View.OnClickListener() {
public void onClick(View v) {
button.setBackgroundResource(0);
switch (i % 5) {
case 0:
button.setBackgroundResource(R.drawable.image0);
break;
case 1:
button.setBackgroundResource(R.drawable.image1);
break;
case 2:
button.setBackgroundResource(R.drawable.image2);
break;
case 3:
button.setBackgroundResource(R.drawable.image3);
break;
case 4:
button.setBackgroundResource(R.drawable.image4);
break;
default:
break;
}
i++;
}
};
}
Upvotes: 0
Reputation: 7082
try like that
Drawable fDraw = button.getBackground();
Drawable sDraw = getResources().getDrawable(R.drawable.lok);
if(fDraw.hashCode() == sDraw.hashCode())
{
//same
}
else
{
//not same
}
}
Upvotes: 0
Reputation: 65
If you want to change the image of button when you click the button. First, you should in res/value add some information about the image. then If we need to exchange 3 pictrues,only need to creat selector.xme int res/drawable
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_window_focused="false"
android:drawable="@drawable/t3"/>
<item android:state_focused="true"
android:drawable="@drawable/t1"/>
<item android:state_pressed="true"
android:drawable="@drawable/t2"/>
<item android:drawable="@drawable/t3"/>
</selector>
so sorry my English is not good and my way is only i tried ago
Upvotes: 0
Reputation: 7415
You can also check the Tag
of Imagebutton.
for example, setTag(X) whenever you set ImageButton's Background.
then you can compare its Tag & then change its background & tag.
Upvotes: 0
Reputation: 9507
You should use Tag
. Set Tag for a button using setTag()
and when click of button getTag()
. Here is an example for that.
Upvotes: 3
Reputation: 28484
*Try this *
public void onClick(View v) {
if(button.getBackground().equals(R.drawable.lok))
{
if(Integer.parseInt(v.getTag())== R.drawable.lok2){
}else{
}
button.setBackgroundResource(0);
button.setBackgroundResource(R.drawable.lok2);
button.setTag(R.drawable.lok2);
}
}
Upvotes: 0