i am trying to create an app. When user clicks on the button, it gets pressed (image is loaded). When he clicks the button now, it gets unpressed (image is loaded) and so on. I am using the code below with which the problem is that when i click on the unpressed button 3rd time it doesn’t get into the pressed state again. So at first click it gets pressed, at second click it gets unpressed, and at the 3rd click nothing happens.
NameButton = (Button)findViewById(R.id.Button01);
NameButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
NameButton.setBackgroundResource(R.drawable.pressed);
NameButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
NameButton.setBackgroundResource(R.drawable.unpressed);
}
});
}
});
You might do it like that:
And have a boolean variable setPressed as a member variable.
Alternatively, consider using ToggleButton.