1

I have a linearLayout I'd like to change the background of programatically:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/downloadLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"

    android:weightSum="1" >
    ...

and I've attempted to set the background Image of the XML layout using the following:

LinearLayout linearLayout2 = (LinearLayout) findViewById(R.id.downloadLayout);
        int resId = getResources().getIdentifier(background,
                "drawable", getPackageName());

linearLayout2.setBackgroundResource(resId);

However the background image never loads, there is no NPE, the image simply never loads. Any suggestions are appreciated.

I've done a bit of debugging and I currently have the following values:

        linearLayout2 = android.widget.LinearLayout{529b3f58 V.E..... ......I. 0,0-0,0 #7f0a008e app:id/downloadLayout}
        background = http://xxx.xxx.x.xxx/bgs/big_lebowski_bg.jpg
        resID = 0

P.S.

I've also tried accomplishing the same using Picasso - I'm not sure how to get around the error stated and load it successfully:

Source:

final LinearLayout downloadLayout = (LinearLayout) findViewById(R.id.downloadLayout);
Picasso.with(this).load("http://i.imgur.com/DvpvklR.png").into(downloadLayout);

Error:

The method into(Target) in the type RequestCreator is not applicable for the arguments (LinearLayout)

2 Answers2

1

Picasso works with ImageViews only, not layouts.

You could put an ImageView inside your layout, set it to match parent's width and height, and inject the image into the ImageView.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/downloadLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:weightSum="1" >
        <ImageView
            android:id="@+id/imageView"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
</LinearLayout>

This should work then:

ImageView imageView = (ImageView) findViewById(R.id.imageView);    
Picasso.with(this).load("http://i.imgur.com/DvpvklR.png").into(imageView);
TomaszRykala
  • 927
  • 6
  • 16
  • 33
0

You have to do so in the background thread and not on the main thread . Consider using image loading libraries such as Picasso

Example is UPDATED

Make sure you have an image view in the LinearLayout and let it fill_parent (i.e the LinearLayout), Picasso is not applicable to an ImageView directly hence the error.

ImageView imageView = (ImageView) findViewById(R.id.imageView); 
Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);

ALTERNATIVELY

LinearLayout linearLayout=(LinearLayout)findViewById(R.id.yourLinearLayoutid);
BitmapDrawable drawableBitmap=new BitmapDrawable(getBitmap(urlString));
linearLayout.setBackgroundDrawable(drawableBitmap);

This method getBitMap, like the one in the Piccasso library should be sufficient without needing the library .

private Bitmap getBitmap(String url)
    {


        //from web
        try {
            Bitmap bitmap=null;
            URL imageUrl = new URL(url);
            HttpURLConnection conn = (HttpURLConnection)imageUrl.openConnection();
            conn.setConnectTimeout(30000);
            conn.setReadTimeout(30000);
            conn.setInstanceFollowRedirects(true);
            InputStream is=conn.getInputStream();
            OutputStream os = new FileOutputStream(f);
            Utils.CopyStream(is, os);
            os.close();
            bitmap = decodeFile(f);
            return bitmap;
        } catch (Exception ex){
           ex.printStackTrace();
           return null;
        }
    }
Otieno Rowland
  • 2,182
  • 1
  • 26
  • 34