How can i set background image for an ImageView dynamically???

3 replies [Last post]
lidochca
User offline. Last seen 1 year 23 weeks ago. Offline
Joined: 08/20/2010

Hello guys,

I have the following question.
I need to use R.drawable.id's dinamicaly, depending on the country choosen, more precisely,depending on a "country" variable.
For example, my screen contains a country flag. If the Deutschaland is the choosen country, i have to display something like this:

ImageView flag;
flag=(ImageView)findViewById(R.id.ImageView01);
flag.setBackgroundResource(R.drawable.flag_de);

Inside android application, in "res/drawable-hdpi" i have saved all needed flags with the following names: flag_de.jpg, flag_uk.jpg, flag_ro.jpg and so on.

How can i set background image for this "flag" value dynamically?????
If i use the following way, there are too mush code:

if (country.equals("DE") {
flag.setBackgroundResource(R.drawable.flag_de);
} else if (country.equals("UK")) {
flag.setBackgroundResource(R.drawable.flag_uk);
} else if (country.equals("RO")) {
flag.setBackgroundResource(R.drawable.flag_ro);
}

I want to create somehow "R.drawable.flag" by appending the country extension "de" .

String countryFlag="flag_"+mySource.countryExtension ; //something like flag_DE
countryFlag=countryFlag.toLowerCase(); // =flag_de
flag.setBackgroundResource(R.drawable.countryFlag); // of course, it's not working

Could anyone give me a suggestion please ?
Thank you
Lidy