android get button background imageName programmatically - android

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 ...

Related

Get the name of the image resource used in image button

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"/>

Replace image with other image

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.

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 drop image under the text within TextView

How can i drop an image under a piece of text in a TextView.
For example i want to drop image of bread under the text of bread in my TextView. I really want to show a drawable instead of background color for a part of text in a TextView. Is it possible with ImageSpan?
you can just simply add android:drawablebottom="#drawable/yourimage" to the TextView in xml
or otherwise the equivalent in Java is
public void setCompoundDrawablesWithIntrinsicBounds (Drawable left, Drawable top, Drawable right, Drawable bottom)
usage:
myText.setCompoundDrawablesWithIntrinsicBounds(null,null,null, myNewDrawable);
Just add android:drawablebottom="#drawable/bread" in your textview
If you want it like a watermark, use ImageButton as follows, make sure you use a small icon-like image
<ImageButton
android:layout_width="140dp"
android:layout_height="wrap_content"
android:src="#drawable/bread" />
Write below code and try
In your XML File
<Button
android:layout_width="140dp"
android:layout_height="wrap_content"
android:src="Bread" android:id="#+id/mBtn1" />
In your Java File.
Button mBtn1 = (Button) findViewById(R.id.mBtn1);
Drawable d = getResources().getDrawable(R.drawable.bread);
mBtn1.setCompoundDrawablesWithIntrinsicBounds(null, null, null, d);
it will solve your problem.

Set image from android.R.drawable in image view android

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

Categories

Resources