I have this database of 100+ images (of country flags) in my drawable folder.
Now I want to display the flag of the country you're currently in, in an ImageView.
I get the country with String country_variable = address.getCountryCode();
And I set the image with flag.setImageDrawable(getResources().getDrawable(R.drawable.country_variable));
As you all know R.drawable.country_variable wont't work because the compiler can't find a image named country_variable in the drawable folder.
What is the best way to do this?
You should be able to use getResources().getIdentifier() to get the id by the resource name. Something like:
flag.setImageDrawable(getResources().getDrawable(getResources().getIdentifier("drawable/" + country_variable, "drawable", getPackageName()));
Try this:
flag.setImageDrawable(getResources().getDrawable(getResources().getIdentifier(country_variable, "drawable", getPackageName()));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With