14

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(????);

1
  • you want to set colour to the Text of the TextView or background of textview? Commented Apr 23, 2015 at 6:40

7 Answers 7

22

Color entries should be like this

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

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

Sign up to request clarification or add additional context in comments.

1 Comment

I don´t know why, but I defined my "color" as a "drawable" :-/ you are right, but I have to set the color like this >> getResources().getColor(R.color.tableHead) <<
10

Use,..

Color.parseColor("#bdbdbd");

like,

mTextView.setTextColor(Color.parseColor("#bdbdbd"));

OR......................

Get a handle to the root layout used, then set the background color on that. The root layout is whatever you called setContentView with.

// Now get a handle to any View contained // within the main layout you are using

 View someView = findViewById(R.id.randomViewInMainLayout);

// Find the root view

 View root = someView.getRootView()

// Set the color

  root.setBackgroundColor(getResources().getColor(android.R.color.red));

Comments

4
tv.setTextColor(getResources().getColor(R.color.tableHead));

And guess what your colors.xml should be like this

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="tableHead">#FF444444</color>
</resources>

Comments

2

Your color.xml should look like this:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="tableHead">#FF444444</color>
</resources>

How you will use this color to set in textview: Like this

tv.setBackgroundColor(getResources().getColor(R.color.tableHead));

Comments

1

Try something like this:

tv.setBackgroundResource(Color.parseColor("#ffffff"));

Comments

1

Firstly modify your color.xml as below

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

</resources>

For setting the textview background color you can do like

tv.setBackgroundColor(R.color.tableHead);

Additionally for setting the textview textcolor you can do like

tv_empty.setTextColor(R.color.tableHead)

Comments

0

if there is a color in color constants like

<color name="error_red_color">#f00</color>

then it can be set as following -

tv.setTextColor(ContextCompat.getColor(context, R.color.error_red_color))

or

tv.setTextColor(getResources().getColor(R.color.error_red_color, null))

Other ways are -

tv.setTextColor(Color.RED);

tv.setTextColor(Color.parseColor("#FFFFFF"));

tv.setTextColor(Color.rgb(100,100,100));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.