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"/>
Related
I'm new in android, so I still learning simple things. :)
I have images (*.jpg) in directory named "drawable-xhdpi" (only there). I can see the R.java file contain these images and set them a corresponding hex value.
I add my image view to the XML file:
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/a1" />
So, After setting this, I can see it in the begining of the emulator.
Now, I want to change it to be "a2.jpg" in code. I have the string path of an image file described above (for example: "a2"). therefore, I used:
ImageView (ImageView) findViewById(R.id.imageView1);
int resId = getResources().getIdentifier("a2", "drawable", getPackageName());
image.setImageResource(resId);
But it's fail. the image isn't replaced by the other image. and it doesn't matter if I wrote the above lines in the OnCreate method of the activity, or in some method which respond to android:OnClick.
I looked the previous answer in the forum, but still can't find - what have I missed?
Thanks for your help.
I looked around but didn't find an answer to my problem anywhere. I have an imageButton, and would like to change the image when the button is pressed. I'm trying to figure out a way to get Eclipse to interpret my code literally.
String card = nextcard.toString(); //Becomes "Ace_Of_Hearts", for example
theImageButton.setImageResource(R.drawable.card); //R.drawable.Ace_Of_Hearts is an image in the correct folder
Sadly this doesn't work. But because the card variable changes dynamically, I can't hard code a specific image to set the button to. So how should I set the image?
Get the id of all the cards in the drawable dir:
Integer ace_of_hearts_id = R.drawable.ace_of_hearts;
Integer ace_of_spades_id = R.drawable.ace_of_spades;
Integer queen_of_hearts_id = R.drawable.queen_of_hearts;
Integer ten_of_hearts_id = R.drawable.ten_of_hearts;
....
Put the ids and the string names in a HashMap as key value pairs:
HashMap cardMap = new HashMap<String,Integer>();
cardMap.put("ace_of_hearts",ace_of_hearts_id);
cardMap.put("ace_of_spades" ace_of_spades_id);
......
Then add your code:
String card = nextcard.toString(); //Becomes "Ace_Of_Hearts", for example
theImageButton.setImageResource(cardMap.get(card)); /
Havent actually compiled the code, so there might be errors, but this should give you a basic idea to go about.
You can use imagebutton selector for doing this.
First create selector in res/drawable btn_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<item android:drawable="#drawable/button_bg_selected" android:state_selected="true"></item>
<item android:drawable="#drawable/button_bg_normal"></item>
Now you can use this selctor as src of your image button.
<ImageButton
android:id="#+id/button1"
android:src="#drawable/btn_selector"
android:layout_width="200dp"
android:layout_height="126dp"/>
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 ...
I'm having this question about setting pictures for Buttons in XML :
I made some .png pictures I want to set as pictures instead of the buttons that I get in default when i create my android apps.. Now I loaded in the files into the drawable folder.. And how do I write in XML to get the right thing?
Is setting the button visible to false and adding a picture as background for it an option?
Because that's all the ways I thought about. Or is there a better way to do this kind of thing?
what I understood is that you want to set the picture in background. Do it like this in your Button tag:
android:background="#drawable/yourpicturename"
Add the png to the drawable folder that is under the res folder in your project (if you are using eclipse). Then in your XML file simply use android:background="#drawable/filename". Make sure to omit the file extension
You can use any of:
drawableTop, drawableLeft, drawableRight, drawableBottom
For example,
<Button android:drawableRight="#drawable/some_image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Some text" />
The image some_image.png will be positioned to the right of your text in this case.
i know how to set image/icon from our res/drawable
<ImageView android:id="#+id/ImageSearch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/android_green_3d"/>
If we set it directly from the code:
imageView1.setImageResource(R.drawable.android_green_3d);
But we can also get the icon from android.R.drawable like this:
imageView1.setImageResource(android.R.drawable.ic_menu_more);
How to set image/icon in xml id the source is android.R.drawable? Anyone know?
Thanks in advance.
Thats easy: change #drawable/ to #android:drawable/. Code completion didn't work in this case, so you should search in the GIT repository or online for the images and their names