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);
Related
If an image is already defined in Xml and when I run the app, it pops of on the screen of the emulator without any problem, what's the purpose of setImageResource() method in Java code ?
for any dynamically programming you might need to change the content of an ImageView at runTime and for this according to your resource that might comes from web service you can do it with
setImageResource(int resId)
setImageDrawable(android.graphics.drawable.Drawable)
setImageBitmap(android.graphics.Bitmap)
We can set an image resource programmatically into ImageView by using the setImageResource() method:
Example:
ImageView img = (ImageView)findViewById(R.id.img);
// supossing to have an image called ic_play inside my drawables.
myImageView.setImageResource(R.drawable.ic_play);
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);
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
I am facing a situation in which-
1.First I have to set the background image of a ImageButton to a default image.
2.Now at runtime based on some condition I have to change the background image of that ImageButton to some other image.
I have tried this but it doesn't work and the background image remains to default image and
doesn't change.
btn.invalidateDrawable((Drawable)getResources().getDrawable(R.drawable.info));
btn.setBackgroundDrawable((Drawable)getResources().getDrawable(R.drawable.infonewred));
How can I achieve this goal.
Have you tried btn.setBackgroundResource(R.drawable.infonewred).
I suggest you set the "default" Background directly in your XML-Layout-File via android:background.
If you have to change the Background in Runtime just use btn.setBackgroundResource(R.drawable.imagename);
since you haven't provided xml, i am guessing you are providing src to imageButton.. so to change the source of the image , what you are doing is changing the image background which will not be visible since it is the source which will be visible to you.. so to change source..
do this.
btn.setImageDrawable()
rather than..
btn.setBackgroundDrawable()
I know that's old but i got the solution as i'm also was searching for that, the correct answer is to use setImageResource , so the code will be like that:
btn.invalidateDrawable(getResources().getDrawable(R.drawable.info));
btn.setImageResource(R.drawable.infonewred);
Try the method setImageResource
Ex:
if(someCondition()){
btn.setImageResource(R.drawable.info);
} else {
btn.setImageResource(R.drawable.infonewred);
}
Just try following code to change at runtime and for default view set background property in layout xml:
if(yourCondition)
btn.setBackgroundResource(R.drawable.infonewred);
in activity file you can write
btn.setBackgroundResource(R.drawable.sliderr);
OR
in your xml layout file you can add one extra line to you button xml code
android:src="#drawable/sliderr"
I am specifically looking for the drawable android.R.id.empty.
Does any one have an idea what the source path of that default Android drawable is?
The file where it is defined is located in
YOUR_SKD_DIRECTORY/platforms/A_PLATFORM/data/res/values/ids.xml
It is not a drawable but an id as you can clearly see from android.R.id.empty.
This id is meant to be set on a View which should be displayed in case your ListView is empty.
I stumpled upon a really nice guide for Froyo drawables. And don't forget to use android.R.drawables for your integer id. http://androiddrawableexplorer.appspot.com/