Usman Asif
Usman Asif

Reputation: 111

Set background color and use Drawable as background in Android

This code is used to create a Button dynamically. The problem is that I want to set background color and also set a background Drawables.

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <stroke android:width="1px" android:color="#696969"/>
</shape>

This is the class I want to set the background color of a Button and then I want use my Drawable.

Button btnTag = new Button(alltable.this);
btnTag.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, 
                       LinearLayout.LayoutParams.WRAP_CONTENT));
try {
    btnTag.setWidth(130);
    btnTag.setBackground(getResources().getDrawable(R.color.blue));

} catch (Exception e){
    e.printStackTrace();
}

Upvotes: 9

Views: 50655

Answers (5)

kj007
kj007

Reputation: 6254

Create a resource in drawable (like files_bg.xml) folder and set it as background for layout.

Use two item in layer list, one for solid color in shape and other one bitmap.

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <solid android:color="@color/totalFilesBgColor" />
        </shape>
    </item>
    <item>
        <bitmap
            android:src="@drawable/bg_icon"
            android:tileMode="disabled" />
    </item>
</layer-list> 

and now set drawable as background in layout or wherever you are using it.

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/files_bg">
</LinearLayout>

Upvotes: 15

msayubi76
msayubi76

Reputation: 1658

This work for me

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
  <item>
    <shape android:shape="rectangle">
        <solid android:color="@color/appColor" />
        <corners android:radius="@dimen/spacing_small"/>
    </shape>
   </item>
     <item android:drawable="@drawable/add_white"/>
</layer-list>

Upvotes: 3

Karan Goel
Karan Goel

Reputation: 1

You can create a new drawable and assign the same to the Background. i.e.


Drawable drawable = new GradientDrawable();
drawable.shape = GradientDrawable.RECTANGLE;
drawable.setStroke(Utils.toPx(1), Color.parseColor("#696969") );
btnTag.setBackground(drawable);

Upvotes: 0

tequilasunset
tequilasunset

Reputation: 1269

A bit late but I had the same problem and I solved it like it's described bellow.

First get your drawable:

Drawable d = getResources().getDrawable(R.drawable.shape_rectangle);

Then apply your custom color to it:

d.setColorFilter(color, PorterDuff.Mode.SRC_ATOP);

Then set it as background to your view/button:

btn.setBackground(d);

Upvotes: 5

Prathap Badavath
Prathap Badavath

Reputation: 1671

This file(rectangle.xml ) put in your drawable folder.

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
<solid android:color="#FF4081"/>
</shape>

modify this line in your code.

btnTag.setBackground(getResources().getDrawable(R.drawable.rectangle,null));\\API level 21 and higher, otherwise
 getResources().getDrawable(R.drawable.rectangle).

Upvotes: 6

Related Questions