Access a image dynamically from res folder android - android

I have a image in res folder like this
I want to access this one dynamically like this
holder.Viewcover.setImageDrawable(Drawable.createFromPath("R.id." + CoverimgUrl.get(position)));
CoverimgUrl is a list which have two image name one is book_cover & and another is blank_image this array is generated dynamically so how can I set this image from that list
In One word how to access a image dynamically which is in drawable folder and I need get that image name from an array list ?

Resources res = getResources();
String mDrawableName = "image_name";
int resourceId = res.getIdentifier(mDrawableName , "drawable", getPackageName());
Drawable drawable = res.getDrawable(resourceId);
icon.setImageDrawable(drawable );

First Make CoverimgUrl list of integer
List<Integer> CoverimgUrl =new ArrayList<Integer>();
CoverimgUrl.add(R.drawable.book_cover);
CoverimgUrl.add(R.drawable.blank_image);
Then
holder.Viewcover.setImageResource(CoverimgUrl.get(position));

createFromPath expects a path to the file, not it's ID.
You can use the following:
int id = getResources().getIdentifier(CoverimgUrl.get(position), "id", getPackageName());
holder.Viewcover.setImageDrawable(getResources().getDrawable(id));
getIdentifier() gets the ID from the string. When you use the "R" class, it contains static integers for ids. So R.id.some_name is actually an integer, which is the id of the some_name resource.
once you get this integer with getIdentifier, you can use getResources().getDrawable() to get the drawable with the given ID.
Let me know if this works.

Related

How to change an ImageView with a filename

I have a string with the name of the file that I want (R.drawable.square) to put in the imageview.
String shape = "square"
I am trying to use the method below to show the image
imageView.setImageResource()
Hardcoding the filename in works but I want to be able to pass different filenames in.
image.setImageResource(R.drawable.square)
tl;dr
can't get from String:"square" to int:R.drawable.square
Please see Android, reference things in R.drawable. using variables?
//to retrieve image in res/drawable and set image in ImageView
String imageName = "picture"
int resID = getResources().getIdentifier(imageName, "drawable", "package.name");
ImageView image;
image.setImageResource(resID );
I achieved the effect by putting the images in the assets directory
I think it's better to use you Strings as key and your resource ids as value in a Map Object.
Map<String, Integer> resources = new HashMap<>();
resources.put("square", R.drawable.square);
// resources.put("...", R.drawable....);
imageView.setImageResource(resources.get("square"));

Use the address in the database for imageview

I'm sorry for my English .
I have a SQLite database of stored name of images in the drawable folder.
Example : R.drawable.image1 , R.drawable.image2 , ...
Is there a way that addresses stored in the database attributed to ImageViews?
Yes, you can get a Drawable by it's name, but it needs to be the full name of the resource, including the extension. So, for instance, if "R.drawable.image1" refers to an image named "image1.png", you will need to look it up by the string "image1.png".
Given the name of the image (e.g. "image1"), you can use the following method to get the resource id of the image (assuming that all of your images have the ".png" extension):
private final Map<String, Integer> iconMap = new HashMap<>();
public int getIconResourceId(final String imageName) {
String name = imageName.replace(".png", "");
Integer resourceId = iconMap.get(name);
if(resourceId == null) {
Resources resources = getResources();
resourceId = resources.getIdentifier(name, "drawable", getPackageName());
iconMap.put(name, resourceId); // cache the entry
}
return resourceId.intValue();
}
Note that the above code caches the resource ids - I use this in code for a RecyclerView, where the same image may need to be retrieved often, while scrolling the RecyclerView. If you don't need that, you may want to remove the HashMap.

Android: Dynamic Button Names

I want to change the text on some buttons dynamically.
All necessary text is saved at the strings.xml and the reference name is saved in a Json Object.
Anyone knows how i can get the data from strings.xml for my setText function?
JSONObject e = Options.getJSONObject(i);
//The Name for strings.xml
String name = e.getString("name");
Resources res = getResources();
button1.setText(String.format(res.getString(R.string. ??????????? )));
Use something like this
getResources().getIdentifier(name, "string", “com.main.package”));
or better:
getResources().getIdentifier(name, "string", getPackageName()));
You can get the any string from String.xml by
getString(R.string.string_name);

get Image Name in ImageView Android

I am dynamically loading images in image view by setting
view.setBackgroundResource(cards[i])
// cards is the image array
cards[] = {R.drawable.image1,R.drawable.image2....}
is there any way I can know the image name or image id associated with image view in android?
You can use the name of a resource like
getIdentifier (String name, String defType, String defPackage);
int id = getResources().getIdentifier("us","drawable","com.app");
The above function will return an integer value same as R.drawable.us.
This is how you access with resource names.
To access the resources name
String resName = getResources().getResourceName(R.drawable.imagename)

How to store drawable in an array

I want to download a bunch of images and would like to store it as drawable in an array. So I tried declaring a drawable array.But it returns me nullPointer exception when I access that array.
My question is, how to declare an array type as drawable ?
an array of drawables would be declared like:
int numDrawables = 10;
Drawable[] drawableArray = new Drawable[numDrawables];
to fill the array:
for(int i = 0; i < numDrawables; i++){
// get a drawable from somewhere
Drawable drawable = new Drawable();
drawableArray[i] = drawable;
}
to access the array:
Drawable aDrawable = drawableArray[0];
This is basic java, If you are getting null pointer exceptions, you are doing something wrong.
you can use an hashMap , and put the keys as urls for example , and to value is the drawable that you download :
HashMap<String,Drawable> myDrawables = new HashMap<String,Drawable>();
//before you download the images, you can test if your drawable is already downloaded,
// with looking for his url in the Hashmap ,
.....
myDrawables.put(urlOfYourDrawable, yourDrawable);
If you want to store list of elements(any type) dynamically use the classes in the collection. why because Array size is static.
Example:
ArrayList<Generic> list=new ArrayList<Generic>();
list.add(Generic);

Categories

Resources