0

By doing

imagebutton1.getResources();

It returns a "Resource". How would you convert that Resource to an int? IE: R.id.xxx

I am aiming to retrieve the id of the image.

Piofmc
  • 345
  • 1
  • 4
  • 18
  • getResources() return a Resources object. Not a Resource. Check http://developer.android.com/reference/android/content/res/Resources.html – joao2fast4u Apr 27 '14 at 00:32
  • This sounds more like a situation that would involve Reflection. I'm not sure why you'd need this. The R.java file is created during the Build process. – mrres1 Apr 27 '14 at 00:43

5 Answers5

0

According to this post, your Drawable may not have an Id.

Are you sure you want to do this?

Community
  • 1
  • 1
joao2fast4u
  • 6,574
  • 5
  • 25
  • 41
0

In your code, find where you assign imagebutton1.

You are most likely using the Activity#findViewById() to assign the content.

Something similar to

int id = R.id.imageButton;

ImageButton imageButton = (ImageButton) findViewById( id );
mrres1
  • 1,127
  • 6
  • 10
0

You don't need a function to give you the integer resource id.

All you have to do is R.id.(drawable name) and that is the int.

If you wanted to get the actual integer number, you would call getInteger(int id).

So in your case, just do R.id.imageButton1.

royn1
  • 35
  • 5
0

here is a simple example to get and set image for ImageButtons.

    ImageButton x = (ImageButton) findViewById(R.id.imageButton1);
    ImageButton x2 = (ImageButton) findViewById(R.id.imageButton2);
    x2.setImageDrawable(x.getDrawable());
Eng. Samer T
  • 6,036
  • 5
  • 32
  • 39
0

The getIdentifier() method returns the resource id from a resource name. Here's how you'd use this, assuming imageButton1 is your resource name(file name in res/drawable):

context.getResources().getIdentifier("imageButton1", "drawable", context.getPackageName());

umanx
  • 1
  • 1
  • 1