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 (Example: country code=US, image file=us)
My code (countryCode is the current countryCode in uppercase letters):
String lowerCountryCode = countryCode.toLowerCase(); String resource = "R.drawable." + lowerCountryCode; img.setImageResource(resource);
Now, of course this will not work because setImageResource
wants an int
, so how can I do this?
Displays image resources, for example Bitmap or Drawable resources. ImageView is also commonly used to apply tints to an image and handle image scaling. To learn more about Drawables, see: Drawable Resources.
ImageView imageView = new ImageView(this); imageView. setImageResource(R. drawable. beerbottle); RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.
This example demonstrates how do I clear an imageview in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.
Go to the “Enhance” menu on the app's homescreen. The enhance function helps optimize your photos for enhanced facial features and a sharper background. It can help reduce noise and strange-looking pixels and works well on low-resolution and blurry photos. Click “use it” to proceed with the deblurring.
One easy way to map that country name that you have to an int
to be used in the setImageResource
method is:
int id = getResources().getIdentifier(lowerCountryCode, "drawable", getPackageName()); setImageResource(id);
But you should really try to use different folders resources for the countries that you want to support.
This is how to set an image into ImageView
using the setImageResource() method:
ImageView myImageView = (ImageView)v.findViewById(R.id.img_play); // supossing to have an image called ic_play inside my drawables. myImageView.setImageResource(R.drawable.ic_play);
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