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.
Related
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"/>
I have been reading some post about changing an image in an ImageView.
But everyone asks for setting an ImageView with a custom image, and I want to set a system image inside it.
I made this:
ledicon.setImageDrawable(getResources().getDrawable(R.drawable.presence_busy));
But Eclipse tells me that presence_busy is not recognized.
Why?
I am using presence_online in the XML file,
android:src="#android:drawable/presence_online"
and all works perfectly.
Try this:
ledicon.setImageDrawable(getResources().getDrawable(android.R.drawable.presence_busy));
(When accessing system resouces you have to look them um in android.R not your own.)
I have an ImageView define in XML like as follows:
<ImageView
android:id="#+id/background"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"/>
I'm attempting to set the image contents dynamically in code, based on some user action (they select an image from their library). I set the image using the following code:
ImageView bgView = (ImageView)root.findViewById(R.id.background);
Drawable d = Drawable.createFromPath(wallpaper);
bgView.setImageDrawable(d);
Calling this code has no immediate effect on the ImageView instance. However, if I put some sort of image into the XML definition, something like:
<ImageView
android:id="#+id/background"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="#drawable/test_image"/>
It works fine. Does anyone know what the cause of the issue might be? I've verified that the image setting code is running on the UI thread, and that the Drawable created is valid. The problem feels like it might be related to the layout not being performed correctly to reflect the added image?
Try:
ImageView bgView = (ImageView)root.findViewById(R.id.background);
Drawable d = Drawable.createFromPath(wallpaper);
// check d != null
d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
bgView.setImageDrawable(d);
So I figured out what the issue was, after a bit more digging. In the case where an empty image is being set (that is, the default state when no custom background is found in the user settings), my code does .setImageDrawable(null) when the view is loaded. Removing this seemed to fix the issue.
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