Skip to content Skip to sidebar Skip to footer

Enable Back Button In Webview

I am just starting out with the Android SDK. I wanted to make a simple app with WebView that displays local web-pages only. I have used examples I have found on the net to make it.

Solution 1:

Override onKeyDown(params), check if the key pressed is back button ,check if the webview can navigate to previous page, if so naviagate to previous page. If there is no web page to display you can finish the actiivty

You can use the below

@OverridepublicbooleanonKeyDown(int keyCode, KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) { 
            //if Back key pressed and webview can navigate to previous page
        webView.goBack();
            // go back to previous pagereturntrue;
    }
    else
    {
        finish();
           // finish the activity
    }
    returnsuper.onKeyDown(keyCode, event);
}

Edit:

Remove the scrollview in xml layout. Try the below

<LinearLayoutxmlns: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"tools:context=".MainActivity" ><WebViewandroid:layout_width="fill_parent"android:layout_height="fill_parent"android:id="@+id/wv"></WebView></LinearLayout>

Edit:2

Here's a sample that works. Tested on samsung galaxy s3

activtiy_main.xml

<RelativeLayout 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"tools:context=".MainActivity" >

<WebView
android:layout_width="fill_parent"android:layout_height="fill_parent"android:layout_centerHorizontal="true"android:layout_centerVertical="true"android:id="@+id/wv"/>

</RelativeLayout>

MainActivity

publicclassMainActivityextendsActivity {
 private WebView webView;

@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


        setContentView(R.layout.activity_main);
        webView = (WebView) findViewById(R.id.wv);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.setWebChromeClient(newWebChromeClient() {
               publicvoidonProgressChanged(WebView view, int progress) {
                MainActivity.this.setProgress(progress * 1000);
               }
             });
             webView.setWebViewClient(newWebViewClient() {
               publicvoidonReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                 Toast.makeText(MainActivity.this, "Oh no! " + description, Toast.LENGTH_SHORT).show();
               }
             });

             webView.loadUrl("http://slashdot.org/");

}   



@OverridepublicbooleanonKeyDown(int keyCode, KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {
        webView.goBack();
        returntrue;
    }
    else
    {
        finish();
    }
    returnsuper.onKeyDown(keyCode, event);
}
}

Post a Comment for "Enable Back Button In Webview"