Setting Android images from string value
There is a method for doing that, you can retreive resource IDs by string using Resources.getIdentifier() http://developer.android.com/reference/android/content/res/Resources.html
Something like:
int resourceId = Activity.getResources().getIdentifier("testimage", "drawable", "your.package.name");
How to set an imageView's image from a string?
if you have the image in the drawable folder you are going about this the wrong way.
try something like this
Resources res = getResources();
String mDrawableName = "logo_default";
int resID = res.getIdentifier(mDrawableName , "drawable", getPackageName());
Drawable drawable = res.getDrawable(resID );
icon.setImageDrawable(drawable );
How to Convert String A into Image in android?
Use textview to show the first letter and add a background color to it. Use Random class to get a random color as background each time.
private String senderFirstLetter;
private TextView senderProfilePic;
// To retrieve first letter of the sender,
senderFirstLetter = (String) holder.sender.getText().subSequence(0, 1);
holder.senderProfilePic.setText(senderFirstLetter);
// To get random color
GradientDrawable drawable = (GradientDrawable) holder.senderProfilePic.getBackground();
Random randomBackgroundColor = new Random();
int color = Color.argb(255, randomBackgroundColor.nextInt(256), randomBackgroundColor.nextInt(256), randomBackgroundColor.nextInt(256));
drawable.setColor(color);
android change background image programmatically from string
Keep your images name inside drawables same as of in String.xml.
Say for eg: images.png it should be images in String.xml
If you pass this name to getResourceId() it will returns you Drawable with that String Variable.
public static int getResourceId(Context context, String name, String resourceType) {
return context.getResources().getIdentifier(name, resourceType, context.getPackageName());
}
int iconId = getResourceId(Activity.this, image, "drawable");
ImageView categoryIcon = (ImageView) v.findViewById(R.id.category_icon);
categoryIcon.setImageResource(iconId);
Android: How to set the ImageView src from string?
I think your approach is a bad idea.
You can localize a drawable too. I think this is the better approach for changing images when localizing your app.
Related Topics
What Is the Simplest Way to Reverse an Arraylist
Return Data from Asynctask Class
Why Do We Have to Call Super in Android Sometimes
Java: Need Some Way to Shorten This Code
Translating Ruby Encryption Code to Java
Is Iterating Concurrenthashmap Values Thread Safe
Transformexception Duplicate Entry for Common.Annotations.Beta
How to Activate "Share" Button in Android App
Reducing Android App (Apk) Size
Android - How to Change Fragments in the Navigation Drawer