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.
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);
Update
i know i can use array or list for it but i want to use an efficient way for it with out using arrays or collection thanks to all of you
special thanks to mussharapp
hi i want to access Imageview using its id
for example i have an imageview which is created using loop like
for(int i=1;i<10;i++) {
Imageview mv=new ImageView(this);
mv.setid(i);
// and so on for image properties
}
now i want to get all images which i have created in loop how i can get these images is there any way using id or some thing else ???
the same problem i am facing
declare a private arraylist:
private ArrayList<Imageview> images = new ArrayList<Imageview>();
then store all your views into that list:
for(int i=1;i<10;i++) {
Imageview mv=new ImageView(this);
mv.setid(i);
// and so on for image properties
images.add(mv);
}
Finally, you can get them based on position/id (since position == id):
ImageView imageView = images.get(position);
Remember to clear list when done or onDestroy().
images.clear();
Given that you are sure that the image view id's will be unique you can do something like this
//Your code
for(int i=1;i<10;i++) {
Imageview mv=new ImageView(this);
mv.setid(i);
// and so on for image properties
}
//...
List<Drawable> drawables = new ArrayList<Drawable>();
for (int i = 1; i < 10; i++) {
ImageView mv = (ImageView) findViewById(i);
drawables.add(mv.getDrawable());
}
Not sure what you are trying to do is efficient. You are probably better off with the other suggestions they have mentioned.
First you have to set Array of images with its path and then you can use it...
You can keep an array of ImageViews.
ImageView[] mImageViews = new ImageView[10];
for(int i=1;i<10;i++) {
mImageViews[i] = new ImageView(this);
}
And access these like mImageViews[2] or mImageViews[5]
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.
I am trying to pass image from one activity to another activity using listview item, but I am getting this error line
the method setImageResource(int)
is the type Image View is not applicable for the argument(string) and at this point of line:-
lblImage.setImageResource(Image);
I don't know now what need to write to remove this error please someone support me and write the exact code to be fit on this line, here I am also placing some code for your reference:
SingleMenuItem(Activity to fetch Image with some text data)::
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.single_list_item);
// getting intent data
Intent in = getIntent();
// Get XML values from previous intent
String title = in.getStringExtra(KEY_TITLE);
String artist = in.getStringExtra(KEY_ARTIST);
String duration = in.getStringExtra(KEY_DURATION);
String Image = in.getStringExtra(KEY_THUMB_URL);
//Bitmap bitmap =(Bitmap) in.getParcelableExtra(KEY_THUMB_URL);
// ImageView image = (ImageView)findViewById(R.id.thumb_url);
// Displaying all values on the screen
TextView lblName = (TextView) findViewById(R.id.name_label);
TextView lblCost = (TextView) findViewById(R.id.email_label);
TextView lblDesc = (TextView) findViewById(R.id.mobile_label);
ImageView lblImage = (ImageView) findViewById(R.id.image_label);
//image.setImageBitmap(bitmap);
lblName.setText(title);
lblCost.setText(artist);
lblDesc.setText(duration);
lblImage.setImageResource(Image); //Getting Error at this line only
//the method setImageResource(int) is the type Image View is not
// applicable for the argument(string)
}
}
ActivityCode (to pass image and text data)
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
String title = ((TextView) view.findViewById
(R.id.title)).getText().toString();
String artist = ((TextView) view.findViewById(
R.id.artist)).getText().toString();
String duration = ((TextView) view.findViewById
(R.id.duration)).getText().toString();
// byte[] array = null;
// Bitmap thumb_url = BitmapFactory.decodeByteArray
(array, 0, array.length);
String Image=((ImageView)view.findViewById
(R.id.list_image)).getImageMatrix().toString();
// Starting new intent
Intent in = new Intent
(getApplicationContext(), SingleMenuItemActivity.class);
in.putExtra(KEY_TITLE, title);
in.putExtra(KEY_ARTIST, artist);
in.putExtra(KEY_DURATION, duration);
in.putExtra(KEY_THUMB_URL, Image);
//in.putExtra(KEY_THUMB_URL, thumb_url);
startActivity(in);
}
You're passing a String to setImageResource(), which requires an integer.
Resource IDs are actually integers - when you refer in code to R.drawable.myImage this is referring to an integer resource in automatically generated code (R.java) which is created by the compiler.
To load an image from a resource string (not recommended) see Android - Open resource from #drawable String
This will work for image id not for image string
if you want show image in the imageview do it like this:
lblImage.setImageResource(R.drawable.image);
OR
according to your requirement you have to get Drawable from the imag link. by using this code you can get image drawable and then set to image view
public static Drawable LoadImageFromWebOperations(String url) {
try {
InputStream is = (InputStream) new URL(url).getContent();
Drawable d = Drawable.createFromStream(is, "src name");
return d;
} catch (Exception e) {
return null;
}
}
call this function and the set drawable to image view like this.
Drawable image = LoadImageFromWebOperations(Image); // Image means iamge url getting
from previous activity
imageview.setImageDrawable(image);
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);
}