Dominik
Dominik

Reputation: 1693

How to use relativelayout.setBackgroundDrawable() with a bitmap?

I have a RelativeLayout object and want to dynamically change the background image with a dynamically created Bitmap object (it changes its color dynamically).

I saw that when I wanted to update the background image of the RelativeLayout object that I can only choose setBackgroundDrawable() which requires a Drawable object as a parameter.

My question is, how can I convert the dynamically created Bitmap object into a Drawable object?

Upvotes: 10

Views: 19727

Answers (6)

Swamy
Swamy

Reputation: 132

//Create Reference for RelativeLayout

RelativeLayout layout = (RelativeLayout) findViewById(R.id.rl);

//set Background to layout reference using the following method

Drawable drawable = getResources().getDrawable(R.drawable.bg);
layout.setBackground(drawable);

Note: R.id.rl is id of RelativeLayout

R.drawable.bg id the Image in drawable folder

Upvotes: 0

Ranjan
Ranjan

Reputation: 21

RelativeLayout rl=(RelativeLayout) findViewById(R.id.main1);
Bitmap myImage = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
Drawable dr = new BitmapDrawable(myImage);
rl.setBackgroundDrawable(dr);

Upvotes: 2

Siten
Siten

Reputation: 4533

BitmapDrawable(obj) convert Bitmap object into drawable object.

Try:

RelativeLayout relative = (RelativeLayout) findViewById(R.id.relative1);
Drawable dr = new BitmapDrawable(bit);
(view).setBackgroundDrawable(drawable);

I hope this will help you.

Upvotes: 21

Nikunj Patel
Nikunj Patel

Reputation: 22066

You can do it by this way

Drawable drawable = new BitmapDrawable(bitmap);
RelativeLayout r;
r = (RelativeLayout) findViewById(R.id.relativelayout1);
ll.setBackgroundDrawable(drawable);

Upvotes: 6

user370305
user370305

Reputation: 109237

Drawable d = new BitmapDrawable(bitmap);

Upvotes: 1

Lalit Poptani
Lalit Poptani

Reputation: 67286

Try this,

Drawable drawable = new BitmapDrawable(bitmap);

Upvotes: 3

Related Questions