Greyshack
Greyshack

Reputation: 1971

Refering to id by string android

Can I somehow do something like this, that when I want to load multiple bitmaps in a loop and they have similar name which differs only in number (for example R.id.bitmap1, R.id.bitmap2) I can refer to it by some string that I will change.

Because you can for example do something like this:

Class myClass = Class
.forName("com.something.something." + menuName);
Intent intent = new Intent(this, myClass);
startActivity(intent);

I was wondering if I can do sth similar with bitmaps

Upvotes: 0

Views: 72

Answers (1)

zadkiel
zadkiel

Reputation: 70

That's how to do:

int drawableResId = getResources().getIdentifier("my_drawable", "drawable", getPackageName());
Bitmap bitmap = BitmapFactory.decodeResource(getActivity().getResources(), drawableResId);

You can also do the same with views and strings:

int viewResId = getResources().getIdentifier("my_view", "id", getPackageName());
int stringResId = getResources().getIdentifier("my_string", "string", getPackageName());

Upvotes: 2

Related Questions