Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get styled text from TextView

Quite simple problem:

  1. I have some TextView

    TextView textView = (TextView) findViewById(R.id.textView1);
    
  2. I set styled text (with <b> tags or any other tags)

    textView.setText(Html.fromHtml("Simple text. <b>Bold text</b>. Simple text."));
    

Problem: when I call textView.getText() I receive Simple text. Bold text. Simple text.. <b> tags have disappeared somehow.

How can I retrieve text from TextView with its formatting?

like image 966
kolobok Avatar asked Feb 17 '23 00:02

kolobok


1 Answers

@Wand Maker's assumption (see comments in question) was correct

Following code works:

TextView textView = (TextView) findViewById(R.id.textView1);
textView.setText(Html.fromHtml("Simple text. <b>Bold text</b>. Simple text."), TextView.BufferType.EDITABLE);
String almostSameText = Html.toHtml(tv.getEditableText()).toString();

The result is: <p>Simple text. <b>Bold text</b>. Simple text</p>

like image 86
kolobok Avatar answered Feb 28 '23 07:02

kolobok