14

I have a short question:

How is it possible to convert a String, containing the Id of a Drawable, which is

String idString = "R.drawable.bubblegum";

to an Integer,

idInt

so that I can use that ID for the reference of an image (, which is used in a SimpleAdapter)

So, to make an example, I can't do that:

bubble.setImageDrawable(this.getResources().getDrawable(idString));
//not possible, cause idString is a String an not an Id/Int

So, I have the String that's containing the id, but unfortunately as a String.

Thanks!

10ff
  • 813
  • 4
  • 16
  • 31

6 Answers6

22

Although this question is rather old already, the thing you're missing is that "id" and "drawable" are different resource types. So instead of

getResources().getIdentifier(stringId, "id", "my.Package");

it's

getResources().getIdentifier(stringId, "drawable", "my.Package");

You can also get package name with the activity context like activityContext.getPackageName()

/**
 * Returns Identifier of String into it's ID as defined in R.java file.
 * @param pContext
 * @param pString defnied in Strings.xml resource name e.g: action_item_help
 * @return
 */
public static int getStringIdentifier(Context pContext, String pString){
    return pContext.getResources().getIdentifier(pString, "string", pContext.getPackageName());
}
AZ_
  • 21,688
  • 25
  • 143
  • 191
domsom
  • 3,163
  • 1
  • 22
  • 27
13

Call getIdentifier() on the Resources object you get via getResources(), as seen in these StackOverflow questions:

among others.

Community
  • 1
  • 1
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 2
    When I do this: String stringId = "R.id.icon"; long intId = getResources().getIdentifier(stringId, "id", "my.Package"); Log.d("Test",intId+""); then LogCat displays 0. Wrong Way? – 10ff Nov 07 '11 at 22:49
3

You could try the following

int id = getResources().getIdentifier("arr_name"+positionSelected,
                        "array", rootview.getContext().getPackageName());

I use in spinner dropdown, get array string follow parent spinner may help you!

Ojash
  • 1,234
  • 8
  • 20
0

int idInt = R.drawable.bubblegum;

Unless there's something I'm missing here.

DeeV
  • 35,865
  • 9
  • 108
  • 95
  • how does findViewById(R.drawable.bubblegum) return a ImageView? – Yashwanth Kumar Nov 07 '11 at 14:58
  • It passes a reference to it. `ImageView im = (ImageView) findViewById(R.id.bubblegum);`. `findViewById` only accepts ID numbers from within the ViewGroup hierarchy. You can't use the drawable id. – DeeV Nov 07 '11 at 15:04
  • You are missing the fact that the id is known as a string only. – njzk2 Nov 07 '11 at 15:29
  • sorry, I made a mistake, I didn't want to reference that ImageView, I just tried to make an example..which was wrong. But I edited it.My problem is, like njzk2 said, that the id is known as a string only. – 10ff Nov 07 '11 at 22:29
0

if your idString is constant, i.e. it's doesn't change during runtime, follow DeeV's answer. If it changes, you can take a look at getIdentifier method.

Vladimir
  • 9,683
  • 6
  • 36
  • 57
-2

At least, I couldn't get a solution for this problem. It seems that there's no way to convert a String "R.id.mytext" into an integer like R.id.mytext that can be used in findViewById(R.id.myText).

10ff
  • 813
  • 4
  • 16
  • 31
  • Everybody who answered is missing the point. Assuming that you are simply trying to just reference the id of a resource that already exists as you write the code. But just a thought here, what if you do not know at all. Or maybe you are trying to assign an id. Such as Menuitem.add(int resId) ??? What to do in that case? – Xijukx Sep 25 '19 at 08:38