Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i access an Android drawable by a variable

Tags:

android

How can i access an Adroid drawable by a variable? Example I have drawables :[ logo1.png logo2.png ... logoN.png]; Is there a way so i can do that?

String logopicker="1";
logo.setImageResource(R.drawable.**logo+logopicker**);

Well i know this code will never run but you get the idea of what i want to do. Thank you

like image 539
weakwire Avatar asked Jan 19 '26 14:01

weakwire


1 Answers

This code is running for me successful (I have already used in my one of the application), You can Try this:

     cnt=1;

     String icon="logo" + cnt;
     int resID = getResources().getIdentifier(icon, "drawable",  getPackageName()); 
     logo.setImageResource(resID); 

     cnt++;  // this require if you want to set images in loop

Enjoy !!

like image 183
Paresh Mayani Avatar answered Jan 22 '26 18:01

Paresh Mayani