-1

I have a button that should call a method depending on the name or Id of its shown icon , getResources().getIdentifier did not help Thank you

Afeef
  • 71
  • 6
  • 1
    so if you have logic to set the different icons, keep track of the icon you set ? – a_local_nobody Mar 07 '21 at 19:48
  • The id is for the button, not for the image set on the button. So the id or name will be the same no matter what icon is set. You should look for an alternative solution, like a state, instead of relying on what icon is set on a button. – m0skit0 Mar 07 '21 at 20:11
  • The idea is that I want one button to do multiple jobs depending on the icon, I could use GetText but it won't work since the text of the button is translated to many languages . and of curse the state of the button will be the same regarding the icon – Afeef Mar 07 '21 at 20:20

1 Answers1

0

You can use an Image Button and test the drawable of this image button

Use the getDrawable() method with the ImageButton and test it using .getConstantState().equals()

Sample code:

ImageButton btn = (ImageButton) findViewById(R.id.myImageBtn);
Drawable drawable = btn.getDrawable();
if (drawable.getConstantState().equals(getResources().getDrawable(R.drawable.myDrawable).getConstantState())){
   //Do your work here
}

References: https://stackoverflow.com/a/24106975/14990708

Nina
  • 79
  • 2
  • 10
  • Thank you so much , my question was about normal buttons , not image buttons .anyway , I solved my problem using a global variable – Afeef Mar 10 '21 at 19:50