I have two spannable object for same string and both have different styles. I need to merge all spannable objects and display all styles into TextView.
From one of returning from Html.fromHtml() and second return StyleSpan(Bold).
I tried TextUtils.concate(htmlSpan, boldSpan) but it displayed same string two times because it append spannable, i want to display my all styles into single string.
Use SpannableStringBuilder but you can create spans inside the append method
SpannableStringBuilder sb = new SpannableStringBuilder();
sb.append(String.valueOf(Html.fromHtml(stringToConvert));
sb.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 0, sb.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
And then just set sb to your view
textView.setText(sb);
EDIT:
I saw your edited question, Above I rewrote this code to get it to apply two spannables on one string. I think you cannot merge different spannable objects on one string. I believe a workaround would be to apply one span, then convert in to string and then apply the second span. This way it will apply two spans to one string. Or you can manage your bold text type via xml if you always need it in bold
Finally i found my solution, Html.fromHtml() not have spannable type parameter, so firstly i got SpannableStringBuilder from Html.fromHtml() and carry forward it into create Bold StyleSpannable method then set into TextView, and it works awesome.
It retain HTML spannable into bold StyleSpannable method.
Thanks guys for your feed back and response of my answer.
SpannableStringBuilder spannable = setTextViewHTML(content);
common.convertBoldString(spannable);
textView.setText(spannable);
For the above code content is a variable, which contain words which i like to bold and it also contain HTML tags.
convertBoldString is method in my Common methods class which has parameter
SpannableStringBuilder and this object use to set bold StyleSpan.
Related
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.
I am working on an android app i want to know how i can make few words in bold regular and few in italic for example i have a textview with a text HELLOO now I want to display the text like this in a text view
HELLOO
please tell me how to achieve this through styles in android??
Say, you have a TextView namely etx, use the following code:
final SpannableStringBuilder sb = new SpannableStringBuilder("HELLOO");
final StyleSpan bss = new StyleSpan(android.graphics.Typeface.BOLD); // Span to make text bold
final StyleSpan iss = new StyleSpan(android.graphics.Typeface.ITALIC);Span to make text italic
sb.setSpan(bss, 0, 4, Spannable.SPAN_INCLUSIVE_INCLUSIVE); // make first 4 characters Bold
sb.setSpan(iss, 4, 6, Spannable.SPAN_INCLUSIVE_INCLUSIVE); // make last 2 characters Italic
etx.setText(sb);
The main advantage of using this approach is that you can format text dynamically.
<b> is deprecated. use <strong> and <em> instead of <i>.
text1.setText(Html.fromHtml("<strong>bold text</strong> normal text <em>italic text</em> "));
If you have String in strings.xml then.
<string name="startup"><b><i>HELL</i></b><i>oo</i></string>
You can use HTML Tags Here inside <string> </string>
If you want to make it bold use the b tag.If you want it to be italic use i tag.If u want to make a word bold or italic put that single word inside the tag.
<resources>
<string name="register"> <u><b><i>SignUp</i></b></u> </string>
</resources>
String text="<html>"+"<b>"+"Hell"+"</b>"+"<i>"+"oo"+"</i>"+"</html>";
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.
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.
i want to put large text inside textView. The text has multiple section with multiple colours. i have seen Html.fromHtml() function and also know how to use setSpan(). But any of these not work for me. In my case i dont know the id of text View at runtime. As i am inflating different Views at runtime each View has many textViews. It will be better if i can find a way to set styles in strings.
Please some one help me out i have spend so much time here.
you can set id of text view weather the view is find by inflater
when you get text view using inflater then you can set id of that particular text view
and you can set textSize() and other attributes
This method creating spannables directly with the attributes you need will work.
Basically you can set colors like this, given a TextView called text:
String greeting = "Hello World!"
SpannableString str = SpannableString.valueOf(greeting);
str.setSpan(new ForegroundColorSpan(0xffffffff), 0, greeting.length(),
Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
str.setSpan(new BackgroundColorSpan(0xff0099ff), 0, greeting.length(),
Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
text.append(str);