Sie sind auf Seite 1von 6

sign up

Stack Overflow

log in

Questions Tags Users Badges Unanswered Ask

Read this post in our app!

94

How set background drawable programmatically in Android


java

android

background

drawable

out-of-memory

To set Background:
RelativeLayout layout =(RelativeLayout)findViewById(R.id.background);
layout.setBackgroundResource(R.drawable.ready);

Is the best way to do it?

share

improve this question


Chad Bingham
6,271 9 39 75

Asked
Sep 21 '12 at 1:05

Edited
Jul 3 '15 at 15:57

6 Answers

164

layout.setBackgroundResource(R.drawable.ready); is correct.

Another way to achieve it is to use the following:

Order By

Votes

final int sdk = android.os.Build.VERSION.SDK_INT;


if(sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
layout.setBackgroundDrawable( getResources().getDrawable(R.drawable.ready) );
} else {
layout.setBackground( getResources().getDrawable(R.drawable.ready));
}

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);
}
}

More details on ContextCompat


As of API 22, you should use the getDrawable(int, Theme) method instead of getDrawable(int).

share

improve this answer


Lazy Ninja
11.6k 6 35 66

Answered
Sep 21 '12 at 1:23

Edited
Aug 6 '15 at 5:23

'getDrawable(int)' is deprecated. S.M_Emamian Aug 5 '15 at 8:29

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));

and for API 16<:


layout.setBackgroundDrawable(getResources().getDrawable(R.drawable.ready));

share

improve this answer


Ahmad
33.4k 12 73 102

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

RelativeLayout relativeLayout; //declare this globally

now, inside any function like onCreate, onResume


relativeLayout = new RelativeLayout(this);
relativeLayout.setBackgroundResource(R.drawable.view); //or whatever your image is
setContentView(relativeLayout); //you might be forgetting this

share

improve this answer


Sujay Kumar
109 2 11

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

improve this answer


Jordy
839 11 16

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

You can also set the background of any Image:

View v;
Drawable image=(Drawable)getResources().getDrawable(R.drawable.img);
(ImageView)v.setBackground(image);

share

improve this answer


Bhaskar Kumar Singh
36 5

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

improve this answer


Vostro
11 1

Your Answer

Answered
Mar 30 at 10:36

log in
or

Name

Email

By posting your answer, you agree to the privacy policy and terms of service.

Post Your Answer

meta chat tour help blog privacy policy legal contact us full site
Download the Stack Exchange Android app
2016 Stack Exchange, Inc