I'm trying to show HTML string with TextView on my android App. I'm curious about that how could I make it possible to show HTML with inline style via TextView. There're some reason that we can't use WebView for this. Is there any repository on GitHub that you know it works?
Thanks for helping.
Just for your concern, we need to run it perfectly on API 15.
You need to use Html.fromHtml() to use HTML in your textView
as a text
example :
textView.setText(Html.fromHtml("<h2>Title</h2><br><p>Description here</p>"));
I don't remember the source that I get , but I made this way:
// get our html content
String htmlAsString = getString(R.string.html);
Spanned htmlAsSpanned = Html.fromHtml(htmlAsString); // used by TextView
// set the html content on the TextView
TextView textView = (TextView) findViewById(R.id.textView);
textView.setText(htmlAsSpanned);
html string on a xml file :
<resources>
<string name="app_name">TestProject2</string>
<string name="html">
<![CDATA[
<h1>Main Title</h1>
<h2>A sub-title</h2>
<p>This is some html. Look, here\'s an <u>underline</u>.</p>
<p>Look, this is <em>emphasized.</em> And here\'s some <b>bold</b>.</p>
<p>This is a UL list:
<ul>
<li>One</li>
<li>Two</li>
<li>Three</li>
</ul>
<p>This is an OL list:
<ol>
<li>One</li>
<li>Two</li>
<li>Three</li>
</ol>
]]>
</string>
</resources>
Defining on textview:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"
android:gravity="top|fill_vertical">
<TextView
android:id="@+id/textView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
<!--
<WebView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/webView"
android:layout_below="@+id/helloWorld"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
/>
-->
</LinearLayout>
And on main activity
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.text.Html;
import android.text.Spanned;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// get our html content
String htmlAsString = getString(R.string.html); // used by WebView
Spanned htmlAsSpanned = Html.fromHtml(htmlAsString); // used by TextView
// set the html content on a TextView
TextView textView = (TextView) findViewById(R.id.textView);
textView.setText(htmlAsSpanned);
// WebView webView = (WebView) findViewById(R.id.webView);
// webView.loadDataWithBaseURL(null, htmlAsString, "text/html", "utf-8", null);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.