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.
Related
How can i set a particular string as a title in Edittext. I want to output to look like something like this:
Title
Some other text.
I have tried to make the text bold and give it a larger style. Something like this:
s.setSpan(new StyleSpan(Typeface.BOLD), 0, length ,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
s.setSpan(new RelativeSizeSpan(1.2f), 0, length , Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
That does the trick but i wish if i could instead create a h1 tag around the string so the formatted html code i extract from Edittext can be displayed in textview or used elsewhere.
Update
It seems like a problem with Html class. Try something like this.
Log.d("Test", Html.toHtml(Html.fromHtml("<h1>Hello</h1>")));
You would get something like this in your logs:
<p dir="ltr"><b>Hello</b></p>
It seems like Html class is capable of converting h1 tags to spannable but not capable of being able to convert it back to h1 tags.
The fromHtml method converts the header tags to Spannable using a similar trick i already stated above. It's not implemented in the toHtml method and hence it don't create header tags from Spannable. Can someone try to fix this.
You can take a look at HTML.fromHtml(String text)
This will parse HTML and return a spanned string. The only thing notable is, that it's a bit tricky modifying the styles used for the spans.
Try something like this. Which worked for me
SpannableString text = new SpannableString(Html.fromHtml(<h1>Your content</h1>));
I'm trying to get back the text of a Button as a String, I set it previously:
button.setText(Html.fromHtml("<sup><small>1</small></sup>/<sub><small>8</small></sub>"));
but, the String returned from Html.toHtml() is:
<sup>1</sup>/<sub>8</sub>
How do I get the tag back?
To get the string:
SpannedString spannedString = new SpannedString(button.getText());
Html.toHtml(spannedString)
toHtml() does not handle <small> tags, nor any other tags that fromHtml() turns into RelativeSizeSpan objects. You would have to write your own Spanned-to-HTML conveter.
TextView tv = (TextView) findViewById(R.id.text);
tv.setText(Html.fromHtml(getResources().getString(R.string.test)));
Define string 'test' in strings.xml
<string name="test"><![CDATA[<sup><small>1</small></sup>/<sub><small>8</small></sub>]]></string>
In your case simple put button.setText(Html.fromHtml(getResources().getString(R.string.test))); It worked for me.
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
I have a string defined in string.xml like
<string name="eventtitle">Title: %1$s </string>
which is formatted using string.format . How to define the string to get only the Titel: as Bold.
Thanks in advance for the help
You can do it like,
textView.setText(Html.fromHtml("<b>Title</b>: Text"));
If you have text in dynamic way..
And to define formatings in Strings.xml you can do like,
<string name="text1">This text uses <b>bold</b> and <i>italics</i>
by using inline tags such as <b> within the string file.</string>
see This Link
Now, it is reasonably supported by Android.
you can define the string in xml as <string name="text_to_show">Hey <b>This is in bold</b>
Then in code, use this to convert to CharSequence and then use it for e.g in TextView
String text = getResources().getString(R.string.text-to_show);
CharSequence styledText = Html.fromHtml(text);
textview.setText(styledText);
Actually, many of the answers are obsolete. After researching by myself, what I've found out that the best answer is one by #Wahib. Here's the improved version:
Define the string resource as a:
<string name="styled_text">Hey, <b>this is bold</b> text</string>
Use the resource like this:
String text = getResources().getString(R.string.styled_text);
CharSequence styledText = HtmlCompat.fromHtml(text, HtmlCompat.FROM_HTML_MODE_LEGACY);
textview.setText(styledText);
Here's the result:
You can use HTML markup like "<b>BOLD</b> other text ...".
See this Google resource for more information.
You have to style your string in the TextView in which in is displayed. See this link
Typeface tfaerial=Typeface.createFromAsset(getAssets(),"fonts/aerial.ttf");
Typeface tfTradeGothicLight=Typeface.createFromAsset(getAssets(), "fonts/TradeGothic-Light.OTF");
String strt_title_desc=this.getResources().getString(R.string.eventtitle);
int upto=strt_title_desc.indexOf(":"); //of you can specify 5
if (strt_title_desc!=null)
{
aboutAuthTV.setTextColor(Color.BLACK);
aboutAuthTV.setLineSpacing(1.2f, 1.5f);
aboutAuthTV.setTextSize(23);
SpannableString SS = new SpannableString(strt_title_desc);
SS. setSpan ( new StyleSpan(tfTradeGothicLight.getStyle()), 0, upto,Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
SS. setSpan ( new StyleSpan(tfaerial.getStyle()), upto, strt_dialog_desc.length(),Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
yourtextView.setText(SS);
}
// this for changing the font size, style and colors
String str="<font size =\"20\"><B>Bold</B> <br/> Then Normal Text<br/>
<i>Then Italic</i> </font>" +
"<br/> <font color=\"green\" >this is simple sentence </font>" +
"<br/><br/><br/><br/><a>this is simple sentence</a>";
Spanned strHtml= Html.fromHtml(str);
TextView tv = (TextView)findViewById(R.id.textView);
tv.setText(strHtml);
Now, it is reasonably supported by Android.
I have already given answer to this how to add styling within a string
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.