I have same info String like... String info1 ="yyy"; String info2 ="mmm";
Now I want to disply it in Html Format and bold As..
Our First Information:yyy Our Second Information:mmm
I have same info String like... String info1 ="yyy"; String info2 ="mmm";
Now I want to disply it in Html Format and bold As..
Our First Information:yyy Our Second Information:mmm
Below is example for the same.
textview.setText(
Html.fromHtml(
"<b>" +info1 + "</b> " + "<font color=\"#000000\">" +
info2 +
"</font>"+
""));
assuming you're using a textview :
myTextView.setText(Html.fromHtml("<h2>"youString"</h2><br><p>"YourSecondString"</p>"));
String info1 ="yyy"; String info2 ="mmm";
etmsg.setText(Html.fromHtml("First Info <b>"+info1+"</b>"+"<br/>Second Info <b>"+info2+"</b>"));
First define your string in string.xml file which is under values folder in resources folder as:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="select"><b><h2>S</h2></b>elect</string>
</resources>
and then use them as android:text="@string/select"...
String info1 ="yyy";
String info2 ="mmm";
TextView txt;
txt = (TextView)findViewById(R.id.txt);
txt.setText(Html.fromHtml("<b>" + str1 + "</b>" + "<br />" +
"<small>" + str2 + "</small>" ));
Just pointing out that a SpannableStringBuilder may be an interesting alternative if you're looking for a non-HTML-based solution. This way you could for example also use an inline HTML-styled link (URLSpan) to not execute the default ACTION_VIEW on the provided URL, but have it launch a new activity - or anything else you like.
String boldText = getString(R.string.bold_text);
String italicText = getString(R.string.italic_text);
String strikethroughText = getString(R.string.strikethrough_text);
String urlText = getString(R.string.url_text);
SpannableStringBuilder ssb = new SpannableStringBuilder();
ssb.append(boldText);
ssb.setSpan(new StyleSpan(Typeface.BOLD), ssb.length()-boldText.length(), ssb.length() , Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
ssb.append(" ");
ssb.append(italicText);
ssb.setSpan(new StyleSpan(Typeface.ITALIC), ssb.length()-italicText.length(), ssb.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
ssb.append(" ");
ssb.append(strikethroughText);
ssb.setSpan(new StrikethroughSpan(), ssb.length()-strikethroughText.length(), ssb.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
ssb.append(" ");
ssb.append(urlText);
ssb.setSpan(new URLSpan("http://www.stackoverflow.com"), ssb.length()-urlText.length(), ssb.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
TextView tv = (TextView) findViewById(R.id.spannable_text);
tv.setText(ssb);
Which will look as follows:
Other spans can be found as subclass of CharacterStyle. E.g. the TextAppearanceSpan allows you to do most of the above but then by using xml-declared styles. I've used it to create a Button with two lines of text with different styles (different text size and colour) applied to each line.