15

I mean something like:

<string name="error" color="#9a1d1d">Error!</string>
apaul
  • 16,092
  • 8
  • 47
  • 82
Gimmy88
  • 295
  • 2
  • 5
  • 13
  • 1
    maybe this link can help you : http://stackoverflow.com/questions/6674183/storing-hex-color-values-in-strings-xml – SAbbasizadeh Apr 14 '13 at 08:36
  • 2
    Another alternative is to something like this: `Error!` and call `Html.fromHtml(...)` on it. See [here](http://stackoverflow.com/questions/6400619/android-html-fromhtmlstring-doesnt-work-for-font-color-text-font) for an example. Personally, I prefer the flexibility of `Spannable` (Raghunandan's answer), but some people conceive working with html as more easy. – MH. Apr 14 '13 at 19:11
  • 1
    here is your solution : https://stackoverflow.com/questions/15997186/is-it-possible-to-set-the-color-of-a-string-directly-in-string-xml/51075339#51075339 – Prince Dholakiya Jun 28 '18 at 05:36

13 Answers13

18

As suggested by rekire not possible to set color the way you are doing.

You can use the method as suggested by rekire.

In you xml you can specify color for your textview as

  android:textColor="#0EFFFF"

You can also set text color programaticaly

  TextView tv= (TextView)findviewById(R.id.textView1);
  tv.setTextColor(Color.RED);  

To set color of particular words in textview, you can use spannable string

    TextView tv= (TextView)findviewById(R.id.textView1);
    tv.setText("");  
    String s="Hello World";
    SpannableString ss=  new SpannableString(s);                
    ss.setSpan(new ForegroundColorSpan(Color.GREEN), 0, 5, 0);  
    tv.setText(ss);
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • Thx, i think i'll use this method because i have to print more strings in the same textview based on the situation, so i can't set the color in the layout. – Gimmy88 Apr 14 '13 at 09:00
  • Thanks for your code. it will works perfectly that what i expect in my application. For quick understanding- First 0 is the starting index number of your colored string, 5 is the ending character of your colored string. third parameter not having any use. Thanks @Raghunandan – sudharsan chandrasekaran Sep 28 '16 at 05:06
  • @sudharsanchandrasekaran yes you are right. But the third parameter is a flag. Spannable exclusive or inclusive https://developer.android.com/reference/android/text/Spannable.html#setSpan(java.lang.Object, int, int, int). Further read http://stackoverflow.com/questions/9879233/explain-the-meaning-of-span-flags-like-span-exclusive-exclusive – Raghunandan Sep 28 '16 at 05:15
  • yep! thanks for your one more new information @Raghunandan – sudharsan chandrasekaran Sep 28 '16 at 05:20
  • is it possible to make bold the particular text instead of color changing? @Raghunandan – sudharsan chandrasekaran Sep 28 '16 at 05:34
  • @sudharsanchandrasekaran yes use bold span. You can find particular text you want using a regex – Raghunandan Sep 28 '16 at 05:36
  • @Raghunandan thanks. In my case both the two functionalities are works good. I will post the sample here for other learner's better understanding. //Code// ss.setSpan(new StyleSpan(Typeface.BOLD),80,142,0); ss.setSpan(new ForegroundColorSpan(Color.rgb(240,132,23)),80,142,0); – sudharsan chandrasekaran Sep 30 '16 at 04:52
11

Prior to 4.x, you could do this:

<string name="error"><font fgcolor="#ff9a1d1d">Error!</font></string>

However, bug https://code.google.com/p/android/issues/detail?id=58192 broke this functionality because it introduced an integer parser that can't handle numbers with the highest bit set, and unfortunately you can't omit the opacity part of the color (which most people would prefer to set to ff as in this example.)

I just yesterday learned a clever work-around. What you do is negate the hex color value in two's complement. How you do this depends on your hex calculator, but the easiest way is to subtract your color from 0x100000000. In your case, that would result in 0x100000000 - 0xff9a1d1d = 0x65e2e3. (Or you could just invert it, e.g. 0065e2e2 which would be close enough). You then negate this again with a minus sign:

<string name="error"><font fgcolor="-#65e2e3">Error!</font></string>

and voilla! you have your desired color.

Kudos to TWiStErRob for figuring this out in https://stackoverflow.com/a/11577658/338479

ETA: I just discovered that this will crash your app if you do it on a 2.x system; it throws a NumberFormat exception

Community
  • 1
  • 1
Edward Falk
  • 9,991
  • 11
  • 77
  • 112
9

Try this one

<string name="some_text">Font color is <font fgcolor="#ffff0000">red</font></string>
ihsanhf
  • 430
  • 1
  • 9
  • 18
  • did not work for me, anyone else that got this to work? edit: ah, wait..you need to use html.fromhtml for this – Boy Jan 30 '15 at 18:49
  • It broke in 4.x. See TWiStErRob's explanation and work-around in http://stackoverflow.com/a/11577658/338479 – Edward Falk Jul 27 '15 at 15:16
  • Note that anyone using this approach should use the [Resources.getText(id:)](https://developer.android.com/reference/kotlin/android/content/res/Resources#gettext) method to get the String and not the [Resources.getString(id:)](https://developer.android.com/reference/kotlin/android/content/res/Resources#getstring) method. The former method retains any rich text styling in the String and the latter does not. Also, with the `Resources.getText(id:)` method, you can use the `color` attribute instead of `fgcolor` in the `font` element just fine. – Adil Hussain Nov 27 '20 at 17:46
6

Put this code in the string.xml file:

<font color="Red"><a href="support@legacystories.org">HERE</a></font>
JacksOnF1re
  • 3,336
  • 24
  • 55
patel135
  • 927
  • 13
  • 19
5

No this is not possible you have to specify that in your layout. But you can put the color in your colors.xml.

colors.xml

<color name="foo">#abc123</color>

your_layout.xml

<TextView android:textColor="@color/foo" android:text="@string/error" />
rekire
  • 47,260
  • 30
  • 167
  • 264
5

Its possible try this..

string.xml

<string name="colorText"><![CDATA[ Give your string here. Like, This sentence has a<b><font color=#FF0000>Red color</b> text.]]></string>

ExampleClass.java

TextView colorTextView = (TextView)findViewById(R.id.colorText);
String customColorText = getResources().getString(R.string.colorText)
colorTextView.setText(Html.fromHtml(customColorText));
Nimantha
  • 6,405
  • 6
  • 28
  • 69
Silambarasan Poonguti
  • 9,386
  • 4
  • 45
  • 38
5

Strings.xml

<string name="pbs_setup_title">Select %1$s <font fgcolor="#FF33B5E5">brand</font> or scan the <font fgcolor="#FFFF0000">Remote</font></string>

class.java

String SelectedDeviceType_Str = "TV"

SetupYourDevice_TextView.setText(Html.fromHtml(String.format(Html.toHtml(new SpannedString(getResources().getText(R.string.pbs_setup_title))),
                    SelectedDeviceType_Str)));
Baby Groot
  • 4,637
  • 39
  • 52
  • 71
Shruthi
  • 51
  • 1
  • 1
3
  1. Create a colour in colors.xml file in values

      <color name ="red">#ff0000</color>
    
  2. In your strings.xml do something like

      <string name="some_text">The next word is <font fgcolor="red">red</font> but that is all</string>
    

I found I could not use the hex code directly in strings.xml

  • 2
    That only works because "red" was a pre-defined color in the system. You can only use aqua, black, blue, fuchsia, green, grey, lime, maroon, navy, olive, purple, red, silver, teal, white, and yellow. Try modifying your element from colors.xml or removing it completely; you'll see that it doesn't make any difference. – Edward Falk Jul 27 '15 at 14:11
3

In my experience, I stored color in strings.xml look like

<color name="name_color">#ffffff</color>

When I have an View and set `

nameView.setBackgroundColor(R.color.name_color);

it's OK.

But when I set color for text look like

name_TextView.setTextColor(R.color.name_color);

it's not Effect.

If you get the same problem, just set

name_TextView.setTextColor(Color.parseColor("code hexa of color you want"));

Hope it help.

sonngaytho
  • 111
  • 5
1

You can change text color through string.xml :

<string name="completed_running_not_alloted"><font fgcolor="#6DC287">Completed /</font><font fgcolor="#FFCC29"> Running /</font> Not Alloted</string>

If you want to set text programatically then you can use it it will render your text color dynamically.

 private SpannableStringBuilder setSpan(int completed, int running){
            SpannableStringBuilder span1 = new SpannableStringBuilder(completed+" / ");
            ForegroundColorSpan color1=new ForegroundColorSpan(Color.parseColor("#6DC287"));
            span1.setSpan(color1, 0, span1.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);

            SpannableStringBuilder span2 = new SpannableStringBuilder(running+"");
            ForegroundColorSpan color2=new ForegroundColorSpan(Color.parseColor("#FFCC29"));
            span2.setSpan(color2, 0, span2.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);

            Spanned concatenated=(Spanned) TextUtils.concat(span1, span2);

            return new SpannableStringBuilder(concatenated);
        }
Ramapati Maurya
  • 654
  • 9
  • 11
1

i hope this help for you.

   TextView textView=findViewById(R.id.text);

   String error= getResources().getString(R.string.error);
   ForegroundColorSpan fgColor=new ForegroundColorSpan(Color.RED);  // here set the color of Text

   SpannableString ss=new SpannableString(error);  //here set the text to spannableString

   ss.setSpan(fgColor,0,error.length(),Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);  //here which color , when to where you will set the color
   textView.setText(ss);

What is SpannableString in android ?

Android SpannableString Example. The SpannableString in android is an excellent way to style strings in a TextView. Put simply, it allows a TextView to provide different styles to different areas of text.

Prince Dholakiya
  • 3,255
  • 27
  • 43
0

In Strings.xml:

<resources>
        <color name="etDisabled">#ac8888</color>
        <color name="myorange">#f56415</color>
        <color name="mygreen">#95cd08</color>
  </resources>

Then in the code:

 TextView myText = (TextView) findViewById(R.id.tvJugador1);
  myText.setTextColor(R.color.mygreen);
kike0kike
  • 119
  • 7
-1

Is it possible to set the color of a string directly in string.xml?

Yes it is possible...

Do not use hex codes or the colors you define in color! Use default colors in Android Studio. RED WHITE BLACK and automatically suggested colors like this. If you use hex codes, text will not be visible in api before api 19

<string name="try"><font color="RED">This is blue color words </font> this default color words</string>

and for Android Stduio other HTML codes

<string name="try2"><b>Bold style words</b> And this not bold ;) </string>
<string name="try3"><u>Underline style words</u> And this not underline </string>
<string name="try4"><i>italic style words</i> And this not underline </string>

Source : https://developer.android.com/guide/topics/resources/string-resource.html#escaping_quotes

bzgd
  • 49
  • 3