Tuesday, September 2, 2014

WebView With HTML Script Demo

Ø  Create MainActivity.java class in src/package.


MainActivity.java


import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends Activity {

       private WebView webView1;
       private ProgressDialog pd;

       @SuppressLint("SetJavaScriptEnabled")
       @Override
       protected void onCreate(Bundle savedInstanceState) {
             super.onCreate(savedInstanceState);
             setContentView(R.layout.main);

             pd = ProgressDialog.show(this, "", "Loading...", true);

             webView1 = (WebView) findViewById(R.id.webView1);
             webView1.getSettings().setJavaScriptEnabled(true);
             webView1.getSettings().setSupportZoom(true);
             webView1.getSettings().setBuiltInZoomControls(true);
             webView1.setWebViewClient(new WebViewClient() {
                    @Override
                    public void onPageFinished(WebView view, String url) {
                           if (pd.isShowing() && pd != null) {
                                 pd.dismiss();
                           }
                    }
             });
             webView1.loadUrl("file:///android_asset/test.html");
       }
}


Ø  Create main.xml in res/layout.


main.xml


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

    <WebView
        android:id="@+id/webView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>


Ø  Create test.html in assets.




test.html


<head>
<title>Internal Script</title>
</head>
<body>
       <script type="text/javascript">
   document.write("Hello Javascript!")
</script>

</body>

1 comment:

  1. Online Casino Sites 2021 - Lucky Club
    What is online gambling? — Most casinos allow players to gamble at a range of real money casinos. You luckyclub.live can also play games on slots, roulette, and video poker. For

    ReplyDelete