Home » Android » java – How to get Resource(int) from String – Android

java – How to get Resource(int) from String – Android

Posted by: admin June 15, 2020 Leave a comment

Questions:

Possible Duplicate:
Android, getting resource ID from string?

String res = "R.drawable.image1";

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

How to&Answers:

Use this to get resource id:

int picId = getResources().getIdentifier(picName, "drawable", getApplicationContext().getPackageName());

Answer:

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.

Answer:

View view = findViewById(R.drawable.image1);
int id = view.getId();