Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Image Resource

How should I get current image resource of an imageButton? I need something like this:

imageButton.getImageResource(); 

Thanks a lot

like image 837
MOHRE Avatar asked Oct 14 '14 21:10

MOHRE


People also ask

What is an image resource?

A string representing the accessible name of the image. In an API, an image resource is represented as an ImageResource dictionary. In [ JSON ], image resource is represented as an object.

How to get image resource in Android programmatically?

You can use imageButton. getDrawable() to get the drawable of the ImageButton object and then use setImageDrawable() instead of setImageResource() .

How to add image resource in Android studio?

To import image resources into your project, do the following: Drag and drop your images directly onto the Resource Manager window in Android Studio. Alternatively, you can click the plus icon (+), choose Import Drawables, as shown in figure 3, and then select the files and folders that you want to import.

How to put image in Drawable XML file?

Step 1: In this method first of all in your system find your required images and copy the image as we do normally. Step 2: Then open the Android Studio go to the app > res > drawable > right-click > Paste as shown in the below figure. Step 3: Then a pop-up screen will arise like below.


2 Answers

I think you can't, current API doesn't allow this. But if you really need this, you can do something like this:

imageButton.setImageResource(R.drawable.your_resource);
imageButton.setTag(R.drawable.your_resource);
//
// somewhere later...
//
Integer resource = (Integer)imageButton.getTag();
like image 104
dimsuz Avatar answered Oct 11 '22 00:10

dimsuz


You can use imageButton.getDrawable() to get the drawable of the ImageButton object and then use setImageDrawable() instead of setImageResource().

like image 23
Mokhefi Abdelkrim Avatar answered Oct 10 '22 22:10

Mokhefi Abdelkrim