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

1
  • what do you mean in html format? you want to display it in a WebView? as HTML? Commented Nov 4, 2011 at 6:30

6 Answers 6

1

Below is example for the same.

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

                    ""));
Sign up to request clarification or add additional context in comments.

Comments

0
String info1 ="yyy"; String info2 ="mmm"; 
etmsg.setText(Html.fromHtml("First Info <b>"+info1+"</b>"+"<br/>Second Info <b>"+info2+"</b>")); 

2 Comments

How to write if we want both info in different line
@AlamAbsar check updated answer..will need a UpVote for that..ha ha :)
0

assuming you're using a textview :

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

Comments

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

Comments

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>" ));

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.