Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing the background resources of buttons

Tags:

android

button.setBackgroundResource(R.Drawable.abc);
if ( button.getBackground()==getResources().getDrawable(R.drawable.abc))
{
    button.setBackgroundResource(R.drawable.xyz);
}

else if( button.getBackground()==getResources().getDrawable(R.drawable.xyz) )
{
    button.setBackgroundResource(R.drawable.abc);
}

I want to compare the background images set on the button. The above code has been taken from Stack Overflow... but it doesn't seem to work

Please suggest a better method.

like image 713
Sabre.Tooth Avatar asked Apr 12 '13 16:04

Sabre.Tooth


1 Answers

Try this

if ( button.getBackground().getConstantState()==getResources().getDrawable(R.drawable.abc).getConstantState())
{
     button.setBackgroundResource(R.drawable.xyz);
}
like image 91
Pragnani Avatar answered Dec 03 '22 19:12

Pragnani