Dynamically create an HTML string and load it into the WebView

Description

The following code shows how to dynamically create an HTML string and load it into the WebView.

Example

Main layout xml file


      <?xml version="1.0" encoding="utf-8"?>
      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
            android:orientation="vertical" >

        <WebView android:id="@+id/webview1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

      </LinearLayout>

In the MainActivity.java file, add the following statements:


        import android.app.Activity;
        import android.os.Bundle;
        import android.webkit.WebSettings;
        import android.webkit.WebView;
/*  w w  w  .jav  a2  s.co m*/
        public class MainActivity extends Activity {
            @Override
            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);

              WebView wv = (WebView) findViewById(R.id.webview1);
              final String mimeType = "text/html";
              final String encoding = "UTF-8";
              String html = "<H1>A simple HTML page</H1><body>" +
                  "<p>The quick brown fox jumps over the lazy dog</p>" +
                  "</body>";
              wv.loadDataWithBaseURL("", html, mimeType, encoding, "");

            }
        }
Dynamically create an HTML string and load it into the WebView




















Home »
  Android »
    Android UI »




UI Basics
Action Bar
Animation
Button
Canvas
CheckBox
Clock Date Picker
Dialog
EditText
Event
Fragment
Gesture
GridView
ImageView
Layout
ListView
Map
Menu
Model
OpenGL
ProgressBar
RadioButton
Spinner
Tab
TextView
Thread
Toast
Video
View
WebView