Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Resource(int) from String - Android [duplicate]

Possible Duplicate:
Android, getting resource ID from string?

String res = "R.drawable.image1";

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

like image 418
Saruulbat Avatar asked Apr 16 '12 08:04

Saruulbat


3 Answers

Use this to get resource id:

int picId = getResources().getIdentifier(picName, "drawable", getApplicationContext().getPackageName());
like image 84
Nishant Avatar answered Nov 04 '22 06:11

Nishant


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.

like image 36
Michal Avatar answered Nov 04 '22 06:11

Michal


View view = findViewById(R.drawable.image1);
int id = view.getId();
like image 2
Chandra Sekhar Avatar answered Nov 04 '22 07:11

Chandra Sekhar