Tuesday, 1 September 2015

Android Programming Tutorial 49 : WebView In Android

Create Android Application:

  • File >> New >> Android Application
  • Enter Project name: WebViewApp
  • Package: com.ambilpursunil.newapp
  • Keep other default selections, click Next until you reach Finish

                                   

     1.Simply Copy and Past the Code which is display below in the  MainActivity.java 

    package com.ambilpursunil.newapp;

    import android.app.Activity;
    import android.graphics.Bitmap;
    import android.os.Bundle;
    import android.view.KeyEvent;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.webkit.WebChromeClient;
    import android.webkit.WebChromeClient.CustomViewCallback;
    import android.webkit.WebView;
    import android.webkit.WebViewClient;
    import android.widget.FrameLayout;

    public class MainActivity extends Activity {
                private WebView webView;
                private FrameLayout customViewContainer
    private WebChromeClient.CustomViewCallback customViewCallback;
                private View mCustomView;
                private myWebChromeClient mWebChromeClient;
                private myWebViewClient mWebViewClient;

                @Override
                public void onCreate(Bundle savedInstanceState) {
                            super.onCreate(savedInstanceState);
                            setContentView(R.layout.activity_main);
                            customViewContainer = (FrameLayout)                                                                       findViewById(R.id.customViewContainer);
                            webView = (WebView) findViewById(R.id.webView);

                            mWebViewClient = new myWebViewClient();
                            webView.setWebViewClient(mWebViewClient);

                            mWebChromeClient = new myWebChromeClient();
                            webView.setWebChromeClient(mWebChromeClient);
                            webView.getSettings().setJavaScriptEnabled(true);
                            webView.getSettings().setAppCacheEnabled(true);
                            webView.getSettings().setBuiltInZoomControls(true);
                            webView.getSettings().setSaveFormData(true);
                            webView.loadUrl("https://ambilpursunil.blogspot.com/");
                }

                public boolean inCustomView() {
                            return (mCustomView != null);
                }

                public void hideCustomView() {
                            mWebChromeClient.onHideCustomView();
                }

                @Override
                protected void onPause() {
                            super.onPause();
                            webView.onPause();
                }

                @Override
                protected void onResume() {
                            super.onResume();
                            webView.onResume();
                }

                @Override
                protected void onStop() {
                            super.onStop();
                            if (inCustomView()) {
                                        hideCustomView();
                            }
                }

                @Override
                public boolean onKeyDown(int keyCode, KeyEvent event) {
                            if (keyCode == KeyEvent.KEYCODE_BACK) {
                            if (inCustomView()) {
                            hideCustomView();
                            return true;
                            }
                            if ((mCustomView == null) && webView.canGoBack()) {
                            webView.goBack();
                            return true;
                            }
                            }
                            return super.onKeyDown(keyCode, event);
                             }
                           class myWebChromeClient extends WebChromeClient {
                           private Bitmap mDefaultVideoPoster;
                           private View mVideoProgressView;
                           @Override
    public void onShowCustomView(View view, int requestedOrientation,
                           CustomViewCallback callback) {
                            onShowCustomView(view, callback);
                            }
                          @Override
    public void onShowCustomView(View view, CustomViewCallback callback) {
                           if (mCustomView != null) {
                           callback.onCustomViewHidden();
                           return;
                           }
                           mCustomView = view;
                           webView.setVisibility(View.GONE);
                           customViewContainer.setVisibility(View.VISIBLE);
                           customViewContainer.addView(view);
                           customViewCallback = callback;
                           }

                           @Override
                           public View getVideoLoadingProgressView() {
                           if (mVideoProgressView == null) {
                           LayoutInflater inflater = LayoutInflater
                           .from(MainActivity.this);
      mVideoProgressView = inflater.inflate(R.layout.video_progress,
                           null);
                           }
                           return mVideoProgressView;
                           }
                          @Override
                          public void onHideCustomView() {
                          super.onHideCustomView();
                          if (mCustomView == null)
                          return;
                         webView.setVisibility(View.VISIBLE);
                         customViewContainer.setVisibility(View.GONE);
                         mCustomView.setVisibility(View.GONE);
                         customViewContainer.removeView(mCustomView);
                         customViewCallback.onCustomViewHidden();
                         mCustomView = null;
                         }
                         }
                        class myWebViewClient extends WebViewClient {
                        @Override
     public boolean shouldOverrideUrlLoading(WebView view, String url)                                               {
                        return super.shouldOverrideUrlLoading(view, url);
                        }
                }
       } 


    2.Simple Copy and Past the below code:  actvitiy_main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  android:orientation="vertical"
                  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_gravity="center"/>
        <FrameLayout
                android:id="@+id/customViewContainer"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:visibility="gone"/>
    </LinearLayout>

    3.Copy and past the code for manifest.xml

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.ambilpursunil.newapp "
        android:versionCode="1"
        android:versionName="1.0" >

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

        <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.ambilpursunil.newapp.MainActivity"
                android:configChanges="orientation|keyboardHidden"
                android:hardwareAccelerated="true"
                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>


    4.Right click on the project and Run As-->Android Application


    OutPut:

    When our application launch data will be visible on the screen.


    Please Send Your Comments To ambilpura.sunil@gmail.com




    Stay Tune For Next Tutorial...  Google Maps With Lat and Lng In Android:

    No comments:

    Post a Comment