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
Related
I have a TextView that displays texts retrieved from db and some parts of the texts are marked as comment.
Here is sample.....
// CREATE A VARIABLE AT THE CLASS LIKE THIS...
private static int delay = 4000; //4Secs
/*NOW THE DELAY METHOD*/
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
// DECLARE ACTIONS TO BE DELAYED HERE...
}
}, delay);
Now i want all the single and multi line comment texts to have a different textColor. How do i go about this?
If you format the String with html's font-color property then pass it to the method Html.fromHtml(your text here)
String text = "<font color=#cc0029>First Color</font> <font color=#ffcc00>Second Color</font>";
yourtextview.setText(Html.fromHtml(text));
or You can prints lines with multiple colors without HTML as:
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("Text2");
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);
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
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.
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>"));
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.