11

Is there anyway to set the color of a string resource in android? I mean, I know I can use some html tags to change string style (or substrings) but have not found any to change color. I have seen other solutions here at stackoverflow like passing the string to Html.fromHtml(string) before setting the text but I want to do it in the string resource editor. Any possibility?

PearsonArtPhoto
  • 38,970
  • 17
  • 111
  • 142
Notbad
  • 5,936
  • 12
  • 54
  • 100

4 Answers4

21

It looks like this method is working:

    <string name="some_text">this is <font fgcolor="#ffff0000">red</font></string>
Kapé
  • 4,411
  • 3
  • 37
  • 54
  • 7
    It seems to have stopped working in 4.3 (possibly earlier) -- anybody know what's up? – Edward Falk Sep 11 '13 at 07:08
  • 8
    [Why it stopped working](https://code.google.com/p/android/issues/detail?id=58192), [how it stopped working](https://github.com/android/platform_frameworks_base/commit/a8f6d5f0720f400b6f59b0809aaefea83c5f51d4), [a nice workaround / involves code :(](http://stackoverflow.com/a/18839246/253468) and an ugly workaround: for any color above `7fffffff` apply the following: `` put `ff6890a5` into a calculator (optionally convert to decimal first) and flip the sign, then (optionally convert back to hexa) take the last 8 hexadecimal digits and use ``. – TWiStErRob Sep 04 '14 at 20:51
  • 2
    OK, now **THAT** was bloody brilliant. It worked perfectly. Thank you so much and I wish I'd known about this a long time ago. – Edward Falk Jul 26 '15 at 19:51
  • 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:45
12

As far as I know it is not possible. I would use a SpannableString to change the color.

    int colorBlue = getResources().getColor(R.color.blue);
    String text = getString(R.string.text);
    SpannableString spannable = new SpannableString(text);
    // here we set the color
    spannable.setSpan(new ForegroundColorSpan(colorBlue), 0, text.length(), 0);

Spannable is really nice. You can set thinks like fontseize and stuff there and just attach it to a text view. The advantage is that you can have different colors in one view.

Edit: Ok, if you only want to set the Color the solution mentioned above me is the way to go.

Norman
  • 2,351
  • 1
  • 20
  • 16
  • I see, if this is the way to go I could create my string resources with some "code" in it to know where the substring I want to change the color begins and ends and process it the way you exposed. Thanks. – Notbad Mar 09 '12 at 14:31
  • `int colorBlue = getResources().getColor(R.color.blue); .... spannable.setSpan(new ForegroundColorSpan(colorBlue), 0, text.length(), 0);` can be replaced by a single line like this `spannable.setSpan(new ForegroundColorSpan(R.color.blue) ..` – Anand Sainath Jul 15 '13 at 10:25
  • Also, I would personally use the flag constants declared in the spannable class instead of hard-coding 0 during the call. Makes for more readable and maintainable code. Just something that I would do.. – Anand Sainath Jul 15 '13 at 10:28
0

I have recently made a flexible solution for this problem. It enables me of easily add multiple styles to substrings by using method chaining. It makes use of the SpannableString. When you want to give a certain substring a color, you can use ForegroundColorSpan.

public StyledString putColor(String subString, @ColorRes int colorRes){
    if(getStartEnd(subString)){
        int color = ColorUtil.getColor(context, colorRes);
        ForegroundColorSpan foregroundColorSpan = new ForegroundColorSpan(color);
        fullStringBuilder.setSpan(foregroundColorSpan, startingIndex, endingIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    }
    return this;
}

For full code see gist.

EJay
  • 318
  • 1
  • 4
  • 17
  • While a link is great, please [provide context to your link(s)](http://meta.stackoverflow.com/a/8259/169503), as an *answer* should actually contain an answer. Be aware that being *barely more than a link to an external site* is a possible reason as to [Why and how are some answers deleted?](http://stackoverflow.com/help/deleted-answers). – Jan Apr 26 '16 at 10:05
0

The strings themselves have no color, but you can change the color of the text in the textView they appear in. See the textview documentation, but there are 2 ways to do it.

XML

android:textColor

Code

setTextColor(int)
PearsonArtPhoto
  • 38,970
  • 17
  • 111
  • 142
  • Thanks for the answer. I think it wasn't well formulated. I refered to change the color of a substring in the resource string. Sorry for that. – Notbad Mar 09 '12 at 14:28