Implementing Navigation Dorsum Characteristic To Android Webview Application

Posted on 18 views

By default, Navigation Back Feature is disabled inwards android WebView. You tin enable it past times Overriding onKeyDown method, too thus inwards this tutorial I volition learn y’all how to Implement Navigation Back Feature to Android WebView. To larn dorsum from your android WebView application, y’all postulate to press dorsum push from android device.


 too thus inwards this tutorial I volition learn y'all how to Implement Navigation Back Feature to Android  Implementing Navigation Back Feature to Android WebView Application

Implementing Navigation Back Feature to Android WebView Demo:

 

Use Following Code to Implement Navigation Back Feature:

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) {
            myWebView.goBack(); // Go to previous page
            return true;
        }
        // Use this every bit else part
        return super.onKeyDown(keyCode, event);
    }


Step 1: Create Android Project:

Create a novel android projection for Implementing Navigation Back Feature to Android WebView Application amongst next information:
Application name: Adding Navigation Back Feature to Android WebView
Company Domain: sirseni.com
Package name: com.sirseni.addingnavigationbackfeaturetoandroidwebview
Minimum SDK: Android 2.2 (API 8 Froyo)

Step 2: Adding WebView to Your activity_main.xml file:

res/layout/activity_main.xml
<WebView xmlns:android=”http://schemas.android.com/apk/res/android”
    android:id=”@+id/myWebView”
    android:scrollbars=”none”
    android:layout_width=”fill_parent”
    android:layout_height=”fill_parent” />

Step 3: Modify your MainActivity.java file:

src/MainActivity.java
/* Implementing Navigation Back Feature to Android WebView */
package com.sirseni.addingnavigationbackfeaturetoandroidwebview;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public shape MainActivity extends Activity {
    WebView myWebView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myWebView = (WebView) findViewById(R.id.myWebView);
        myWebView.loadUrl(“http://www.centerend.com”);
        WebSettings webSettings = myWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        myWebView.setWebViewClient(new MyWebViewClient());
    }
    private shape MyWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (Uri.parse(url).getHost().equals(“www.centerend.com”)) {
                return false;
            }
            Intent intent = novel Intent(Intent.ACTION_VIEW, Uri.parse(url));
            startActivity(intent);
            return true;
        }
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) {
            myWebView.goBack(); // Go to previous page
            return true;
        }
        // Use this every bit else part
        return super.onKeyDown(keyCode, event);
    }
}

Add the INTERNET Permission to Your AndroidManifest File

You must add together the INTERNET permission to your AndroidManifest.xml file to charge website inwards your application. So earlier running your WebView application, add together unmarried trouble code to your Android Manifest file.
<uses-permission android:name=”android.permission.INTERNET” />

Step 4: Modify AndroidManifest.xml file:

<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android”
    package=”com.sirseni.addingnavigationbackfeaturetoandroidwebview”>
    <uses-permission android:name=”android.permission.INTERNET” />
    <application
        android:allowBackup=”true”
        android:icon=”@mipmap/ic_launcher”
        android:label=”@string/app_name”
        android:theme=”@style/AppTheme”>
        <activity
            android:name=”.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>
Now run your Implementing Navigation Back Feature to Android WebView application past times only clicking Run icon. To encounter how Navigation Back Feature works, only click 1 of habitation concealment icon similar HTML, CSS, Bootstrap too for dorsum to habitation page y’all postulate to press dorsum push from your android device.

Simple Android WebView Example too Tutorial

How to Load HTML Data/String on Android WebView Application
Implementing Navigation Back Feature to Android WebView Demo:


Download Complete Example Project


Download complete Implementing Navigation Back Feature to Android WebView Application example projection origin code from GitHub.

READ  lenovo yoga tablet review in Hindi लिनोवो योगा एक बेहतरीन टेबलेट