Seyid-Kanan Bagirov
Seyid-Kanan Bagirov

Reputation: 103

How to use setBackgroundDrawable

I'm working with image view in android for devices from API 12 to 22. As we know, there are 2 methods for calling setBackground differ, whether we use API 16+ or a lower API. I do it as most tutorials and other Stack Overflow questions guided, but I still get black color instead of image in the image view. What else should I do to make it work?

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

bitmap = BitmapDecoder.decodeBitmapFromResource(context.getResources(),
                    R.drawable.sample_photo, 200, 500);

    if(sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
        setBackgroundV16Minus(holder.locationImage, bitmap);
    } else {
        setBackgroundV16Plus(holder.locationImage,bitmap);
    }

update:

@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
private void setBackgroundV16Plus(View view, Bitmap bitmap) {
    view.setBackground(new BitmapDrawable(context.getResources(),bitmap));
}

@SuppressWarnings("deprecation")
private void setBackgroundV16Minus(View view, Bitmap bitmap) {
    view.setBackgroundDrawable(new BitmapDrawable(bitmap));
}

Upvotes: 0

Views: 468

Answers (1)

IntelliJ Amiya
IntelliJ Amiya

Reputation: 75788

The setBackgroundDrawable() method of the Class in is now deprecated in android SDK API level 16

Try this logic

 if (android.os.Build.VERSION.SDK_INT >= 16)
  .setBackground(...);
else
  .setBackgroundDrawable(...);

Upvotes: 1

Related Questions