How to set color on string - android

I want to set color just on "read more" string :
holder.Title.setText(current.getTitle());
holder.Description.setText(start+"...."+"read more");
holder.Date.setText(current.getPubDate());
I have tried to use html.fromhtml but it is not working with me !!!

Try as follow
String textFirstPart = start + "....";
String textSecondPart = "read more";
String text = textFirstPart + textSecondPart;
Spannable spannable = new SpannableString(text);
spannable.setSpan(new ForegroundColorSpan(Color.RED), textFirstPart.length(),
(textFirstPart + textSecondPart).length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
holder.Description.setText(spannable, TextView.BufferType.SPANNABLE);

Here you go
SpannableString styledString = new SpannableString("read more");
// change text color
styledString.setSpan(new ForegroundColorSpan(Color.BLUE), 0, 8, 0);
// underline text
styledString.setSpan(new UnderlineSpan(), 0, 8, 0);
Read more here

Starting from Android N,
the method Html.fromHtml(htmlText) is deprecated and you have to use
Html.fromHtml(htmlText, MODE) instead, so use the following condition,
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
holder.setText(Html.fromHtml(sourceString,Html.FROM_HTML_MODE_LEGACY);
} else {
holder.setText(Html.fromHtml(sourceString);
}
Reference:
https://developer.android.com/reference/android/text/Html#FROM_HTML_MODE_COMPACT

Related

SpannableString is not working in a Dialog

I want to show the spanString in Bold style and rest of the string in normal style in TextView. But the whole TextView showed in normal style only. Please help me.
if ((mStoreListValue.get(pos).getStoreStatus().equals("CLOSED"))){
final Dialog dialog = new Dialog(v.getRootView().getContext(), android.R.style.Theme_Translucent_NoTitleBar);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.getWindow().setContentView(R.layout.resclosed_dialog);
ImageView btn_close = dialog.findViewById(R.id.close_btn);
TextView txt_close = dialog.findViewById(R.id.tv_closed);
SpannableString spanString = new SpannableString(sname);
spanString.setSpan(new StyleSpan(Typeface.BOLD), 0, spanString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
txt_close.setText(spanString + " " + mContext.getResources().getString(R.string.closed));
btn_close.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
dialog.dismiss();
}
});
dialog.show();
Thats because you just concat SpannableString with String which will be result as toString()(a Normal String with hash code of object). Use a single String with multiple Span.
See the following example.
String s1="My app";
String s2= "Close";
SpannableString spanString = new SpannableString(s1+s2);
spanString.setSpan(new StyleSpan(Typeface.BOLD), 0, s1.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
spanString.setSpan(new StyleSpan(Typeface.NORMAL), s1.length(), (s1+s2).length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(spanString);
Use this
SpannableStringBuilder builder = new SpannableStringBuilder();
SpannableString spanString = new SpannableString(sname);
spanString.setSpan(new StyleSpan(Typeface.BOLD), 0, spanString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
builder.append(spanString);
builder.append(mContext.getResources().getString(R.string.closed));
txt_close.setText(builder);
Instead of this
txt_close.setText(spanString + " " + mContext.getResources().getString(R.string.closed));
Using String templating, you can have a string resource:
<b> indicates bold,
%1$d indicates I am expecting a digit/number
so I get (5 min away) in the textview
//xml
<string name="confirm_eta_text"> <b>%1$d min </b> away</string>
// Java
mEta.setText(
Compat.fromHtml(String.format(getResources().getString(R.string.confirm_eta_text), 5)));
Android String Formatting

How to set color of texts Concatenating

I have a TextView in which I have to set a Text. But I'm adding (Concatenating) some text to it and I want only that concatenated text to get some color but not the whole textstoryLine=storyLine.substring(0,190)+" ...Click To Expand";. Here storyLine is a final text that is to be set in a textview. I just want change color of "Click To Expand".
String cntStr = " ...Click To Expand";
String storyLine = storyLine.substring(0,190);
String textLine = storyLine + cntStr;
Spannable spannable = new SpannableString(textLine);
spannable.setSpan(new ForegroundColorSpan(Color.RED), storyLine.length(), textLine.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
myTextView.setText(spannable, TextView.BufferType.SPANNABLE);
Happy Coding !!!!
You can try
String styledText = storyLine + "<font color='blue'>Click To Expand</font>";
textView.setText(Html.fromHtml(styledText), TextView.BufferType.SPANNABLE);
You can achieve by this :
SpannableStringBuilder builder = new SpannableStringBuilder();
String red = "Click To Expand";
builder.append("storyLine=storyLine.substring(0,190)+" ...");
SpannableString redSpannable= new SpannableString(red);
redSpannable.setSpan(new ForegroundColorSpan(Color.RED), 0, red.length(), 0);
builder.append(redSpannable);
mTextView.setText(builder, BufferType.SPANNABLE);
Happy coding!!
You can use SpannableString to color the sub string.
SpannableString span1=new SpannableString("Hello World...Click to Expand");
span1.setSpan(new ForegroundColorSpan(Color.BLUE), 15, 29, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
Textview.setText(span1,TextView.BufferType.SPANNABLE);
You can also concatenate
Textview.setText(TextUtils.concat(AnyString, span1));
Set Texview type spannable in Layout xml if needed.

how to change text color in the middle of the sentence in android

I need to change the color of the String which appears randomly in a sentence.
Ex: These following sentences are what I need to display.
hai #xyz how are you.
i am learning #abc android.
In this I have to change the color of the words "#xyz", "#abc" i.e, which starts with the character "#".
I used some string functions split(), subString(). but I am not getting what i need.
so, please guide me how to solve this.
Use SpannableString for ex:
SpannableString ss = new SpannableString("hai #xyz how are you.");
ss.setSpan(new ForegroundColorSpan(Color.RED), 4, 9, 0);
Try following to change color of each word with #:
String s="hai #xyz how are you.";
ForegroundColorSpan span = new ForegroundColorSpan(Color.RED);
SpannableString ss = new SpannableString(s);
String[] ss = s.split(" ");
int currIndex = 0;
for (String word : ss) {
if (word.startsWith("#")) {
ss.setSpan(span, currIndex,currIndex+ word.length(), 0);
}
currIndex += (word.length() + 1);
}
you can use this code:
t.setText(first + next, BufferType.SPANNABLE);
Spannable s = (Spannable)t.getText();
int start = first.length();
int end = start + next.length();
s.setSpan(new ForegroundColorSpan(0xFFFF0000), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
or you can use html:
String first = "This word is ";
String next = "<font color='#EE0000'>red</font>";
t.setText(Html.fromHtml(first + next));
You can easily achieve this using html tags
tv_message.setText(Html.fromHtml("<font color=\"#000000\">"+"Hi "+"</font>"+" "+"<font color=\"#EE0000\">"+"XYZ "+"</font>"+" "+"<font color=\"#000000\">"+"How are You ? " + "</font>"));

URLSpan in SpannableString

I am following this example in using SpannableString:
http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/text/Link.html
I am trying to create a string which has 'R.string.text1' following by R.string.text2 but R.string.text2 (has 10 characters) in URL format:
SpannableString ss = new SpannableString(getString(R.string.text1));
ss.setSpan(new URLSpan(getString(R.string.text2)), 0, 10, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
But what I am getting is
I don't see the string R.string.text2 at all
and only the first 10 characters is in URL format
How can I fix my problem?
This is a more general answer to show just a basic working example of a URLSpan.
// set up spanned string with url
SpannableString spannableString = new SpannableString("Click here for more.");
String url = "https://developer.android.com";
spannableString.setSpan(new URLSpan(url), 6, 10, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
// set to textview
textView.setText(spannableString);
textView.setMovementMethod(LinkMovementMethod.getInstance()); // enable clicking on url span
May i correct you ?
SpannableString ss = new SpannableString(getText(R.string.text1)+" "+getText(R.string.text2));
ss.setSpan(new URLSpan(getString(R.string.text2)),
getString(R.string.text1).length()+1,
ss.length(),
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
quite late, but at least done for the others ^^
you can also do it this way :
SpannableString ss1 = new SpannableString(getText(R.string.text1));
// do what you want with ss1
SpannableString ss2 = new SpannableString(getText(R.string.text2)); // the text visible
ss2.setSpan(new URLSpan(getString(R.string.text2)), // this is the link itself
0,
ss2.length(), // link on entire text
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
CharSequence csSS = TextUtils.concat(ss1, ss2); // import android.text.TextUtils;
try this way..
SpannableString ss = new SpannableString(getString(R.string.text1+""+R.string.text2));
ss.setSpan(new URLSpan(getString(R.string.text2)), 0, getString(R.string.text2).length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
In the kotlin, you can use like this way
fun SpannableStringBuilder.urlSpan(value: String): SpannableStringBuilder {
val start = length
this.append(value)
val end = length
this.setSpan(URLSpan(value), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
return this
}
SpannableStringBuilder().urlSpan("www.google.com")

Change text color of one word in a TextView

I am looking for a way to change the color of a text of a single word in a TextView from within an Activity.
For example, with this:
String first = "This word is ";
String next = "red"
TextView t = (TextView) findViewById(R.id.textbox);
t.setText(first + next);
How would I change the color of the next text to red?
Easiest way I know is to just use html.
String first = "This word is ";
String next = "<font color='#EE0000'>red</font>";
t.setText(Html.fromHtml(first + next));
But this will require you to rebuild the TextView when (if?) you want to change the color, which could cause a hassle.
t.setText(first + next, BufferType.SPANNABLE);
Spannable s = (Spannable)t.getText();
int start = first.length();
int end = start + next.length();
s.setSpan(new ForegroundColorSpan(0xFFFF0000), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
you have to use spannable this will also allows you to increase some text's size, make it bold etc.... even put in some image.
Use SpannableStringBuilder like this :
SpannableStringBuilder builder = new SpannableStringBuilder();
SpannableString str1= new SpannableString("Text1");
str1.setSpan(new ForegroundColorSpan(Color.RED), 0, str1.length(), 0);
builder.append(str1);
SpannableString str2= new SpannableString(appMode.toString());
str2.setSpan(new ForegroundColorSpan(Color.GREEN), 0, str2.length(), 0);
builder.append(str2);
TextView tv = (TextView) view.findViewById(android.R.id.text1);
tv.setText( builder, TextView.BufferType.SPANNABLE);
for long string you can use this:
String help = getString(R.string.help);
help = help.replace("some word", "<font color='#EE0000'>some word</font>");
txtDesc.setText(Html.fromHtml(help));
If you want to change the state of all the instances of a specific String inside a TextView text(case insensitive) you can use StringBuilders and SpannableString like this:
StringBuilder textBuilder = new StringBuilder(myTextView.getText().toString());
StringBuilder searchedTextBuilder = new StringBuilder((mySearchedString));
SpannableString spannableString = new SpannableString(myTextView.getText().toString());
int counter = 0;
int index = 0;
for (int i = 0;i < textBuilder.length() - mySearchedString.length() - 1;i++)
{
counter = 0;
if (Character.toLowerCase(textBuilder.charAt(i)) == Character.toLowerCase(searchedTextBuilder.charAt(index)))
{
counter++;
index++;
for (int j = 1,z = i + 1;j < mySearchedString.length() - 1;j++,z++)
{
if (Character.toLowerCase(textBuilder .charAt(z)) == Character.toLowerCase(searchedTextBuilder .charAt(index)))
{
counter++;
index++;
}
else
{
index++;
if (index % mySearchedString.length() == 0)
{
index = 0;
}
break;
}
}
if (counter == mySearchedString.length() - 1) // A match
{
spannableString.setSpan(new ForegroundColorSpan(Color.RED), i,
i + mySearchedString.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); // Do the change you want(In this case changing the fore ground color to red)
index = 0;
continue;
}
else
{
index = 0;
continue;
}
}
}
myTextView.setText(spannableString);
}
Store the whole TextView text inside a StringBuilder.
Store the searched string inside a StringBuilder.
Store the wholre TextView text inside a SpannableString
Make a simple operation to find all the String instances inside the TextView text and change them when reached.
Set the text value of the TextView to the SpannableString.
I implemented a utility function in Kotlin for my own usecase and maybe useful for someone else.
fun getCusomTextWithSpecificTextWithDiffColor(textToBold: String, fullText: String,
targetColor: Int) =
SpannableStringBuilder(fullText).apply {
setSpan(ForegroundColorSpan(targetColor),
fullText.indexOf(textToBold),
(fullText.indexOf(textToBold) + textToBold.length),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
}
How I am using it:
context?.let {
infoMessage.text = AppUtils.getCusomTextWithSpecificTextWithDiffColor(
wordAsBold,
completeSentence, ContextCompat.getColor(it, R.color.white))
}
USE:
makeTextBold("Your order is accepted","accepted", textView);
makeTextBold("Your order is canceled","canceled", textView);
Function:
public static void makeTextBold(String sentence, String word, AppCompatTextView textView) {
SpannableStringBuilder builder = new SpannableStringBuilder();
int startIndex = sentence.indexOf(word.toLowerCase().trim());
int endIndex = startIndex + word.toLowerCase().trim().length();
SpannableString spannableString = new SpannableString(sentence);
StyleSpan boldSpan = new StyleSpan(Typeface.BOLD);
spannableString.setSpan(boldSpan, startIndex, endIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); //To make text Bold
spannableString.setSpan(new ForegroundColorSpan(Color.RED), startIndex, endIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); //To change color of text
builder.append(spannableString);
textView.setText(builder, TextView.BufferType.SPANNABLE);
}
I think this is more readable
for coloring a word in a string
it is also probably more efficient a bit because you write once
String str = YOUR_STRING
Spannable s = new SpannableString(str);
int start = str.indexOf(err_word_origin);
int end = start + err_word_origin.length();
s.setSpan(new ForegroundColorSpan(Color.BLUE), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
YOUR_TEXT_VIEW.setText(s , TextView.BufferType.SPANNABLE);
my solution extension:
fun coloredText(
baseText: String,
coloredText: String,
targetColor: Int
): SpannableStringBuilder {
val transformText = "$baseText $coloredText"
return SpannableStringBuilder(transformText).apply {
setSpan(
ForegroundColorSpan(targetColor),
transformText.indexOf(coloredText),
(transformText.indexOf(coloredText) + coloredText.length),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
)
}
}
Usage
binding.mytextView.title = coloredText(
baseText = getString(R.string.my_title),
coloredText = getString(R.string.my_title_colored_part),
targetColor = ContextCompat.getColor(requireContext(), R.color.blue))
Iv'e found this best answer
https://stackoverflow.com/a/53573169/14250778
just changed one line to support also words that starts with uppercase letter
public void setHighLightedText(TextView tv, String textToHighlight) {
// added "toLowerCase()" to support words that starts with uppercase letter
String tvt = tv.getText().toString().toLowerCase();
int ofe = tvt.indexOf(textToHighlight, 0);
Spannable wordToSpan = new SpannableString(tv.getText());
for (int ofs = 0; ofs < tvt.length() && ofe != -1; ofs = ofe + 1) {
ofe = tvt.indexOf(textToHighlight, ofs);
if (ofe == -1)
break;
else {
// set color here
wordToSpan.setSpan(new BackgroundColorSpan(0xFFFFFF00), ofe, ofe + textToHighlight.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
tv.setText(wordToSpan, TextView.BufferType.SPANNABLE);
}
}
}

Categories

Resources