Find drawable by string
Yes, you can look it up by name using Resources.getIdentifier()
.
Context context = imageView.getContext();
int id = context.getResources().getIdentifier("picture0001", "drawable", context.getPackageName());
imageView.setImageResource(id);
It's not efficient, but it works to look up occasional resources.
how to get drawable from string?
First get rid of the prefix "R.drawable."
:
String s = myString.replace("R.drawable.", "");
and then get the drawable
's integer id with getIdentifier()
:
int id = context.getResources().getIdentifier(s, "drawable", context.getPackageName());
context
is a valid Context
you must supply, or if your code is inside an activity you can omit it:
int id = getResources().getIdentifier(s, "drawable", getPackageName());
How can I get drawable resource by string?
You can use LocalContext
to get current context, and then use same methods as you used in view based Android:
val context = LocalContext.current
val drawableId = remember(name) {
context.resources.getIdentifier(
name,
"drawable",
context.packageName
)
}
Image(
painterResource(id = drawableId),
contentDescription = "..."
)
Get drawable by string
Use this function to get a drawable when you have the image name. Note that the image name does not include the file extension.
public static Drawable GetImage(Context c, String ImageName) {
return c.getResources().getDrawable(c.getResources().getIdentifier(ImageName, "drawable", c.getPackageName()));
}
then just use setBackgroundDrawable
method.
If you only want the ID, leave out the getDrawable part
i.e.
return c.getResources().getIdentifier(ImageName, "drawable", c.getPackageName());
how get Drawable from String path?
The method getDrawable()
is deprecated and the alternative is
ContextCompat.getDrawable(context, R.drawable.***);
But if you have a String
name of the Image. Then here is what you have to do:
getResources().getIdentifier("image_name_in_drawable_folder", "drawable", getActivity().getPackageName());
Do not put the full path from the Uri. Just put the identifier name only the image name only.
Android: How to search image-name (=string) in drawable directory and show it in an ImageView?
This code:
int resId = context.getResources().getIdentifier(imagename, "drawable", context.getPackageName());
iv.setImageResource(resId);
sets to the ImageView
with id iv
the image with name (id) imagename
(excluding .jpg) after finding its id in folder drawable
.
If your code is in an activity, replace context
with this
.
How can I convert String to Drawable
This can be done using reflection:
String name = "your_drawable";
final Field field = R.drawable.getField(name);
int id = field.getInt(null);
Drawable drawable = getResources().getDrawable(id);
Or using Resources.getIdentifier()
:
String name = "your_drawable";
int id = getResources().getIdentifier(name, "drawable", getPackageName());
Drawable drawable = getResources().getDrawable(id);
Then use this for setting the drawable in either case:
view.setBackground(drawable)
Android - Open resource from @drawable String
If you really need to work with a string, try something like this:
private void showImage() {
String uri = "drawable/icon";
// int imageResource = R.drawable.icon;
int imageResource = getResources().getIdentifier(uri, null, getPackageName());
ImageView imageView = (ImageView) findViewById(R.id.myImageView);
Drawable image = getResources().getDrawable(imageResource);
imageView.setImageDrawable(image);
}
Else I would recommend you to work with R.* references like this:
int imageResource = R.drawable.icon;
Drawable image = getResources().getDrawable(imageResource);
Accessing drawable file from String Arraylist in android
You can get drawable from your drawable res folder by the saved name. For example if you have ic_home.png
in your res/drawable
folder you can get that passing "ic_home"
as the first parameter of this method.
public static Drawable getDrawable(String name, Context context) {
int resourceId = context.getResources().getIdentifier(name, "drawable", context.getPackageName());
return context.getResources().getDrawable(resourceId);
}
How to get drawable resource id from string name base on current app theme in android (dark/light theme)
Short answer: Don´t use setImageResource(Int id). Get the themed drawable with getResources().getDrawable(Int id, Resources.Theme theme) and use setImageDrawable(Drawable drawable) instead.
Explanation: Themed resources don´t have a particular id suffix/preffix; is the same ID across all themes, to avoid breaking the app. The theme is supposed to be applied beforehand, allowing all the theme-sensitive values to be replaced by the theme´s version. Usually, a visual context (activity) contains the reference, but depending on how/when you are trying to get the resource, you may get one with a different theming (application context for example will not have theme references), and because that, we have getDrawable(Int id, Resources.Theme theme) since api 22. That´s the root of your problem. When you call setImageDrawable(Int res), the imageView executes this block:
private void resolveUri() {
if (mDrawable != null) {
return;
}
if (getResources() == null) {
return;
}
Drawable d = null;
if (mResource != 0) {
try {
d = mContext.getDrawable(mResource);
} catch (Exception e) {
Log.w(LOG_TAG, "Unable to find resource: " + mResource, e);
// Don't try again.
mResource = 0;
}
} else if (mUri != null) {
d = getDrawableFromUri(mUri);
if (d == null) {
Log.w(LOG_TAG, "resolveUri failed on bad bitmap uri: " + mUri);
// Don't try again.
mUri = null;
}
} else {
return;
}
updateDrawable(d);
}
as you can see, it uses the deprecated Context.getDrawable(Int id) method. So the drawable will, at best, match the theme reference in the context, if any. In your widget, the call likely happens before the rest of the change happens. So, specify the desired theme instead and you´ll be okay.
Related Topics
How to Load Image as Bitmap to Glide
Use 32-Bit Jni Libraries on 64-Bit Android
Sethinttextcolor() in Edittext
How to Take Pictures from The Camera Without Preview When My App Starts
Getting The Current Position of a Viewpager
Flutter Insecure Http Is Not Allowed by Platform
Android Viewpager Padding/Margin Between Page Fragments
Findbugs Idea - Classnotfoundexception Com.Google.Wireless.Android.Sdk.Stats.Intellijindexingstats
Change Button Text and Action - Android Development
How to Access an Existing Sqlite Database in Android
Android: Is There Any Free PDF Library for Android
How to Know If Android Talkback Is Active
Change Navigation Bar Icon Color on Android
Android Build Gradle Is Too Slow (Dependency Resolution)
Crop Square Image to Circle - Programmatically
Set Animated .Gif as Background Android
Custom Filtering Arrayadapter in Listview
How to Get a Button's Height to Match Another Element's Height