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
Related
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 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.)
Hello I have a drawable myshape.xml, it contains a <shape> and I cannot set an android:id to shapes.
In my code I want to set the background of a view to this file using
catAll.setBackgroundDrawable(getResources().getDrawable(R.id......???));
where myshape.xml does not show up in my R file because it has no id. and I cannot set id to object.
In my XML I do set the shape by simply typing the drawable resource name. But I need to do this programmatically.
You don't need to get the drawable yourself. Use this instead:
catAll.setBackgroundResource(R.drawable.myshape);
For future reference, if you do wish to get the drawable keep in mind that drawables live in the R.drawable namespace. So your code would became:
getResources().getDrawable(R.drawable.myshape);
This is akin to what you do in your XML:
#drawable/myshape
instead of
#id/myshape
The question is really old but googles first hit references to this thread.
So getDrawable(id) is deprecated.
Short solution (kotlin)
yourView.background = ContextCompat.getDrawable(context, R.drawable.your_ressource_id)
For more, please read this: https://stackoverflow.com/a/29146895/4420355
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/