8

Possible Duplicate:
Android, getting resource ID from string?

String res = "R.drawable.image1";

I need to get it's INT id. Please help me!

Community
  • 1
  • 1
Saruulbat
  • 301
  • 2
  • 3
  • 8

3 Answers3

27

Use this to get resource id:

int picId = getResources().getIdentifier(picName, "drawable", getApplicationContext().getPackageName());
Eng.Fouad
  • 111,301
  • 67
  • 311
  • 403
Nishant
  • 31,733
  • 5
  • 37
  • 53
4

If you want to get the integer then simply remove the quotes.

int id = R.drawable.image1; // instead of "R.drawable.image1"

That will give you the number.

Michal
  • 15,101
  • 9
  • 71
  • 103
2
View view = findViewById(R.drawable.image1);
int id = view.getId();
Chandra Sekhar
  • 17,770
  • 14
  • 76
  • 115