user828948
user828948

Reputation: 151

How to set the button image for UIControlStateSelected in android?

Hi i have button default it should be unselected,when i click it should be selected if i go next activity and if i come back it should be selected,if i was not selected before going next activity it should be unselected.how can i do this final Button up8 = (Button) findViewById(R.id.adultup8);
up8.setOnClickListener(new View.OnClickListener() {public void onClick(View view) { if(teeth[7]==0){up8.setBackgroundResource(R.drawable.adultup8); teeth[7]=8;}else{up8.setBackgroundResource(R.drawable.adultup8_pressed);teeth[7]=0;}}});

Upvotes: 1

Views: 142

Answers (2)

Ayush Verma
Ayush Verma

Reputation: 264

Your Initialization should be in onCreate() or at class level. Performing condition check should be at onResume(). If this button has to be of application level performing this condition, then you should use shared preferences to hold boolean value, so that, the state of button can be saved at application level.

protected void onResume() {
// TODO Auto-generated method stub
super.onResume();

up8.setOnClickListener(new View.OnClickListener() {
    public void onClick(View view) {
        if(teeth[7]==0){
            up8.setBackgroundResource(R.drawable.adultup8);
            teeth[7]=8;
        }
        else{
            up8.setBackgroundResource(R.drawable.adultup8_pressed);
            teeth[7]=0;
        }
    }
});

}

Upvotes: 0

Ayush Verma
Ayush Verma

Reputation: 264

put your code into onReume(). It will help you out definitely.

Upvotes: 0

Related Questions