1

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.

Sagar Maiyad
  • 12,655
  • 9
  • 63
  • 99
A J
  • 4,542
  • 5
  • 50
  • 80

3 Answers3

1

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);
Chintan Rathod
  • 25,864
  • 13
  • 83
  • 93
1

view.setBackgroundResource(R.drawable.bg_color)

pskink
  • 23,874
  • 6
  • 66
  • 77
0

Use

TextView et = new TextView(activity);
et.setText("350");
et.setBackgroundColor(getResources().getColor(//some color));
kostas ch.
  • 1,960
  • 1
  • 17
  • 30