How Set Background Drawable Programmatically in Android - Stack Overflow
How Set Background Drawable Programmatically in Android - Stack Overflow
Stack Overflow
log in
94
android
background
drawable
out-of-memory
To set Background:
RelativeLayout layout =(RelativeLayout)findViewById(R.id.background);
layout.setBackgroundResource(R.drawable.ready);
share
Asked
Sep 21 '12 at 1:05
Edited
Jul 3 '15 at 15:57
6 Answers
164
layout.setBackgroundResource(R.drawable.ready); is correct.
Order By
Votes
But I think the problem occur because you are trying to load big images.
Here is a good tutorial how to load large bitmaps.
UPDATE:
getDrawable(int ) deprecated in API level 22
getDrawable(int ) is now deprecated in API level 22. You should use the following code from the support library instead:
ContextCompat.getDrawable(context, R.drawable.ready)
If you refer to the source code of ContextCompat.getDrawable, it gives you something like this:
/**
* Return a drawable object associated with a particular resource ID.
* <p>
* Starting in {@link android.os.Build.VERSION_CODES#LOLLIPOP}, the returned
* drawable will be styled for the specified Context's theme.
*
* @param id The desired resource identifier, as generated by the aapt tool.
*
This integer encodes the package, type, and resource entry.
*
The value 0 is an invalid identifier.
* @return Drawable An object that can be used to draw this resource.
*/
public static final Drawable getDrawable(Context context, int id) {
final int version = Build.VERSION.SDK_INT;
if (version >= 21) {
return ContextCompatApi21.getDrawable(context, id);
} else {
return context.getResources().getDrawable(id);
}
}
share
Answered
Sep 21 '12 at 1:23
Edited
Aug 6 '15 at 5:23
Hey, I am trying to do a task only if the background image of an image button is a certain drawable resource. How can I compare... I've tried
if(buttonBackground.equals(R.drawable.myDrawable)) where Drawable buttonBackground = myButton.getBackground();I get this error: snag.gy/weYgA.jpg Ruchir
Baronia Nov 28 '15 at 21:53
add a comment
64
Try this:
layout.setBackground(getResources().getDrawable(R.drawable.ready));
share
Answered
Sep 21 '12 at 1:09
Edited
Apr 11 '14 at 4:20
but this is the same thing Ahmad :) MoshErsan Sep 21 '12 at 1:13
ah ok, then I would refere to Lazy Ninjas answer. Ahmad Sep 21 '12 at 1:30
27
You do not need getResources().getDrawable(). The correct code is layout.setBackgroundResource(R.drawable.ready); just like the OP used. The issue here comes
from the size of the bitmap. BVB Aug 13 '13 at 17:35
setBackground is API level 16 or above only. Erwan Feb 6 '14 at 1:02
add a comment
share
share
Answered
Dec 8 '13 at 9:36
If your backgrounds are in the drawable folder right now try moving the images from drawable to drawable-nodpi folder in your project. This
worked for me, seems that else the images are rescaled by them self..
Answered
Apr 17 '14 at 12:44
Well if you haven't got a copy of the images you need to use in the project in HD quality, why let android rescale them to crappy quality by using the normal drawable folder. And
even do the question is old, if it still pops up in Google than posting something new in it is ok imho. Jordy Apr 17 '14 at 13:29
add a comment
View v;
Drawable image=(Drawable)getResources().getDrawable(R.drawable.img);
(ImageView)v.setBackground(image);
share
Answered
Jul 28 '15 at 13:11
I'm using a minSdkVersion 16 and targetSdkVersion 23 The following is working for me, it uses ContextCompat.getDrawable(context,
R.drawable.drawable);
Instead of using:
layout.setBackgroundResource(R.drawable.ready);
Rather use:
layout.setBackground(ContextCompat.getDrawable(this, R.drawable.ready));
getActivity() is used in a fragment, if calling from a activity use this
share
Your Answer
Answered
Mar 30 at 10:36
log in
or
Name
By posting your answer, you agree to the privacy policy and terms of service.
meta chat tour help blog privacy policy legal contact us full site
Download the Stack Exchange Android app
2016 Stack Exchange, Inc