0

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

6 Answers6

1

Below is example for the same.

textview.setText(
                Html.fromHtml(
                        "<b>" +info1 + "</b> " + "<font color=\"#000000\">" +
                        info2 + 
                        "</font>"+

                    ""));
Maneesh
  • 6,098
  • 5
  • 36
  • 55
0

assuming you're using a textview :

myTextView.setText(Html.fromHtml("<h2>"youString"</h2><br><p>"YourSecondString"</p>"));

Quentin DOMMERC
  • 876
  • 1
  • 8
  • 24
0
String info1 ="yyy"; String info2 ="mmm"; 
etmsg.setText(Html.fromHtml("First Info <b>"+info1+"</b>"+"<br/>Second Info <b>"+info2+"</b>")); 
MKJParekh
  • 34,073
  • 11
  • 87
  • 98
0

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"...

Prashant Kumar
  • 134
  • 1
  • 2
  • 9
0
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>" ));
atrivedi
  • 348
  • 1
  • 4
  • 12
0

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.

Community
  • 1
  • 1
MH.
  • 45,303
  • 10
  • 103
  • 116