how to get name of a drawable assets dynamically in android? - android

I have some photos in my drawable folder res that name of them is like
move1.jpg
move2.jpg
move3.jpg
and so on.
I have a table in my database which has a field to store photo ID.
I have a selected list of items (in my case movement), that I want, user see the proper photo based on the Id of photo.
movements = db.getAllMovements(true);
...
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Dialog movement_image_dialog = new Dialog(getActivity());
movement_image_dialog.setContentView(R.layout.image_dialog);
ImageView movement_imageView = (ImageView) movement_image_dialog.findViewById(R.id.movement_imageview);
**int photoId = finalMovements.get(position).getPhotoId();**
**movement_imageView.setImageResource(R.drawable.*move4*);**
...
}
My question is how I can set the Image resource with the photoId that I have?
I need something like this
R.drawalbe.(move+photoId)

You can use resource getIdentifier(String name, String defType, String defPackage) as
int drawableResourceId = this.getResources().getIdentifier("nameOfDrawable", "drawable", this.getPackageName());
Where
this is an Activity.
nameOfDrawable is move1, move2 etc in your case.
drawableResourceId is the one you can pass in
movement_imageView.setImageResource(drawableResourceId);

we get dinamically images from like this.
String imageId=yourImageId;
Drawable drawable = getResources().getDrawable(getResources()
.getIdentifier(imageId, "drawable", getPackageName()));

I think you should try this..
int photoId=R.drawable.move1;
img.setImageResource(photoId);

Related

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.

How to link database record with drawable id?

In my android application I have categories that stored in categories table of database. And I want to assign icons for each category. Category table looks like:
The problem is that I am not able to use drawable constants that defined in R file because they are not static and will be changed from build to build.
Is it correct to create public.xml file and define all drawable constants inside the file?
I am afraid that I can override some android constants using this approach.
Why don't you simply use the drawable names?
You can get the resource id at run time, for the given resource name and type.
Add this method to your code:
protected final static int getResourceID
(final String resName, final String resType, final Context ctx)
{
final int ResourceID =
ctx.getResources().getIdentifier(resName, resType,
ctx.getApplicationInfo().packageName);
if (ResourceID == 0)
{
throw new IllegalArgumentException
(
"No resource string found with name " + resName
);
}
else
{
return ResourceID;
}
}
And use it like this:
int myID =
getResourceID("your_resource_name", "drawable", getApplicationContext());
Note: no path nor extension, in case of images.
I think you need this:
int imageResource = context.getResources().getIdentifier(imageName,
"drawable", context.getPackageName());
and if you want to set the image to an imageView:
// set the image
if (imageResource != 0)
yourImageView.setImageResource(imageResource);

Access a image dynamically from res folder 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.

Nullpointer when trying to find a string by ID

I'm using the below code to find a resource by id;
setContentView(R.layout.golders);
for (int i=1; i<hm.size()+1;i++)
{
int id = getStringIdentifier("Bus"+i);
view = (TextView)findViewById(id);
view.setText(hm.get(i).toString());
}
My getStringIdentifier() is working but when I try to set the text I'm getting a NullPointerException.
I've used the setContentView to focus on the golders.xml file which has the ids that I want to update.
I've tried Cleaning the project but that hasn't done anything either, any ideas?! Thanks!
EDIT:
public int getStringIdentifier(String aString)
{
String packageName = "com.example.bustimetable.Robbos";
int resId = getResources().getIdentifier(aString, "string", packageName);
return resId;
}
Your getStringIdentifier(String) method returns a string ID (something from R.string). You need a new method, something like getIdentifier(String), that will return soemthing from R.id. I can't see the XML, so I don't know what your TextView's ID is, but... you'll want to verify that the ID is, in fact, Bus_ where the _ is some number.
public int getIdentifier(String aString)
{
String packageName = "com.example.bustimetable.Robbos";
int resId = getResources().getIdentifier(aString, "id", packageName); // Get from R.id, not R.string
return resId;
}
The problem is that getStringIdentifier() is returning an id for a string resource. Apparently the id which is returned is not a valid id for a view resource. Even if was, I don't know how you can guarantee that you will get the view that you want. You need to either add another method to return id's for view resources, or modify the one you have so that it will return id's for either string or view resources

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)

Categories

Resources