I got about 200+ Country names in my app.
I got 200+ flag icons for each country.
The flag icons names are equals the country names, so like:
Country name: ENG, icon name eng.png
I want to make a list of them in my app to select country.
I dont want to build the layout by manually and add 200+ icons for each and every TextView...
My question is, can i add dynamically somehow ?
Something like this:
private void setIcon(String iconName)
{
countryIcon.setBackgroundResource(R.drawable. /* and i need the magic here*/ +iconName )
}
So can i reference to R.drawable dynamically by somehow with param?
Try this:
private void setIcon(String iconName) {
Resources res = getResources();
int imageResource = res.getIdentifier("drawable/" + iconName, null, getPackageName());
Drawable image = res.getDrawable(imageResource);
countryIcon.setBackgroundResource(image);
}
If you've a pattern, is not difficult at all.
Here's an example. I did this for loading flag-images in a ListView depending the flag IDs of each row-object. Once i've the language_id i get the language_KEY (String) which is the same as my icon's name:
int ico_id, int visible=View.visible();
String flag;
Bitmap icona;
flag= (MyWharehouse.getLangKey(language_id.get(position))).toLowerCase(); //get the image.png name
ico_id = a.getResources().getIdentifier(flag, "drawable", a.getString(R.string.package_str));
icona = BitmapFactory.decodeResource(a.getResources(), ico_id);
((ImageView)a.findViewById(R.id.im_lang_01)).setImageBitmap(icona); //changing the image
Related
I need to load an image from drawable to an image view.
The ImageView is part of a ListViewItem, hence I have a large number of items and a large number of images.
When loading, the app parses a JSON file which states, among other information, the file name to be loaded per list item.
for example, "test_a.jpg".
The file "test_a.jpg" is located in "drawable".
I want, on runtime, to load drawable/test_a.jpg, test_b.jpg, big_chief.jpg and so on...
int intCurrImageResourceID = this.context.getResources().getIdentifier(strPictureName, "drawable", context.getPackageName());
any ideas on how to achieve that?
*** EDIT ****
It turns out that you shouldn't use file extension. So image.jpg is wrong. image is right. Thanks all. Especially #Augusto Carmo.
---- updated (correct answer)
A question, in your code, is strPictureName equals, for example, to something like this: "myDogPicture.png"? If yes, you should remove ".png" from the string first. Test it :) To get images from drawable with resource ids, you should not use the image extension.
---- old answer
You can achieve this with the following code:
// example - your ImageView
ImageView myImageView = (ImageView)findViewById(R.id.my_image_view);
// suppose the name of your image in drawable/ is my_image_name.jpg.
myImageView.setImageResource(R.drawable.my_image_name);
If you already know what the mapping between strings and resource ids will be, what do you think about using enum? example:
public enum MyImage{
CAT_IMAGE("cat.png", R.drawable.cat),
DOG_IMAGE("dog.png", R.drawable.dog),
HORSE_IMAGE("horse.png", R.drawable.horse);
private final String fileName;
private final int resId;
MyImage(String fileName, int resId){
this.fileName = fileName;
this.resId = resId;
}
/**
*
* #param imageFileName
* #return -1 if image not found
*/
public int getResId(String imageFileName){
int resId = -1;
for (MyImage myImage : MyImage.values()) {
if (imageFileName.equals(myImage.fileName)){
resId = myImage.resId;
break;
}
}
return resId;
}
}
I want the user to select a wallpaper and I'll set it as the android wallpaper.
I'm currently using this line:
myWallpaperManager.setResource(R.drawable.a1);
Problem is it's not dynamic and I want to change the chosen image to R.drawable.a2,R.drawable.a3,R.drawable.a4, etc based on the number I got.
So if I have int chosenPicNum=3 I want to create the string "R.drawable.a"+3 and then call myWallpaperManager.setResource(R.drawable.a3);
But I can't do it. The error is eclipse reads it as a string and not a resource.
Currently here's the closest I've gotten:
String imageResource="R.drawable.a"+chosenPicNum) ;
So imageResource in this case is String type "R.drawable.a3" but I want it to be R.drawable.a3.
Thank you for your help!!
Dvir, ambitious 20 year old :)
Use getIdentifier() like this:
public final static String PACKAGE = "..."; // insert your package name
private int getDrawable(String name) {
return getId(name, "drawable");
}
private int getId(String name, String type) {
return getResources().getIdentifier(name, type, PACKAGE);
}
Access the method using:
myWallpaperManager.setResource(getDrawable("a" + chosenPicNum));
As you use resources, you already know their number and names. So use an array:
int[] ids = new int[] {R.drawable.a1, R.drawable.a2, R.drawable.a3, };
And when you have your chosenNumPic (assuming it's 0 based):
if (chosenNumPic >= 0 && chosenNumPic < ids.length) {
myWallpaperManager.setResource(ids[chosenNumPic]);
}
Hope it helps :)
try this
int resId = getResources().getIdentifier("your_drawable_name"), "drawable", getActivity().getPackageName());
imageview.setBackgroundResource(resId);
I have 10 images on /resources/drawable folder as following names;
pr1.gif, pr2.gif, ... pr10.gif
And I can easily set an image like this;
imgview1.setImageResource(R.drawable.pr5);
But I can't set it like following;
int pr = getpr(dm);
imgview1.setImageResource(R.drawable.pr + pr);
Is there a way to set it like this or should I use switch case?
You can get the id of a drawable by name using getIdentifier():
String prefix = "pr";
int suffix = getpr(dm); // I assume this returns the image number
Resources res = getResources();
int resId = res.getIdentifier(prefix + suffix, "drawable", "my.package.name");
imgview1.setImageResource(resId);
Note you can use this for any type of resource, whether it's drawable, string, array, etc. Just make sure you change the second parameter from "drawable" to the appropriate type.
I have an Activity which has variable position that comes from another activity. I handle this variable with Intent like below. (Actually position is listView's position from another activity.) and values of position variable are 0 to 200
I took my Extra, then assigned it to a string than I parsed it to an Integer. These are normal and easy for me and working.
Intent i = getIntent();
String position = i.getStringExtra("POSITION");
int position_to_int = Integer.parseInt(position);
But my problem is about resources.
Simply I want to put an image to my LinearLayout dynamically . I have some country flags and they named in format like 0.gif, 1.gif, 2.gif, 3.gif ...
My idea is using position variable's value for this purpose.
but when I try to use position_to_int variable for name of flag, Eclipse returning error normally. Because R.drawble.XXX values storing in R.java
ImageView imageView = new ImageView(this);
// setting image resource
imageView.setImageResource(R.drawable.position_to_int);
How can I use position_to_int variable to show an ImageView dynamically?
You can't access it directly. You'll have to get the resource using its name:
private Drawable getDrawableResourceByName(int name) {
String packageName = getPackageName();
int resId = getResources().getIdentifier("character_you_prefixed" + String.valueOf(name), "drawable", packageName);
return getResources().getDrawable(resId);
}
Note that your resource names must begin with a letter, not a number, or else your project will not compile. Try adding a character to the beginning of each file name.
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?
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);
you use that code
ImageView[] ivCard = new ImageView[1];
#override
protected void onCreate(Bundle savedInstanceState)
ivCard[0]=(ImageView)findViewById(R.id.imageView1);
You can use this code:
// Create an array that matches any country to its id (as String):
String[][] countriesId = new String[NUMBER_OF_COUNTRIES_SUPPORTED][];
// Initialize the array, where the first column will be the country's name (in uppercase) and the second column will be its id (as String):
countriesId[0] = new String[] {"US", String.valueOf(R.drawable.us)};
countriesId[1] = new String[] {"FR", String.valueOf(R.drawable.fr)};
// and so on...
// And after you get the variable "countryCode":
int i;
for(i = 0; i<countriesId.length; i++) {
if(countriesId[i][0].equals(countryCode))
break;
}
// Now "i" is the index of the country
img.setImageResource(Integer.parseInt(countriesId[i][1]));
you may try this:-
myImgView.setImageDrawable(getResources().getDrawable(R.drawable.image_name));