With swift, you can specify the image by the image name and paste it, but by the way, it was not android (Is it there?)
I couldn't find it, or I didn't really look for it, but I made it somehow.
The procedure is ① Get the resource ID from the image name ② Set the image using the resource ID Only this.
Util.java
public void setBackgroundResourceByString(Context context, String imgName, View view){
int resourceId = context.getResources().getIdentifier(imgName, "drawable", context.getPackageName());
view.setBackgroundResource(resourceId);
}
Substitute the context obtained by getContext () etc. as the first argument, The second argument is to substitute the image name put in drawable as a string type, Substitute the imageView or View with the image pasted as the third argument.
I feel a little sick
Util.java
public int getResourceIdByString(Context context, String imgName){
return context.getResources().getIdentifier(imgName, "drawable", context.getPackageName());
}
A method like this may be safe
By the way, maybe drawable can also get the resource ID of colors and characters registered as color and string.
Recommended Posts