0

want put html code like code tutorial description so i want to show html code in activity like given website link below

At the end of the HTML tutorial, you can find more than 200 examples.

With our online editor, you can edit and test each example yourself.

2 Answers2

1

add below library in your build.gradle file

//for html to text
compile 'org.jsoup:jsoup:1.8.3'

and use below method to get your html code as text in android.

public String html2text(String html) {
    try {
        return Jsoup.parse(html).text();
    } catch (Exception ignored) {
        return "";
    }
}

String myhtmlcode = "<p>its my html code</p>";

yourTextView.settext(html2text(myhtmlcode));
Dhiren Basra
  • 820
  • 9
  • 24
0

i want to show html code in a textview then click on button show output of html code......... – Sachin Srivastava

add below library in your build.gradle file

//for html to text
compile 'org.jsoup:jsoup:1.8.3'

and

TextView txt_htmltext;
Button btn;

txt_htmltext = (TextView) findViewById(R.id.txt_htmltext);
btn = (Button) findViewById(R.id.btn);

txt_htmltext.setText(R.string.temp_html_text);

btn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        txt_htmltext.setText(html2text(txt_htmltext.getText().toString()));
    }
});

public String html2text(String html) {
    try {
        return Jsoup.parse(html).text();
    } catch (Exception ignored) {
        return "";
    }
}

res/values/string.xml:

<string name="temp_html_text"><![CDATA[<!DOCTYPE html> <html> <head> <title>Page Title</title> </head> <body> <h1>This is a Heading</h1> <p>This is a paragraph.</p> </body> </html>]]></string>

XML code:

<LinearLayout
    android:id="@+id/layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="vertical"
    android:padding="3sp">

    <TextView
        android:id="@+id/txt_htmltext"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center|left"
        android:text="@string/temp_html_text"/>

    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="html to text"/>

</LinearLayout>
Dhiren Basra
  • 820
  • 9
  • 24