Change word color in resource string - android

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?

It looks like this method is working:
<string name="some_text">this is <font fgcolor="#ffff0000">red</font></string>

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.

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)

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.

Related

Change the color of a part of a TextView in strings.xml

In my android app with Kotlin, I created a layout in which there's a TextView that shows some text. For the text I have an item in strings.xml where I want to change the color of part of this Text, I tried the following code :
<string name="description">the product is <font fgcolor="green"> free </font></string>
But, The color didn't change.
I just want to change the color of "free" to green, can someone explain how I can achieve this?
Use <font color="#008000">free</font> instead. According to the documentation, the correct attribute name is color and it only supports hex codes.
Ben P.'s awesome answer should satisfy your use case. However, I want to present to you another way you can achieve this.
You can use SpannableString to achieve the same effect. With SpannableString, you can set several behaviours (color, font-weight, font-size, click-behaviour, etc) to any part of your String.
For the string in your question, you can do something like this:
// the textview you want to set your coloured text to
TextView textView = (TextView) findViewById(R.id.myTextView);
// declare the string you want to span as a Spannable
Spannable wordtoSpan = new SpannableString("the product is free");
// set the colour span
wordtoSpan.setSpan(new ForegroundColorSpan(Color.GREEN), 15, 19, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
// set the text to your TextView
textView.setText(wordtoSpan);

Is there any way to change the color of an already applied BackgroundColorSpan in a SpannableString?

What I'm actually doing is I'm storing a SpannableString in the form of HTML but it has a BackgroundColorSpan which has an aplha channel in its color. Now I got to know (via trials) that my aplha channel of the color goes away (due to inability of HTML) from the text whenever I try to store it.
Now what I want to know is that is there a way I can extract all the BackgroundColorSpan instances in the SpannableString AND change their color property? All the BackgroundColorSpan instances are of same color I just want to add an alpha channel to their color (by changing their color) before I present the text to users.
I figured out a way to extract all the BackgroundColorSpan instances by using getSpans but i still can't find a way to change their color.
Here's the related code:
SpannableString spannableDescString = new SpannableString(trimTrailingWhitespace(Html.fromHtml(note.getDesc())));
BackgroundColorSpan[] highlightSpanArray = spannableDescString.getSpans(0,spannableDescString.length(),BackgroundColorSpan.class);
if(highlightSpanArray.length!=0){
for(BackgroundColorSpan item : highlightSpanArray){
//what should I put here to change every item's color
}
}
desc.setText(spannableDescString);
Nevermind, I got the answer here
All I had to do was to delete the current span and replace it with the BackgroundColorSpan of the color that I required. Here's the code snippet.
SpannableString spannableDescString = new SpannableString(trimTrailingWhitespace(Html.fromHtml(note.getDesc())));
BackgroundColorSpan[] highlightSpanArray = spannableDescString.getSpans(0,spannableDescString.length(),BackgroundColorSpan.class);
if(highlightSpanArray.length!=0){
for(BackgroundColorSpan item : highlightSpanArray){
//what should i put here to change every items color
// get the span range
int start = spannableDescString.getSpanStart(item);
int end = spannableDescString.getSpanEnd(item);
// remove the undesired span
spannableDescString.removeSpan(item);
// set the new span with desired color
spannableDescString.setSpan(new BackgroundColorSpan(Color.RED),start,end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
desc.setText(spannableDescString);
I simply didn't know if I could find the starting and ending of individual spans.

Change attributes to a selected part of text in EditText

I've searched something around here but nothing came up.
I'd like to have an EditText in which I can change attributes like color or dimension of a selected part.
I've already tried with the spannable thing, from another question:
TextView myTV = (TextView)findViewById(R.id.test);
String textString = "StackOverFlow Rocks!!!";
Spannable spanText = Spannable.Factory.getInstance().newSpannable(textString);
spanText.setSpan(new BackgroundColorSpan(0xFFFFFF00), 14, 19, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
myTV.setText(spanText);
I assume that I have to assign it to some onClick method..Also my problems are those two number, I should put some "selectedText" there instead I think.
Is this even possible?
I'd like to have an EditText in which I can change attributes like color or dimension of a selected part
If you want the user to "change attributes... of a selected part", my recently-updated RichEditText offers that. I do not have color or text size going yet, though, as those will need a toolbar (rather than my current action mode support).
Also my problems are those two number, I should put some "selectedText" there instead I think
You are probably looking for getSelectionStart() and getSelectionEnd().

Color is lost after adding string format

I need to add some text in a textview with some text with red color and some are black color with a specific formatting
Spannable wordtoSpan = new SpannableString(temp.substring(start, i));
wordtoSpan.setSpan(new ForegroundColorSpan(Color.RED), 0, wordtoSpan.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
//failedToSolve.append(wordtoSpan); // was working when I used no formatting
failedToSolve.append(String.format("%7s", wordtoSpan)); // coloring is not working after I apply String.format on it
I know I am formatting Spannable object with String class method. Is there any alternatives?
How can I do the formatting and coloring together? I used HTML tag but no effects. Thanks in advance.
Is there any alternatives?
Call String.format() first, then create the SpannableString from the result and apply the ForegroundColorSpan.

Multi Formatted TextView

I'm trying to have the layout of the comments on a progam in the following form:
I want the USER_NAME in Bold, and the DATE in Enphasis (The red part should be normal, I don't want it red). My problem comes with the red part. I cant get the cooment to be displayed like this using layouts, I've been able to get:
and
Is there any way to get the comment working like the one I'm showing. I've thought about a multi formatted textView, is it posible??
If getting the comment like this is not possible, just say it's imposible.
One possible solution is to put two TextViews into a Linear layout one after another and then use SpannableStringBuilder to assign the formatted text to the first view.
SpannableStringBuilder sb = new SpannableStringBuilder();
sb.append(text);
sb.setSpan(createBoldSpan(), 0, lengthOf(userName),
Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
firstTextView.setText(sb, TextView.BufferType.NORMAL);
createBoldSpan should return TextAppearanceSpan
private TextAppearanceSpan userNameSpanInBold(String userName) {
return new TextAppearanceSpan(...);
}
please follow API docs for TextAppearanceSpan - you can create one using custom style.
You could use a WebView with loadData if HTML markup would be good, or alternatively delve into the realm of Spannable text.
I've not worked with Spannable text myself, but essentially it allows you to attach attributes (e.g. styles & colors) to parts of text in a TextView.
The relevant method is TextView.setText(CharSequence text, TextView.BufferType type) with BufferType of SPANNABLE. The documentation I've seen is.... confusing.

Categories

Resources