How to get id of drawable of an imageview? - android

What I want to do is to get the decimal value of the image resource of an imageview on click.
Example is that a Toast should display "2130837504" which is the id of a drawable.

You can't do that. What you can do is this:
There is no getDrawableId function so you'll need to do something like
set a tag for the ImageView when you change its drawable. For
instance, set the drawable id as a tag for the ImageView so you could
just get the drawable id from the tag.
How to do that?
myImageView.setTag(R.drawable.currentImage); //When you change the drawable
int drawableId = (Integer)myImageView.getTag(); //When you fetch the drawable id
which is taken from here

Related

Setting an ImageView from the drawable folder dynamicly

Hello developers out there,
I have written a class called Item. Now I want this "Item" to get an image in the constructor from the drawable folder. I already tried to somehow get the resourceId from the images in the drawable folder, but didn't know how, because there is now such function as imageView.getResourceId() which isn't the best solution anyway because I would need to temporarly add all images from drawable to an imageView and then get the Id. Is there any way to solve this problem?
Thank you for your help
Solution:
Just use this code in java to set the drawable image to ImageView dynamically:
your_image_view_object.setImageDrawable(ContextCompat.getDrawable(Your_Activity.this, R.drawable.your_drawable_image));
For Example:
imageView.setImageDrawable(ContextCompat.getDrawable(MainActivity.this, R.drawable.ic_profilepic));
That's it. Hope it helps.
Try this,
int id = getResources().getIdentifier("yourpackagename:drawable/" + StringGenerated, null, null);
This will return the id of the drawable you want to access... then you can set the image in the imageview by doing the following
imageview.setImageResource(id);

Image Source Changing

I want to change an ImageView source from my code(not the xml). I have an activity and I want to change the image in it, according to a message I recieve from an other activity. Is there any method that can change the image source from the class code and not the xml code??
For example:
if (message.equals("hello")){
//change the ImageView to hello.png;
}
You can do this by following the below steps:
Save the images with message name(i.e. hello.png, etc).
Now in your if condition use below code to set the image. Change mipmap to drawable if you have saved the images in drawable folder
int imageId=context.getResources().getIdentifier(message,"mipmap",context.getPackageName());
imageView.setImageResource(imageId);

Get the resource id from a drawable

In xml:
<ImageButton
android:id="#+id/b_checkEv"
android:src="#drawable/img_huella"
... />
In activity:
ImageButton ib = (ImageButton) findViewById(R.id.b_checkEv);
Drawable dr = ib.getDrawble();
I change dinamically the image of the ImageButton in code with ib.setDrawable().
Anybody knows how could I get the resouce id from the Drawable in order to know which image is set in the ImageButton in each moment?
Thanks!
The best way to control Android Buttons are Tags.
You can do "ib.setTag("image1.png")" and manage image data here.
Then you can do "String imageName = ib.getTag()" and get the image name.
Regards

How to set background image for Gallery

I want to set the background of my Gallery by an image (say background.jpg ) in Drawable folder.
As the prototype of the setBackground() method is
void setBackground(Drawable background)
Set the background to a given Drawable, or remove the background.
But I don't know how to refer to my image in this method.
I have tried to refer like this
Gallery galleryModified;
galleryModified.setBackground(background);
But getting error in second line, saying unable to find resource.
Please reply if you know.
Thanks.
It seems, you are setting background from a resource from drawable folder, which have id, R.drawable.background, if it is the case, try following:
galleryModified.setBackgroundResource(R.drawable.background);
All resources in your project is parsed under R class automatically. You should always retrive them as an object, such as: R.drawable.drawable_name This is a basic way to retrieve many resource types. Just replace drawable keyword such as:
R.string.name_of_string_resource
R.id.id_control

Android Add imagine icon as a "hint" in a textview?

is it possible to add a image icon inside a textviews Hint?
at the moment i can only add text in the hint properties but what i want to do is add a small image icon and a text.
is this possible?
You will need to write your own View which could perhaps be based on a FrameLayout which contains both a TextView (with an empty hint) and an ImageView. Alternatively you could simply use a TextView and when it's empty you set a background image to your "hint icon", and clear the background when text has been entered.
You could user setError to display some text and and an image in the textfield but that would be after the user inputs something and you check it.
Alternatives:
Use setBackground with your hint icon on the background image
Subclass TextView and override the drawing to draw your hint icon
String hint = "<center><img src=\"" + R.drawable.search_hint + "\"/>搜索感兴趣的内容, hello world , I'm form China.</center>";
Html.fromHtml(hint, new ImageGetter()
{
#Override
public Drawable getDrawable(String source)
{
int img = Integer.parseInt(source);
Drawable drawable = getResources().getDrawable(img);
drawable.setBounds(0,0,drawable.getIntrinsicWidth(),drawable.getIntrinsicHeight());
return drawable;
}
}, null);
searchTxt.setHint(hint);

Categories

Resources