This is my first post, sorry for the title and the explication. Sorry for my English.
I define an .xml with all I need. I have 10 ImageView and the id for 10 ImageView are myimage01, myimage02, ... , myimage010.
I need to select an image for an ImageView.
I can do it as follows:
String imageName=getImageName();
id = getResources().getIdentifier(imageName, "drawable", getPackageName());
drawable = res.getDrawable(id);
ImageView cant1= (ImageView)findViewById(R.id.myimage01);
cant1.setImageDrawable(drawable);
ImageView cant2= (ImageView)findViewById(R.id.myimage02);
cant2.setImageDrawable(drawable);
ImageView cant3= (ImageView)findViewById(R.id.myimage03);
cant3.setImageDrawable(drawable);
ImageView cant4= (ImageView)findViewById(R.id.myimage04);
cant4.setImageDrawable(drawable);
ImageView cant5= (ImageView)findViewById(R.id.myimage05);
cant5.setImageDrawable(drawable);
ImageView cant6= (ImageView)findViewById(R.id.myimage06);
cant6.setImageDrawable(drawable);
ImageView cant7= (ImageView)findViewById(R.id.myimage07);
cant7.setImageDrawable(drawable);
ImageView cant8= (ImageView)findViewById(R.id.myimage08);
cant8.setImageDrawable(drawable);
ImageView cant9= (ImageView)findViewById(R.id.myimage09);
cant9.setImageDrawable(drawable);
ImageView cant10= (ImageView)findViewById(R.id.myimage010);
cant10.setImageDrawable(drawable);
But this is too bad, its better to use a loop. But I don't know do this.
I need something like that:
String cad;
for(int i=0;i<10;i++){
cad="myimage0";
String cat= Integer.toString(i);
cad=cad.concat(cat);
ImageView cant1= (ImageView)findViewById(R.id.cad);
cant1.setImageDrawable(drawable);
}
But there is an error in:
ImageView cant1= (ImageView)findViewById(R.id.***cad***);
Thanks for all
You can't directly pass in a String like that to findViewById. You'll need to look up the resource from your String first. Try this:
int idResource = getResources().getIdentifier(cad, "id", getPackageName());
ImageView cant1= (ImageView)findViewById(idResource);
Use this in a loop:
getResources().getIdentifier(resName, "id", getPackageName());
Where resName is exact string for that id (in your example, it's myimage01, myimage02, ...
so we have:
String cad;
for(int i=0;i<10;i++){
cad="myimage0";
String cat= Integer.toString(i);
cad=cad.concat(cat);
int id = getResources().getIdentifier(cad, "id", getPackageName());
ImageView cant1= (ImageView)findViewById(id);
cant1.setImageDrawable(drawable);
}
Related
I have one ShowActivity with two Textview and one imageView.Texviews getting resources from string.xml and showing that on ShowActivity,I WANNA MAKE IMAGEVIEW LIKE THAT.
How it possible to imageview getting resources from drawble for each subjects_numbers?
This is my textview code :
TextView tv2 = (TextView) findViewById(R.id.textView2);
String stringName_2 = "subject_text_" + String.valueOf(Subject_number);
int resID_2 = getResources().getIdentifier(stringName_2, "string", getPackageName());
tv2.setText(resID_2);
I wanna code like this for my Imageview. I MADE it but it DOESN'T WORK..
ImageView img = (ImageView) findViewById(R.id.imgview_x);
String imageName = "image_"+ String.valueOf(Subject_number);
int resID = getResources().getIdentifier(imageName,"drawable",getPackageName());
img.setImageResource(resID);
CAN ANYONE HELP ME PLEASE
you should do like this:
int id = getResources().getIdentifier("yourpackagename:drawable/" + imageName, null, null);
imageview.setImageResource(id);
In case I have to set an image on a button, programmatically, then I use the following code:
Drawable img;
ToggleButton tb_button = (ToggleButton) findViewById(R.id.tglButton);
img = ResourcesCompat.getDrawable(getResources(), R.drawable.p189, null );
tb_button.setCompoundDrawables(img,null,null,null);
Now the situation is that I have read the name of the image from a database into a variable. Thus lets assume that my variable has the following value:
String img_str= "p189";
Now how do I set the same image on the button when the image name is stored inside a variable.
use following method to get image from string.
private int getImageFromString(String name) {
int resId = getResources().getIdentifier(name, "drawable", getPackageName());
return resId;
}
You can create drawable resource id reference dynamically as follows
int resId=getResources().getIdentifier(img_str, "drawable", context.getPackageName())
img = ResourcesCompat.getDrawable(getResources(), resId, null )
and of course don't forget about try catch in case of wrong resource name
You can get drawable as follows by itss name,
Resources resources = context.getResources();
final int resourceId = resources.getIdentifier(imgName, "drawable", context.getPackageName());
return resources.getDrawable(resourceId);
So elaborating on what ABK said. You can get the the Drawable ResId and then use that to get the drawable within a function like this:
private Drawable getDrawableFromString(String name) {
int resId = getResources().getIdentifier(name, "drawable", getPackageName());
return getResources().getDrawable(resourceId);
}
I am trying to create a routine that will assign a card image to an ImageView without using something like this: R.drawable.card_10clover. I would prefer to use a string, but the way I'm doing it doesn't work.
String imgRes = "R.drawable.card_";
int num = 10;
imgRes += "10";
String suit = "clover";
imgRes += "suit;
ImageView card = (ImageView) findViewById(R.id.imgcard);
card.setImageResource(imgRes);
Unfortunately setImageResource only takes an int. Please help.
Thank you,
Jack Blue
Use getIdentifier (String name, String defType, String defPackage);
Your imgRes does not need the prefix R.drawable
String imgRes = "card_";
int num = 10;
imgRes += "10";
String suit = "clover";
imgRes += "suit;
ImageView card = (ImageView) findViewById(R.id.imgcard);
int resourceId = getResource().getIdentifier (imgRes, "drawable", "com....");
card.setImageResource(resourceId);
Can someone please help with this. I am generating a random number and based on this random number I want to pick a png file from my res/drawable-mdpi folder to display. This is the code I'm using:
public void DisplayRandomPicture(int randomNumber)
{
String drawableName = "c"+ randomNumber;
ImageView image= (ImageView)findViewById(R.id.imageView1);
image.setImageDrawable(getResources().getDrawable(getResources().getIdentifier(drawableName, "res/drawable-mdpi", getPackageName())));
}
All the files in the res/drawable-mdpi folder start with "c". There is something wrong with the image.setImageDrawable syntax. When I run my app it crashes.
public void DisplayRandomPicture(int randomNumber)
{
String drawableName = "c"+ randomNumber;
ImageView iw= (ImageView)findViewById(R.id.imageView1);
resID = getResources().getIdentifier(drawableName, "drawable", getPackageName());
iw.setImageResource(resID);
}
Try above and make sure the image exists with the exact name.
I resolved this issue by using:
int id = getBaseContext().getResources().getIdentifier(drawableName, "drawable", getPackageName());
image.setImageResource(id);
I want to get the name of background image of an imageview, but my code is showing the id of the imageview.
ListView lst = list.get(0);
RelativeLayout view = (RelativeLayout) lst.getChildAt(i);
ImageView imgV = (ImageView) view.getChildAt(1);
String checkBoxBtn = imgV.getResources().getResourceEntryName(imgV.getId()).toString();
please help
The name isn't stored. You have to store it in a variable.