Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set Linear layout background dynamically

I wanted to set Linear Layout background dynamically in the following way:

  1. Fetch image from web url through XML parsing and then store that image into sd card.

  2. Now the image saved into sd card.

  3. Set that image as a linear layout background in the app.

Now I am stuck in the third step. Can anyone help?

like image 943
Kanika Avatar asked Oct 04 '11 10:10

Kanika


People also ask

How do I change the background color of linear layout programmatically?

LinearLayout prog = new LinearLayout(this); prog. setBackgroundColor(CommonUtils. getNextRandomColor()); LinearLayout.

How to set background of layout programmatically in android?

To set Background:RelativeLayout layout =(RelativeLayout)findViewById(R. id. background); layout. setBackgroundResource(R.

How to set LinearLayout?

To create a linear layout in which each child uses the same amount of space on the screen, set the android:layout_height of each view to "0dp" (for a vertical layout) or the android:layout_width of each view to "0dp" (for a horizontal layout). Then set the android:layout_weight of each view to "1" .


3 Answers

Use this:

Bitmap bmImg = BitmapFactory.decodeStream(is);
BitmapDrawable background = new BitmapDrawable(bmImg);
linearLayout.setBackgroundDrawable(background);

Also check this: How to convert a Bitmap to Drawable in android?

like image 186
Deimos Avatar answered Oct 17 '22 03:10

Deimos


I have done this way:

private RelativeLayout relativeLayout;

onCreate:

relativeLayout= (RelativeLayout)findViewById(R.id.relativeLayout);

new LoadBackground("http://www.tmonews.com/wp-content/uploads/2012/10/androidfigure.jpg",
            "androidfigure").execute();

AsyncTask to load image in background:

private class LoadBackground extends AsyncTask<String, Void, Drawable> {

    private String imageUrl , imageName;

    public LoadBackground(String url, String file_name) {
        this.imageUrl = url;
        this.imageName = file_name;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected Drawable doInBackground(String... urls) {

        try {
            InputStream is = (InputStream) this.fetch(this.imageUrl);
            Drawable d = Drawable.createFromStream(is, this.imageName);
            return d;
        } catch (MalformedURLException e) {
            e.printStackTrace();
            return null;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
    private Object fetch(String address) throws MalformedURLException,IOException {
        URL url = new URL(address);
        Object content = url.getContent();
        return content;
    }

    @Override
    protected void onPostExecute(Drawable result) {
        super.onPostExecute(result);
        relativeLayout.setBackgroundDrawable(result);
    }
}

Hope this will help you.

like image 44
Hiren Patel Avatar answered Oct 17 '22 01:10

Hiren Patel


An easier way:

BitmapDrawable d = new BitmapDrawable("/sdcard/data/image.jpg");
linearLayout.setBackgroundDrawable(d);
like image 24
kazufukurou Avatar answered Oct 17 '22 03:10

kazufukurou