Is there any control in android which we can use as a rich text box which can handle formatting of characters such as bold, italic etc. and use fonts and color ?
If anyone having any link or source related to the above information please share it with me.
SpannableString class or HTML.fromHtml() allows you to manipulate different styles in actual string. See the links below:
http://developer.android.com/reference/android/text/SpannableString.html
http://developer.android.com/reference/android/text/Html.html#toHtml(android.text.Spanned)
Sample SpannableString and TextView implementation:
TextView tv = new TextView;
String text = "String String String String body";
SpannableString spannableStr = new SpannableString(text);
spannableStr.setSpan(new StyleSpan(Typeface.BOLD), 0 , 10, 0);
tv.setText(spannableStr);
WebView is the closest that I can think of, but it is super duper heavy unless you want the entire screen to be a webview.
Related
i'm writing an application which consists some code snippets.
as the usual manner it can be displayed in TextView but i want to highlight some keywords say "main" , "int" , "class", "return" , etc.
after lots of time i figured out it can be done using html and css that loads to webview up , but it has some disadvantages like slow rendering so it makes bad user Experience .
is there a better way to approach the solution ?
it would be great to give me some code example...
thanks
Use SpannableString:
SpannableString string = new SpannableString("Your text that has to be highlighted");
string.setSpan(new BackgroundColorSpan(Color.YELLOW), 0, 10, 0);
textView.setText(string);
BackgroundColorSpan changes the background color. Also check out ForegroundColorSpan.
Or use Html.fromHtml which accepts Html tags:
String text1 = "Start your text ";
String text2 = "<font color='#EE0000'>and it is now red</font>";
textView.setText(Html.fromHtml(text1 + text2));
Html way might be a bit slower than SpannableString as it involves parsing the string.
Edit:
my app allows users to post comments for all other users to see. I want to allow users to make certain words in their comment bold or italic etc, so that when they post a comment other people will also be able to see the bold words etc.
At the moment i am able to make the words bold but when the text is saved to my remote database server it is saved as normal text and i am unable to find out which words were bold and which arn't.
How can I preserve the bold/italic/underlined... formatting when saving the text from an EditText?
There is one way to achieve this.
You can store HTML content and display HTML content in TextView.
Just look at this Answer.
Here is Sample link.
Quite easily.
Use EditText.getText to get the text, which implements Spanned interface.
Then you need to somehow mark bold parts within text, for this you may use good old HTML format, which means that you'll end up with text (storable in database) with some formatting.
Use Html.toHtml to turn Spanned to (HTML) String. Save result to DB or where you need.
Reverse process is to get your HTML String and convert it to Spanned, then set it bact to EditText. Use Html.fromHtml to accomplish this.
final SpannableStringBuilder sb = new SpannableStringBuilder("Text from the edit Text");
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, 10, Spannable.SPAN_INCLUSIVE_INCLUSIVE); // make first 10 characters Bold
sb.setSpan(iss, 10, 20, Spannable.SPAN_INCLUSIVE_INCLUSIVE); // make last 10 characters Italic
//Set text or save it in db
etx.setText(sb);
I thoroughly searched on SO but didnt get answer of my question.
I want to set a paragraph, I will set it in XML using
The text contains Title and Steps and regular text. I want to make the Title and Steps in bold and rest in normal text.
I can do this by using different 's but how can I do it in the same TextView.
I mean using the same TextView how can I set different attributes for different sentences?
Use a Spannable String
TextView tv = (TextView) findViewById(R.id.tv);
String steps = "Hello Everyone";
String title="Bold Please!";
SpannableString ss1= new SpannableString(title);
ss1.setSpan(new StyleSpan(Typeface.BOLD), 0, ss1.length(), 0);
tv.append(ss1);
tv.append("\n");
tv.append(steps);
For more styling check the link # http://blog.stylingandroid.com/archives/177
in your strings file
<string name="your_text">
<![CDATA[
<p> <b>Title</b> </p>
<p><b><i>Step 1</i></b><p>step1 content content content content content content</p></p>
<p><b><i>Step 2</i></b><p>step2 content content content content content content content</p></p>
]]>
</string>
Then in your activity
TextView tv=(TextView)findViewById(R.id.textView1);
tv.setText(Html.fromHtml(getString(R.string.your_text)));
And output
You can format it like you would in HTML: let's call this custom_text
<b>Your title here</b>
This is the non-bolded stuff.
And then load the text using the Html class:
mTextView.setText(Html.fromHtml(getString(R.string.custom_text)));
That will create a spannable string and set it on the TextView.
please put this string in to res->string.xml
<string name="your_html">
<![CDATA[p><b>This is bold text</b> rementing is simple text
]]>
</string>
Now you can used whenever you have to require this thing.
tv.setText(Html.fromHtml(getString(R.string.your_html)));
It is bestway and work charm.
TextViews support SpannableStrings. You can either make your custom String or format your string in html and then set it with tv.setText(Html.fromHtml(yourString));
kotlin Solution :
For me, the issue was I had a text set in XML through data binding. I had to remove that. then this started taking effect
val spannableStringBuilder = SpannableStringBuilder(headerText)
spannableStringBuilder.setSpan(StyleSpan(Typeface.BOLD), 0, headerText.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
spannableStringBuilder.append(" ")
spannableStringBuilder.append(subText)
textView.text = spannableStringBuilder
The best way to do this is in the Strings resources.
<string name="sample_string"><![CDATA[<b>Title</b>StackOverflow]]></string>
Notice, the text between bold tag (<b> </b>) will appear in bold. Similarly, you can set other styles. For eg. <i> for italics and <u> for underline.
Hope this helps, good luck!
Instead, use this :
First, declare textView
TextView tv1 = (TextView) getActivity().findViewById(R.id.t1);
Then set it's text to the string you want
tv1.setText(Html.fromHtml(getString(R.string.my_text)));
Lastly, use set the typeface for your textView
tv1.setTypeface(null, Typeface.BOLD);
And, you are done.
String = " This is SAMPLE"
I want to increase the size of the SAMPLE alone in String to highlight, but need to be in a single string, this is to set in TextView.
Thanks in Advance.
textView.setText(Html.fromHtml("This is <font color='#707070' size='20'>
SAMPLE</font>"));
Try this:
String str = "This is <b>SAMPLE</b>";
yourTextView.setText(Html.fromHtml(str), BufferType.SPANNABLE);
Moreover, you may decorate your string (str) with html tags to alter the looks.
Or to highlight a part of text without using html stuff, read this
Use Html.fromHtml() in setText() of TextView. Supported Html Tags can be found here Html Tags Supported by TextView
I think this is possible if you pass the text as html with your highlighted part enclosed in html <bold> tag.
You have two options:
1, Format your string in HTML and use HTML.from("Your text");
2, Use spannable. A complete guide can find link here
Is there any way to tell linkify not to underline my links? I am already using textView.setLinkTextColor(stateList) to change the color, but I'd like to remove the underline that is inserts on the link.
Is this even possible?
TextView tv = new TextView(this);
SpannableString ss = new SpannableString(
""+getString(R.string.nonlinktext)+"\n"+getString(R.string.linkedtext)+"");
ss.setSpan(new URLSpan(getString((R.string.linkedtext))), ss.length() - numberofcharactersinlink, ss.length(),Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
I managed to get this working using this implementation. A bit of a hack but it works all the same.