content-driven ImageView - android

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);

Related

how to add the three values of textview and to store in one texview

String s1 = (getIntent().getStringExtra("Value1"));
String s3 = (getIntent().getStringExtra("value2"));
String s2 = (getIntent().getStringExtra("value3"));
id1 = (TextView) findViewById(R.id.id1);
id2 = (TextView) findViewById(R.id.id2);
id3 = (TextView) findViewById(R.id.id3);
id4 = (TextView) findViewById(R.id.id4);
id1.setText((s1));
id2.setText((s2));
id3.setText((s3));
How to add the values of the tex view set text values and to store the value as integer in one text view
id1.setText((s1));
id2.setText((s2));
id3.setText((s3));
then your question, add the three values of textview and to store in one texview
int first = Integer.parseInt(id1.getText().toString());
int second = Integer.parseInt(id2.getText().toString());
int third = Integer.parseInt(id3.getText().toString());
int final = first+second+third;
id4.setText(""+final);

How to get Image

I am new in Android and have very small issue, i want to know what code i need to use to get image.
like if we use this to get text :
name = txtname.getText().toString();
So what we use to get Image ?
CODE:
public static final String TAG_NAME = "name";
public static final String TAG_IMAGEURL = "imageurl"
String name, image ;
Intent in = getIntent();
String mname = in.getStringExtra(TAG_NAME);
String mimage = in.getStringExtra(TAG_IMAGEURL);
ImageLoader imageLoader = new ImageLoader(getApplicationContext());
final ImageView imgv = (ImageView) findViewById(R.id.image);
final TextView txtn = (TextView) findViewById(R.id.name);
txtn.setText(mname);
imageLoader.DisplayImage(mimage, imgv);
name = txttitle.getText().toString();
image = // here i want to know what i should need to use to get image
// like i am getting text using :::txttitle.getText().toString();
ImageView.getDrawable() will return the Drawable set in an ImageView
if you've used setImageDrawable() to set image to the ImageView then you can use imgv.getDrawable() method to get the image.

How to assign Image Resource to ImageView with String in Android

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);

Select the resource for findViewById(R.id.xxx) in code

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);
}

how to get name of drawable of an imageview

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.

Categories

Resources