I mean something like:
Error!
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.
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));
Happy coding....
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.
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);
}
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
Create a colour in colors.xml file in values
<color name ="red">#ff0000</color>
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