I am working on Application in which I want to access the id of image from R.java which is used by imageView.
for examle
imageView i;
i=(ImageView)findViewbyid(imageview1);
i.setImageResource(R.drawble.human);
.....
now i want to know which id is used by i in running application.
//when you place an image in you drawable it will generate an Id in R.java file
when you want that particular image to display in your ImageView
you need to declare like this
public static Integer[] images = new Integer[]{R.drawable.my_love,R.drawable.first,R.drawable.second,R.drawable.third};
And then set the image resource like this,
ImageView i = new ImageView(this.context);
i.setImageResource(this.images[1]);
//you have declared as
imageView i=(ImageView)findViewbyid(imageview1);
//what is this imageview1?
instead of that you need to give your xml declaration ImageView id as
imageView i=(ImageView)findViewbyid(R.id.imageview);
If you are looking for a method to get the Image Background of a ImageView this will help you.
ImageView imageView=(ImageView)findViewById(R.id.image);
imageView.setBackgroundResource(R.drawable.ic_launcher);
imageView.setDrawingCacheEnabled(true);
Drawable d=imageView.getBackground();
This will give you the id of the image in drawable
String mDrawableName =Integer.toString( R.drawable.human);
int resID = getResources().getIdentifier(mDrawableName , "drawable", getPackageName());
Related
How can I get the source id (id.drawable.blablabla) of an ImageView that I created dynamically (like this):
ImageView image = new ImageView(this);
image.setImageResource(imageId);
image.setLayoutParams(new LinearLayout.LayoutParams(size, size));
layout.addView(image);
I wanna use getChildAt method on my layout if it is possible. I just need that image else where and I don't have control what image it actually is becouse they creation is based on rand. Index of that element is enough for me
ANSWER
I jsut find cool solution! I just add image.setTag(imageId) line of code to my method and then when I need rolled image I can simply use id under tag, thanks for help :)
I get dynamic drawable like this:
int id = getResources().getIdentifier("action" + String.valueOf(randomint), "drawable", getPackageName());
Drawable d = getResources().getDrawable(id);
And then add them to an imageview. Which you then add to a Linear layout.
Does this solve your problem?
ImageView image = new ImageView(this);
image.setId(1 + 28000); // Id in integer suppose we use this value where 1 is dynamic value which you wants to add in your imageview.
image.setImageResource(imageId);
image.setLayoutParams(new LinearLayout.LayoutParams(size, size));
layout.addView(image);
// Now here is code to get its id which you want in integer :
image.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
System.out.println("your id" + (arg0.getId() - 28000));
}
});
You can get the drawable like
Drawable myDrawable = iv.getDrawable();
You can compare it with a drawable resource like
if(iv.getDrawable()==getResources().getDrawable(R.drawable.image1)){
//do work here
}
credit https://stackoverflow.com/a/20765566/4211264
i'd like to get filename of shuffled ImageViews
private List<Drawable> images_;
images_ = new ArrayList<Drawable>();
images_.clear();
images_.add(getResources().getDrawable(R.drawable.img1));
images_.add(getResources().getDrawable(R.drawable.img2));
images_.add(getResources().getDrawable(R.drawable.img3));
images_.add(getResources().getDrawable(R.drawable.img4));
Collections.shuffle(images_);
ImageView img_1 = (ImageView)findViewById(R.id.img_1);
img_1.setBackgroundDrawable(images_.get(0));
How to know what image name was setted in this ImageView (img_1) ?
Use setImageResource(resID) instead of setBackgroundDrawable. Now you can get the id of the image as the same resID using the setId(resID) Method. To get the image name use the following method.
getResources().getResourceName(urImage.getId());
ArrayList<Integer> list = new ArrayList<Integer>() {{
add(R.drawable.img1);
add(R.drawable.img2);
add(R.drawable.img3);
}}
Collections.shuffle(list);
ImageView img_1 = (ImageView)findViewById(R.id.urimageId);
img_1.setImageResource(list.get(0));
img_1.setTag(list.get(0));
Now to get the image name do the following.
getResources().getResourceName((Integer)img_1.getTag());
According to the Android Developers website the ImageView is inherited from android.view.View. So you can use getResources() to get a Resources object and then use getAssets() to get the AssetManager.
I'm at work so I can't provide example right now. Sorry. I hope this helps though.
Happy coding! :)
Try this to get file name from an ImageView:
ImageView iv = new ImageView(MainActivity.this);
String path = f.getAbsolutePath();
iv.setImageBitmap(BitmapFactory.decodeFile(path));
iv.setTag(path);
Retrieving the path:
String path = (String) iv.getTag();
My Question is how to change image id from drawable folder in setImageResource() in android.
My drawable folder contains icon0.png to icon9.png and i want to change these images in image view dynamically using this
ImageView iV3;
iV3 = (ImageView) findViewById(R.id.imageView3);
iV3.setImageResource(R.drawable.icon + speed_Arr[2]);
speed_Arr[2] contains any value from 0 - 9.
But this didnt change images.
Plz help me out.
regards.
public static int getIdentifier(Context context, String name)
{
return context.getResources().getIdentifier(name.substring(0, name.lastIndexOf(".")), "drawable", context.getPackageName());
}
Above code will return the resource id from name String.
int res = getResources().getIdentifier("< packageName:drawable/imageName >'", null, null);
Use this res in your iV3.
say I want to dynamically load an image file in R.drawable.* based on the value of a string
Is there a way to do this? It seems like I need to statically refer to anything in R.
Have you declared the id for the image in XML file? If you did, you can use the following method:
Let's say you have picture.png in your res/drawable folder.
In your activity, you can set your image resource in the main.xml file
<ImageView android:id="#+id/imageId" android:src="#drawable/picture"></ImageView>
In FirstActivity
//to retrieve image using id set in xml.
String imageString = "imageId"
int resID = getResources().getIdentifier(imageString , "id", "package.name");
ImageView image = (ImageView) findViewById(resID);
imageString is the dynamic name. After which you can get the identifier of the dynamic resource.
Another method, you can do this:
//to retrieve image in res/drawable and set image in ImageView
String imageName = "picture"
int resID = getResources().getIdentifier(imageName, "drawable", "package.name");
ImageView image;
image.setImageResource(resID );
You will be able to reference your image resource and set your ImageView to it.
int drawableId = getResources().getIdentifier(drawablename, "drawable", getPackageName());
imageview.setImageResource(drawableId);
Try this. This should work.
Class res = R.string.class;
Field field = res.getField("x" + pos);
headerId = field.getInt(null);
header.setText(headerId);
this works with drawables as well, just edit the string. the header part is not required, it's just an example pulled from something I wrote a while ago.
I am trying to make images dynamic in my android application. With the help of other posts i wrote this code but i am unable to set image resource. Here is my code.
// get image whose source i want to change..
ImageView IV = (ImageView) findViewById(R.id.imageView1);
// x for 1,2,3.. hangman1.png,hangman2.png and so on image are located
// under res/drawable-mdpi
int j = getResources().getIdentifier("hangman"+x, "imageView", getPackageName());
// here i get errork, The method setImageResource(int) in the type ImageView
// is not applicable for the arguments (Drawable)
IV.setImageResource( getResources().getDrawable(j) );
int j = getResources().getIdentifier("hangman"+x, "drawable", getPackageName());
try this, if you are trying to get a drawable. and also as kcoppock said , just use j.
Because you're trying to pass in a Drawable. getDrawable(), as its name states, returns a Drawable object. setImageResource takes a resource ID, which is what getIdentifier will return. You should just pass j to setImageResource().
Edit: Heh, and you also should use Yashwanth's answer, as he said. :)
(TEAMWORK!)
combining both above solutions did the trick for me..here is code..
dnt know which answer i should accept..
ImageView IV = (ImageView) findViewById(R.id.imageView1);
int j = getResources().getIdentifier("hangman"+x, "drawable", getPackageName());
IV.setImageResource( j );
from your xml file , use app:src instead of android:background, and from the activity file, use :
setImageDrawable(getRessource().getDrawable(R.drawable.yourRessource));