97

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) {
    layout.setBackgroundDrawable(getResources().getDrawable(R.drawable.img_wstat_tstorm));
} else {
    layout.setBackground(getResources().getDrawable(R.drawable.img_wstat_tstorm));
}

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

So, how would you deal with it?

MysticMagicϡ
  • 28,305
  • 16
  • 71
  • 119
Makoto
  • 1,311
  • 3
  • 12
  • 16

11 Answers11

114

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
    setBackgroundDrawable(background);
}

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

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

Community
  • 1
  • 1
Alex K
  • 8,020
  • 9
  • 37
  • 55
  • 20
    You should note that `setBackground()` won't work for pre API16, an alternative could be `setBackgroundResource` – Mood Aug 04 '15 at 09:27
30

maybe you can try the following:

setBackgroundResource(R.drawable.img_wstat_tstorm);
Hariprasad
  • 3,466
  • 2
  • 22
  • 39
hedgehog
  • 629
  • 7
  • 8
18

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
        setBackgroundDrawable(background);
    }
Joaquin Iurchuk
  • 5,263
  • 1
  • 45
  • 63
15

Correct as of 15th August 2018

Use the support libraries

Drawable drawable = ResourcesCompat.getDrawable(getResources(), drawableRes, null);
ViewCompat.setBackground(layout, drawable);
Stuart Campbell
  • 1,033
  • 10
  • 13
8

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

layout.setBackground(getResources().getDrawable(R.id.img_wstat_tstorm));

David C Adams
  • 1,935
  • 11
  • 12
5
//Java
view.setBackground(ActivityCompat.getDrawable(context, R.drawable.bg))

//Kotlin 
view.background = ActivityCompat.getDrawable(context, R.drawable.bg)
Eliasz Kubala
  • 3,730
  • 1
  • 22
  • 28
5

Use this:

myView.background = ContextCompat.getDrawable(context, R.id.my_drawable)
Malwinder Singh
  • 6,324
  • 14
  • 60
  • 96
3

This is correct in my case Solve this problem

 imageView.setBackgroundResource(images[productItem.getPosition()]);
Sonu Kumar
  • 952
  • 1
  • 11
  • 36
2

Correct as of 23th November 2018

Kotlin:

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

If you include the Theme parameter.

Dimitri de Jesus
  • 1,501
  • 11
  • 13
0

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

Vostro
  • 179
  • 2
  • 3
-1
BitmapDrawable background = new BitmapDrawable(BitmapFactory.decodeResource(getResources(), R.mipmap.Nome_imgem));
        getSupportActionBar().setBackgroundDrawable(background);
Stephen Rauch
  • 44,696
  • 30
  • 102
  • 125