A J
A J

Reputation: 4652

set background color dynamically

I want to set background color dynamically but the colour is actually a gradient which is in a xml

bg_color.xml

<?xml version="1.0" encoding="utf-8"?>

<item><shape>
        <gradient android:angle="270" android:endColor="#f58c0f" android:startColor="#edbc7a" />

        <stroke android:width="1dp" android:color="#929292" />

        <padding android:bottom="10dp" android:left="10dp" android:right="10dp" android:top="10dp" />
    </shape></item>

which is inside drawable folder

how to set this xml as background.

Upvotes: 1

Views: 3639

Answers (3)

pskink
pskink

Reputation: 24750

view.setBackgroundResource(R.drawable.bg_color)

Upvotes: 1

Chintan Rathod
Chintan Rathod

Reputation: 26034

you just need to pass that "drawable" file name as a resource

yourview.setBackgroundResource(R.drawable.your_xml_drawable_file_name);

in your case

textView.setBackgroundResource(R.drawable.bg_color);

Upvotes: 1

kostas ch.
kostas ch.

Reputation: 2035

Use

TextView et = new TextView(activity);
et.setText("350");
et.setBackgroundColor(getResources().getColor(//some color));

Upvotes: 0

Related Questions