I am using a large number of textviews across my android app. And I have a background.xml file with shape and color property of rounded edges. I set this background to my textview. If I have a few different colors as background, I can create seperate background xmls. What if I have more than 30 or 40 different colors to be used, should I have as many background files ?
Asked
Active
Viewed 556 times
0
-
no, use Drawable.setColorFilter – pskink Apr 28 '14 at 06:19
2 Answers
1
You need to maintain single background files and create all stying attribute for your text view like this:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="style_1" parent="@android:style/TextAppearance.Medium">
<item name="android:textColor">#FFA500</item>
<item name="android:textColor">#FFA500</item>
</style>
<style name="style_3" parent="@android:style/TextAppearance.Medium">
<item name="android:textColor">#FFA500</item>
<item name="android:textColor">#FFA500</item>
</style>
<style name="style_n" parent="@android:style/TextAppearance.Medium">
<item name="android:textColor">#FFA500</item>
<item name="android:textColor">#FFA500</item>
</style>
</resources>
For styling please refer: http://developer.android.com/guide/topics/ui/themes.html

Lavekush Agrawal
- 6,040
- 7
- 52
- 85
-
-
You can create common drawable for shape and use in Style. ref this: http://stackoverflow.com/questions/8271329/how-to-get-my-own-defined-attribute-value-in-my-style – Lavekush Agrawal Apr 28 '14 at 06:26
1
you can use PaintDrawable with Dynamic color in Java code like below.
final TextView hint = (TextView) layout.findViewById(R.id.txt_hint);
hint.setTextColor(Color.WHITE);
PaintDrawable paintDrawable1=new PaintDrawable(getRandomColor());
paintDrawable1.setCornerRadius(20f);
hint.setBackground(paintDrawable1);
private int getRandomColor(){
Random rnd = new Random();
return Color.argb(255, rnd.nextInt(256), rnd.nextInt(56), rnd.nextInt(256));
}

Rajesh.k
- 2,327
- 1
- 16
- 19