How to determine the ID in the R.java file of a drawable resource in Android?

Assume you have several drawables in your Android resource folder and what to iterate over them. You use a certain naming schema and would like to use this to determine the resources ID’s.

The following snippets shows how to get a resource ID based on the resource name:

// Name of resource
// Type
// Package of the app
int identifier = getResources().getIdentifier("pic1", "drawable","android.demo");
ImageView image = (ImageView) findViewById(R.id.imageView1);
image.setImageResource(identifier);

I hope this helps.

About Lars Vogel

Lars Vogel is the founder and CEO of the vogella GmbH and works as Eclipse and Android consultant, trainer and book author. He is a regular speaker at international conferences, With more than one million visitors per month his website vogella.com is one of the central sources for Eclipse and Android programming information.
This entry was posted in Android and tagged , . Bookmark the permalink.

2 Responses to How to determine the ID in the R.java file of a drawable resource in Android?

  1. fco barrios says:

    Help a lot thx

  2. Viktor says:

    Simple yet effective. Thx for the tip!

Comments are closed.