Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: set color programatically from XML color constants

Trying to set a color which is defined in res/values/colors.xml to an object,

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <drawable name="listViewSelected">@android:color/holo_blue_light</drawable>
  <drawable name="listViewPressed">@android:color/holo_green_light</drawable>
  <drawable name="pagerTabStrip">#2B3333</drawable>
  <!--<drawable name="pagerTabStrip">#353F3E</drawable>-->
  <drawable name="tableHead">#FF444444</drawable>

</resources>

I can not figure out why it is not working, I tried a lot of approaches (getResources(), Color.parseColor(), ...)

How do I set the color "tableHead" e.g. to a TextView?

tv.setBackgroundColor(????);

like image 931
prototype0815 Avatar asked Apr 23 '15 06:04

prototype0815


People also ask

How do I change text color in XML?

In XML, we can set a text color by the textColor attribute, like android:textColor="#FF0000" .

How do I change the background color in programmatically?

screen); View root = someView. getRootView(); root. setBackgroundColor(color. white);


Video Answer


1 Answers

Color entries should be like this

<color name="tableHead">#FF444444</color>

and use tv.setBackgroundResource(R.color.tableHead);

like image 189
Sohaib Avatar answered Oct 10 '22 01:10

Sohaib