How to set color of texts Concatenating - android

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.

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 red color to the text view in android

String records = "<font color='red'>"+edittext.getText().toString()+"</font>.";
textView.setText("Total Records to be SYNC : "+Html.fromHtml(records) +"\nDo you want sync all records...!", TextView.BufferType.SPANNABLE);
here its not displayed in red color
I think you are doing it in wrong way. Check out this answer, it may help you.
How to set the text color of TextView in code?
A variation using just standard color code:
android:textColor="#ff0000"
Add it in XML of TEXTVIEW
or use
textview.setTextColor(color);
SpannableStringBuilder builder = new SpannableStringBuilder();
String red = edittext.getText().toString();
SpannableString redSpannable= new SpannableString(red);
redSpannable.setSpan(new ForegroundColorSpan(Color.RED), 0, red.length(), 0);
builder.append(redSpannable);
mTextView.setText("Total Records to be SYNC : "+builder+"\nDo you want sync all records...!", BufferType.SPANNABLE);
You can do with following code, I have tested and It's working perfect.
String value1 = "Total Records to be SYNC : ";
String value2 = "\nDo you want sync all records...!";
String valueFromEdittext = edittext.getText().toString();
String finalValue = value1 + valueFromEdittext + value2;
Spannable WordtoSpan = new SpannableString(finalValue);
WordtoSpan.setSpan(new ForegroundColorSpan(Color.RED), value1.length(), (value1 + valueFromEdittext).length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
mTextView.setText(WordtoSpan);

Set color to string programatically in android?

I have bunds of strings to load into listview what i need to set text color to string
I google lot but i got only
textView.setTextColor(getResources().getColor(R.color.red));
im not using textview i juz load strings directly into listview
Somebody suggested to do the follow
String s="Hello World";
SpannableString ss= new SpannableString(s);
ss.setSpan(new ForegroundColorSpan(Color.GREEN), 0, 5, 0);
but its not working for me
pls any suggestion Thanx in advance
StringBuilder sb = new StringBuilder();
sb.append(" <font color='red'>");
sb.append("Hello World");
sb.append("</font>");
textView.setText(Html.fromHtml(sb.toString()));
I hope this will help you.
The code below set the text in the TextView to "Hello World" where "Hello" is red and "World" is green.
TextView myTextView = new TextView(this);
SpannableString myStr1 = new SpannableString("Hello");
SpannableString myStr2 = new SpannableString("World");
myStr1.setSpan( new ForegroundColorSpan(Color.RED), 0, myStr1.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE );
myStr2.setSpan( new ForegroundColorSpan(Color.GREEN), 0, myStr2.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE );
myTextView.setText(TextUtils.concat(myStr1, " ", myStr2);
If you wants to change the color of textview text means use like this
textView.setTextColor(Color.parseColor("#123123")); // give your own text color here
It will work.

Android - is there a way to edit color in a part of a string that is displayed?

I have a part of the code that calls a toString on an object and the toString basically spits out the string that will be shown on the screen.
I would like to change the color of one part of the string.
I just tried something like this:
String coloredString = solutionTopicName + " (" + commentCount + ")";
Spannable sb = new SpannableString( coloredString );
ForegroundColorSpan fcs = new ForegroundColorSpan(Color.rgb(140, 145, 160));
sb.setSpan(new ForegroundColorSpan(Color.BLUE), solutionTopicName.length(), solutionTopicName.length()+3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
return sb.toString();
But I am still seeing the same colors appear without change.
Thanks!
yes, it is possible
use Spannable to do that.
setSpan() will do the job.
Spannable mString = new SpannableString("multi color string ");
mString.setSpan(new ForegroundColorSpan(Color.BLUE), 5, 7, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
Update:
String coloredString = " (" + solutionTopicName + commentCount + ")";
Spannable sb = new SpannableString( coloredString );
ForegroundColorSpan fcs = new ForegroundColorSpan(Color.rgb(140, 145, 160));
sb.setSpan(new ForegroundColorSpan(Color.BLUE), solutionTopicName.length(), solutionTopicName.length()+3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
//textView.setText(sb);
return sb;
Latest:
String solutionTopicName= "Hello";
String commentCount= "hi how are you";
String coloredString = solutionTopicName+"(" + commentCount + ")";
Spannable sb = new SpannableString( coloredString );
sb.setSpan(new ForegroundColorSpan(Color.BLUE), coloredString.indexOf("("), coloredString.indexOf(")")+1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
mTextView.setText(sb);
I would like to change the color of one part of the string. Is that at all possible?
yes, you can change color of specific part of string. There is a SpannableStringBuilder class you can make use of it.
you can do some thing like this.
SpannableStringBuilder sb = new SpannableStringBuilder("This is your String");
ForegroundColorSpan fcs = new ForegroundColorSpan(Color.rgb(140, 145, 160));
sb.setSpan(fcs, 0, 10, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
textView.setText(sb);
here is the definition of setspan method.
setSpan(Object what, int start, int end, int flags)
Mark the specified range of text with the specified object.
setSpan method takes indices as parameter and an instance of Object Class. so any class' instance could be passed in as parameter depending upon your requirement. we used instance of ForegroundColorSpan to change color of text. you can pass Clickable instance to make certain part of string clickable.

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")

Categories

Resources