1

I am trying to set a background image from URL to a linear layout with bitmap but the method BackgroundDrawable() is deprecated and i cannot find any alternative for it.

private LinearLayout linearLayout;
linearLayout = (LinearLayout) findViewById(R.id.layout);
Drawable drawableBitmap = new BitmapDrawable(getApplicationContext().getResources(), getBitmapFromURL(url));
linearLayout.setBackgroundDrawable(drawableBitmap);
Citrix
  • 257
  • 4
  • 14
  • `setBackgroundDrawable()` was replaced by `setBackground()` in API Level 16, as is noted in [the documentation](https://developer.android.com/reference/android/view/View.html#setBackgroundDrawable(android.graphics.drawable.Drawable)). – CommonsWare May 20 '17 at 18:23
  • Possible duplicate of [setBackgroundDrawable() deprecated](http://stackoverflow.com/questions/27141279/setbackgrounddrawable-deprecated) – tynn May 20 '17 at 18:24

2 Answers2

1

Try this solution. Your bitmap goes here,

LinearLayout bg = (LinearLayout) findViewById(R.id.linearlayout);
BitmapDrawable ob = new BitmapDrawable(getResources(), bitmap)
bg.setBackground(ob);
amarnath
  • 785
  • 3
  • 19
  • 23
saopayne
  • 179
  • 3
  • 15
0

use Picasso library

           private LinearLayout linearLayout;
           linearLayout = (LinearLayout) findViewById(R.id.layout);
           ImageView img = new ImageView(this);
           Picasso.with(this)
          .load(imageUri)
          .fit()
          .centerCrop()
          .into(img, new Callback() {
                    @Override
                    public void onSuccess() {                            
                    linearLayout.setBackgroundDrawable(img.getDrawable());
                    }

                    @Override
                    public void onError() {

                    }
                });
sasikumar
  • 12,540
  • 3
  • 28
  • 48