Reputation: 151
I have an imageView that i want to display a little icon of the country that you are currently in. I can get the country code, but problem is i can't dynamically change the imageView resource. My image files are all lowercase
String lowerCountryCode = countryCode.toLowerCase();
String resource = "R.drawable." + lowerCountryCode;
img.setImageResource(resource);
int id = getResources().getIdentifier(lowerCountryCode, "drawable", getPackageName());
setImageResource(id);
abve is my code.
Now, of course this will not work because set Image Resource wants an int, so how can i do this? Thanks in advance
Upvotes: 1
Views: 7521
Reputation: 11310
I mean you know the name of files(Drawable) which you wanna use for one and other country.
ImageView imgView=(ImageView)findViewById(R.id.imageView1);
int id = getResources().getIdentifier(lowerCountryCode.toLowerCase(), "drawable", getPackageName());
imgView.setImageDrawable(res.getDrawable(id));
Upvotes: 3
Reputation: 30864
The following code should work well (assuming that you really have a file with name lowerCountryCode
in your res/drawable/
folder)
String lowerCountryCode = countryCode.toLowerCase();
int id = getResources().getIdentifier(lowerCountryCode, "drawable", getPackageName());
ivCard[0].setImageResource(id);
Upvotes: 1
Reputation: 151
i use that code....
ImageView[] ivCard = new ImageView[1];
@override
protected void onCreate(Bundle savedInstanceState)
ivCard[0]=(ImageView)findViewById(R.id.imageView1);
Upvotes: 0