[Solved] setBackgroundDrawable() deprecated

So my sdk goes from 15 to 21 and when I call setBackgroundDrawable(), Android Studio tells me that it’s deprecated.

I thought of going around it using:

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

if(sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
} else {

But then, I get an error at “setBackground()”.

So, how would you deal with it?

Enquirer: Makoto


Solution #1:

It’s an interesting topic. The way you are doing it is correct, apparently. It is actually just a naming decision change. As this answer points out, setBackground() just calls setBackgroundDrawable():

public void setBackground(Drawable background) {
    //noinspection deprecation

public void setBackgroundDrawable(Drawable background) { ... }

You can see this thread for more information about all of this.

Respondent: Alex K

Solution #2:

maybe you can try the following:

Respondent: hedgehog

Solution #3:

It’s funny because that method is deprecated, but if you look at the Android Source Code you’ll find this:

     * Set the background to a given Drawable, or remove the background. If the
     * background has padding, this View's padding is set to the background's
     * padding. However, when a background is removed, this View's padding isn't
     * touched. If setting the padding is desired, please use
     * {@link #setPadding(int, int, int, int)}.
     * @param background The Drawable to use as the background, or null to remove the
     *        background
    public void setBackground(Drawable background) {
        //noinspection deprecation
Respondent: Joaquin Iurchuk

Solution #4:

Correct as of 15th August 2018

Use the support libraries

Drawable drawable = ResourcesCompat.getDrawable(getResources(), drawableRes, null);
ViewCompat.setBackground(layout, drawable);
Respondent: Stuart Campbell

Solution #5:

You are getting an error because getResources().getDrawable() takes an id (int) not a drawable as its argument. Try this:


Respondent: David C Adams

Solution #6:

Use this:

myView.background = ContextCompat.getDrawable(context,
Respondent: Malwinder Singh

Solution #7:


view.background = ActivityCompat.getDrawable(context,
Respondent: Eliasz Kubala

Solution #8:

This is correct in my case
Solve this problem

Respondent: Sonu Kumar

Solution #9:

Correct as of 23th November 2018


view.background = resources.getDrawable(R.drawable.ic_image,theme)

If you include the Theme parameter.

Respondent: Dimitri de Jesus

Solution #10:

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.setBackground(getResources().getDrawable(R.drawable.img_wstat_tstorm));

Rather use:

layout.setBackground(ContextCompat.getDrawable(getActivity(), R.drawable.img_wstat_tstorm));

getActivity() is used in a fragment, if calling from a activity use this

Respondent: Vostro

Solution #11:

BitmapDrawable background = new BitmapDrawable(BitmapFactory.decodeResource(getResources(), R.mipmap.Nome_imgem));
Respondent: Vando Sep

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Most Popular

To Top
India and Pakistan’s steroid-soaked rhetoric over Kashmir will come back to haunt them both clenbuterol australia bossier man pleads guilty for leadership role in anabolic steriod distribution conspiracy