Home > Enterprise >  ImageButton - how to read the image ID?
ImageButton - how to read the image ID?

Time:07-02

Sorry for the banal question, but I'm new to Android. I have such a problem. I have several ImageButton elements and I need to change their images depending on what is on them during onClick() event. While setting a new image is not a problem, I can not programmatically read what image is currently (the next image depends on it).I search various sources and can't find a solution. It is certainly very simple.

I want to have a code like this:

if (ImageButton1.image... == R.drawable.img1)
{
   ImageButton1.setImageResource(R.drawable.img2)
   ImageButton2.setImageResource(R.drawable.img3)
   ...
}

Any help appreciated. Thanks...

CodePudding user response:

Welcome to Stackoverflow.

One approach to get over this is have an Int array and store the drawables in the array in the format

var listOfImages = listOf(R.drawable.one,R.drawable.two)

Now set the images as ImageButton1.setImageResource(listOfImages(0))

That way you know which drawablr is being used

  • Related