简体   繁体   中英

Set Text Color for textView Android

In the string.xml file i use the following tag

 <string name="CodeColor" >"#0000ff"</string>

If I use

 textview1.setTextColor(Color.RED);

it works, but when I use

  textview1.setTextColor(TextViewStyles.this.getResources().getColor(R.string.CodeColor)); 

 or
 textview1.setTextColor(R.string.CodeColor);

it doen't work. Any suggestions...

Thanks in Advance

You need to create a set of styles in your xml (regularly in res/values/styles.xml)

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="gray">#eaeaea</color>
    <color name="titlebackgroundcolor">#00abd7</color>
    <color name="titlecolor">#666666</color>
<resources>

In the layout files you can call to the colors or styles:

android:textColor="@color/titlecolor"

Checkout some examples:

http://developer.android.com/guide/topics/ui/themes.html

You can use

  textView1.setTextColor(getResources().getColor(R.color.mycolor))

or

  textview1.setBackgroundColor(Color.parseColor("#ffffff"));

or

    textview1.setBackgroundColor(Color.RED);

or

    textView1.setBackgroundColor(R.color.black);

This may be easier:

TextView textresult = (TextView)findViewById(R.id.textView1);
textresult.setTextColor(Color.RED);

you should use R.color.CodeColor . you are using R.string.CodeColor .

try set color like this may helps you

txt.setTextColor(Color.rgb(0, 87, 48));

this is different way but it can change color , here need Red,Green,Blue Code to pass

I am basically just merging all the partially good answers.

You defined your color as a String , but AFAIK Android processes colors as Itegers .
So use the Colors.xml file (instead of strings.xml ): and refer to it in code as R.color.CodeColor .
(Moreover, I think, there is some naming convention that tells you to name these values all lowercase: code_color or codecolor )

Or you can define them as strings, but then you are need to make it an Integer: Color.parseColor(R.string.code_color) .

Define colors in colors.xml file like that:

  <resources>
       <color name="CodeColor" >#0000ff</color>
  </resources>

Then use color whatever you like in your code using: R.color.CodeColor

Good luck!

I tried something like:

textView.setTextColor(R.color.Red);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM