Change Word Color In Resource String
Solution 1:
It looks like this method is working:
<string name="some_text">this is <font fgcolor="#ffff0000">red</font></string>
Solution 2:
As far as I know it is not possible. I would use a SpannableString to change the color.
intcolorBlue= getResources().getColor(R.color.blue);
Stringtext= getString(R.string.text);
SpannableStringspannable=newSpannableString(text);
// here we set the color
spannable.setSpan(newForegroundColorSpan(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.
Solution 3:
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)
Solution 4:
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, @ColorResint colorRes){
if(getStartEnd(subString)){
intcolor= ColorUtil.getColor(context, colorRes);
ForegroundColorSpanforegroundColorSpan=newForegroundColorSpan(color);
fullStringBuilder.setSpan(foregroundColorSpan, startingIndex, endingIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
returnthis;
}
For full code see gist.
Post a Comment for "Change Word Color In Resource String"