How to get Resource name? - android

How to get image name and display it in a textbox?
I used arraylist that generates random images.
String ImageName = getResources().getResourceName((Integer)list.get(position));
The output I get from this code is R.drawable.image1. How to get the image name image1?

try this,
String name = getResources().getResourceEntryName(image[position]);

Try this
string.substring(string.lastIndexOf("."),string.length());

String name = "image1";
int imgId = getResources().getIdentifier("drawable/" + name, "drawable", getPackageName());

Related

how to get name of a drawable assets dynamically in 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);

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.

read message value from string.xml file in android

i have string like #string/screen_enable_auto_mode. I know that i can use:
String mess = getResources().getString(R.string.screen_enable_auto_mode);
and i will get a message.
But i have whole string as #string/screen_enable_auto_mode. How can i parse it to
String mess = getResources().getString(R.string.screen_enable_auto_mode);
that i will get a message from xml?
You can use Resources.getIdentifier for this.
int resId = getResources().getIdentifier(
"screen_enable_auto_mode", "string", "com.package.app");
String mess = getResources().getString(resId);
Replace "com.package.app" with the actual package of your app, of course.

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)

Drawable resource using a variable

I want to use a variable instead of R.drawable.myimage because I have a database with the image's names. I get the name from the database and I want to use the drawable resource with this name.
String flagimage = "R.drawable." + myHelper.getFlagImage(5);
int resourceId = Integer.parseInt(flagimage);
Bitmap flag = BitmapFactory.decodeResource(getResources(), resourceId);
You can use the name of a resource like
getIdentifier (String name, String defType, String defPackage);
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.
You can use this code to get the identifier...
Resources res = this.getResources();
int resID = res.getIdentifier(imagename, "drawable", this.getPackageName());

Categories

Resources