d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Java Android Alternate Button
Add Reply New Topic New Poll
Member
Posts: 2,059
Joined: Aug 16 2013
Gold: 0.80
Apr 27 2015 05:22pm
so i have two buttons X and O

every time i click on button X, I want the image to be swapped with O. When I click on the same object again, it should change back to the original image, X...

I've tried several things and the following is what I have but it seems like onClick doesn't do anything in this case...I've done something with a onTouchListener and that works but I couldn't figure out how to revert back to the original.

sorry it looks a little messy, i was just messing with things and commented most of the code out.

Code
public void onClick(View view) {
//final Button xButton = (Button) findViewById(R.id.xButton);
//final Button oButton = (Button) findViewById(R.id.oButton);
if (view.getId() == R.id.xButton) {
view.setBackgroundResource(R.drawable.o_button);
//xButton.setBackgroundResource(R.drawable.o_button);
}
// if (view.getId() == R.id.xButton) {
//
// if (flag) {
// view.setBackgroundResource(R.drawable.o_button);
// oButton.setBackgroundResource(R.drawable.x_button);
// flag = false;
// } else {
// view.setBackgroundResource(R.drawable.x_button);
// oButton.setBackgroundResource(R.drawable.o_button);
// flag = true;
// }
// }


}


This post was edited by iGotThatFiyah on Apr 27 2015 05:23pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Apr 27 2015 08:31pm
Quote
I've tried several things and the following is what I have but it seems like onClick doesn't do anything in this case


what do you mean it doesnt do anything? log/debug your code. figure out what line is causing your problem. should be fairly straight forward
Member
Posts: 2,059
Joined: Aug 16 2013
Gold: 0.80
Apr 27 2015 08:54pm
nvm i've figured it out. i forgot to set the listener on the view. thanks.
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll