CheckingButton Background Resource Id android
android, android-button, java
Solution
If you are setting multiple drawable backgrounds for a button then you check it as follows
if(button.getBackground().getConstantState()==getResources().getDrawable(R.drawable.name1).getConstantState())
{
//Your code
}
else if(button.getBackground().getConstantState()==getResources().getDrawable(R.drawable.name2).getConstantState())
{
//Your code
}
else
{
//Your code
}
You can use `button.getBackground().getConstantState().equals(getResources().getDrawable(R.drawable.name1).getConstantState())` if above code doesn't work
Problem
I have a button ``` Button button = (Button)findViewById(R.id.button); button.setBakgroundResource(R.drawable.icon); ``` If I want to check which background resource button has, is it possible to do so? how. For example : ``` if (button.getResourceId()==R.drawable.icon) ``` do something with code... UPDATE: THE CONDITION IS FALSE I WANT IT TO BE TRUE THE IMAGES DO NOT MATCH ``` vi.findViewById(R.id.button1).setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { // TODO Auto-generated method stub v.findViewById(R.id.button1).setBackgroundResource(R.drawable.boutton_off); Drawable aImg = (Drawable)v.findViewById(R.id.button1).getBackground(); Drawable bImg = v.getResources().getDrawable(R.drawable.boutton_off); if(aImg==bImg){ System.out.println("On"); }else System.out.println("off"); //main.popMessage(position); } }); ```