Kanika
Kanika

Reputation: 10708

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?

Upvotes: 19

Views: 55951

Answers (7)

  McMillanMe
McMillanMe

Reputation: 1

Use @Deimos's answer, but like this since some methods are deprecated now

Bitmap bmImg = BitmapFactory.decodeStream(is);
BitmapDrawable background = new BitmapDrawable(context.getResources(), bmImg);
linearLayout.setBackground(background);

Upvotes: 0

Sunil_Suthar
Sunil_Suthar

Reputation: 1094

You can also set image from drawable folder.

yourView.setBackgroundResource(R.drawable.FILENAME);

That set FILENAME as background image.

Upvotes: -1

Matheus Evangelista
Matheus Evangelista

Reputation: 31

Try to use this:

Bitmap bmpOriginal = BitmapFactory.decodeResource(getResources(), R.drawable.img);
BitmapDrawable bmpBackground = new BitmapDrawable(getResources(), bmpOriginal)

Upvotes: 0

Hiren Patel
Hiren Patel

Reputation: 52790

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.

Upvotes: 5

Inder Kumar Rathore
Inder Kumar Rathore

Reputation: 39978

API are deprecated you can use the below code

BitmapDrawable background = new BitmapDrawable(getResources(), bitmapImage);
linearLayout.setBackground(background);

Upvotes: 4

Deimos
Deimos

Reputation: 1091

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?

Upvotes: 51

kazufukurou
kazufukurou

Reputation: 166

An easier way:

BitmapDrawable d = new BitmapDrawable("/sdcard/data/image.jpg");
linearLayout.setBackgroundDrawable(d);

Upvotes: 4

Related Questions