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
Related
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);
I'm new to Android programming and I want to know that how can I get the name of the image resource that is currently set on the image button.
eg : b1.setImageResource(R.drawable.img);
//where b1 is the image button and img is the name of the png file present in res/drawable.
Now I would like to know how would I get the name of the image file i.e "img".
Thanks in advance.
You can use setTag() and getTag() this way you can store any data you want.
imageView.setTag("imageNameHere");
String imageName = (String) imageView.getTag();
Let me know! ;)
I am guessing that you are simply looking to add an image as a background to your button.
To do this, add your chosen image to your res/drawable folder (there are many ways to do this). You can rename it if you like, but I recommend lowercase with underscores ( _ ), like "img_1", or just "img" as you have said.
Then, in your XML view, add the android:background line to your button...
<Button
android:id="#+id/b1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/img"/>
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
I have added an image resource for a button in an activity but the Android button object appears bigger than the image resource as shown here: https://plus.google.com/112628117356947034778/posts/AH4B1pvbTeM
As you can see the grey background is the button object appearing from behind the image resource.
I want the button image to overlay the button but when I try to re size it,it stays the same dimension.Does anyone have a solution to this problem? Thanks
Use:
android:layout_width="200dp"
android:layout_height="50dp"
android:background="#drawable/image_name"
// Use fixed value Or Put the all type of image with fix size in drawable-hdpi, mdpi,xhdpi, ldpi
If you will use ImageButton instead of button image background was overlay
Use ImageButton as the element, and also set the attribute android:scaleType="fitXY"
I'm using button in layout.xml as following
<Button
android:id="#+id/karaza"
android:layout_width="160dp"
android:layout_height="160dp"
android:background="#drawable/button_green"
android:text="0"
android:textColor="#FFFFFF"
android:textSize="45dp" />
The background image name is "button_green"
how can I get it in the code of activity as String?
Thanks in advance
Try this:
I donot think there is any direct way to get name of drawable image unless you stores the image name along with image.
In XML you can do the following to set image name as an additional data to imageview
android:tag="btful.png"
Programmetically setting imagename
ImageView img = (ImageView) findViewByID(R.id.img)// my image
img.settag("btful.png"); // to set an image name
you can get the image name by using getTag()
String imageName = (String)img.getTag();
Try this,
Btn.setBackgroundResource(R.drawable.button_green);
Just Findviewbyid of button in java file then use:
btn.getBackground();
it'll work!!
and assign it to a drawable ...