Top Menu

Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Monday 26 October 2015

Display HTML format text in WebView

I have faced this problem to show the HTML format text show in WebView
I found the solution for this and lets share it -

Create a xml with WebView
<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <WebView
        android:id="@+id/webview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

</LinearLayout>

Now create the Activity
package com.example.androidhtmltextsample;

import android.os.Bundle;
import android.app.Activity;
import android.webkit.WebView;

public class MainActivity extends Activity {

 WebView webView;
 
 String mimeType = "text/html";
 String encoding = "utf-8";
 String htmlText = "<ul>\n<li>Apple cider vinegar dries acne without causing any apparent discomfort.</li>" +
        "\n<li>Another interesting acne overnight treatment is the application of toothpaste.</li>" +
        "\n<li>Follow this up by applying a bit of alcohol over the affected area just prior to bedtime.</li>" +
        "\n<li>Do not apply gel based toothpastes on the affected areas.</li>\n</ul>";
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  
  webView = (WebView)findViewById(R.id.webview);
  webView.loadData(htmlText, mimeType, encoding);
 }

}




Now add the Internet Permission in Menifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"    
package="com.example.androidhtmltextsample"    
android:versionCode="1"    
android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8"        
              android:targetSdkVersion="23" />

    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

    <application android:allowBackup="true"        
                 android:icon="@drawable/ic_launcher"        
                 android:label="@string/app_name"       
                 android:theme="@style/AppTheme" >
        <activity android:name="com.example.androidhtmltextsample.MainActivity"         
                  android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        
    </application>

</manifest>

No comments:

Post a Comment