Home » Android » How to get background image name?

How to get background image name?

Posted by: admin November 1, 2017 Leave a comment


How can i find name of background image in my button?
I set drawable:

 ((TextView) findViewById(BUTTON_IDS[i])).setBackground(getResources().getDrawable(R.drawable.ic_launcher));

You could use the views tag. Set the tag with the resource id:


Then, you get it with getTag(), casting to int:

int background = (int) findViewById(BUTTON_IDS[i]).getTag();
// background will contain the value of R.drawable.ic_launcher

There’s no need of casting the view to TextView, as setTag() and getTag() are methods of the basic View object.

Also, you can use setBackgroundResource() with just the resource id:


Even for this there’s no need of casting to TextView, as setBackgroundResource() is a method of the basic View object.

//Initialize view 

ImageView img = (ImageView)findViewbyId(R.id.img);

//get the image name based on view object

String backgroundImageName = String.valueOf(img.getTag());