Reputation: 305
I mean something like:
<string name="error" color="#9a1d1d">Error!</string>
Upvotes: 16
Views: 57551
Reputation: 9442
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));
Upvotes: 5
Reputation: 49
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
Upvotes: -1
Reputation: 664
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);
}
Upvotes: 1
Reputation: 3391
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.
Upvotes: 1
Reputation: 927
Put this code in the string.xml
file:
<font color="Red"><a href="[email protected]">HERE</a></font>
Upvotes: 6
Reputation: 10063
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
Upvotes: 11
Reputation: 115
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);
Upvotes: 0
Reputation: 111
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.
Upvotes: 3
Reputation: 61
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)));
Upvotes: 6
Reputation: 99
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
Upvotes: 3
Reputation: 440
Try this one
<string name="some_text">Font color is <font fgcolor="#ffff0000">red</font></string>
Upvotes: 10
Reputation: 133560
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);
Upvotes: 18
Reputation: 47945
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" />
Upvotes: 5