I have tried to change the onClick image of a button using this code in the listener
public void onClick(View v) {
if(v==ButtonName)
ButtonName.setImageResource(R.drawable.clicked_button_image);
//action code
}
The image of the button in this way changes correctly, however if someone uses the Back button of Android device, the button appears with clicked image (because clearly is showed the last instance of the previous activity)
if I try to use the code:
public void onClick(View v) {
if(v==ButtonName)
ButtonName.setImageResource(R.drawable.clicked_button_image);
ButtonName.setImageResource(R.drawable.unclicked_button_image);
//action code
}
The images of the button doesn’t changes.
How could I solve this problem? (without edit the xml).
What about changing back when you click on the back button?
If you look at the documentation, and the activity lifecycle, you can see that the method
on Restoreis called when you are coming back to an activity.Maybe by having a private variable that you change when you change the background of your button, and by checking this boolean value for example, you can change the background of your button to give it his initial state, programmatically.