Cambiar el color de la palabra en la cadena de recursos

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?

preguntado el 09 de marzo de 12 a las 14:03

4 Respuestas

Parece que este method is working:

    <string name="some_text">this is <font fgcolor="#ffff0000">red</font></string>

Respondido 20 Jul 12, 11:07

It seems to have stopped working in 4.3 (possibly earlier) -- anybody know what's up? - eduardo falk

Why it stopped working, how it stopped working, a nice workaround / involves code :( and an ugly workaround: for any color above 7fffffff apply the following: <font color="#ff6890a5"> poner 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 <font color="-#00976F5B">. - TWiStErRob

Ok ahora QUE was bloody brilliant. It worked perfectly. Thank you so much and I wish I'd known about this a long time ago. - eduardo falk

Note that anyone using this approach should use the Resources.getText(id:) method to get the String and not the Resources.getString(id:) method. The former method retains any rich text styling in the String and the latter does not. Also, with the Resources.getText(id:) método, puede utilizar el color atributo en lugar de fgcolor en el objeto font element just fine. - Adil Hussain

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

    int colorBlue = getResources().getColor(;
    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.

respondido 09 mar '12, 14:03

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. - No está mal

int colorBlue = getResources().getColor(; .... spannable.setSpan(new ForegroundColorSpan(colorBlue), 0, text.length(), 0); can be replaced by a single line like this spannable.setSpan(new ForegroundColorSpan( .. - anand sainath

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

The strings themselves have no color, but you can change the color of the text in the textView they appear in. See the documentación de vista de texto, but there are 2 ways to do it.





respondido 09 mar '12, 14:03

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. - No está mal

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){
        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 esencia.

Respondido 26 Abr '16, 12:04

While a link is great, please provide context to your link(s)como un should actually contain an answer. Be aware that being apenas más que un enlace a un sitio externo es una posible razón de ¿Por qué y cómo se eliminan algunas respuestas?. - Ene

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.