Search code examples
androidwebviewappbar

Android - Clicking on a WebView makes AppBar disappear.


I am trying to allow WebView call in my application. In this call they should be able to browse to another page. I am also trying to keep a consistent AppBar for the application.

I have the .xml defined as:

<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"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="com.realpayment.app.activities.CalledAction"
android:background="@color/c_black">

<android.support.v7.widget.Toolbar
    android:id="@+id/my_toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:elevation="4dp"
    android:theme="@style/JupiterTheme"
    app:popupTheme="@style/ThemeOverlay.AppCompat"/>
<WebView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/calledAction"
    android:background="@color/c_black"
    android:layout_margin="15px"
    android:layout_below="@+id/my_toolbar"
    android:textColor="@color/c_white"
    />

In my code I am calling (I am playing with the options and found these in another SO post) but I starting with just the first and last line and see the same issue (I know in that case you can't click, but I accidentally had a redirect in my link):

    WebView browser = (WebView) findViewById(R.id.calledAction);
    browser.setFocusable(true);
    browser.setFocusableInTouchMode(true);
    browser.getSettings().setJavaScriptEnabled(true);
    browser.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
    browser.getSettings().setDomStorageEnabled(true);
    browser.getSettings().setDatabaseEnabled(true);
    browser.getSettings().setAppCacheEnabled(true);
    browser.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
    browser.loadUrl(sCallToActionLink);

This loads the link perfectly and my action bar is displayed. The up button takes me to the parent activity and the back button takes me to the last activity called. My problem is that if I click a link, it opens up a new screen with its own AppBar which does not have an up button. This appears to be a completely different view. The back button still works, but my plan is to provide an up button so that the user can navigate through the couple of screens she would like and then can hit the up button to go back to a reasonable place in my app. In this case, the only recourse seems to be repeatedly hitting the back button.

Thanks in advance


Solution

  • You can open the link within your WebView if you set a WebViewClient

    For API 24+ Override this method.

    browser.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                view.loadUrl(request.getUrl().toString());
                return false;
            }
        });
    

    If you're supporting API < 24 then use the old method

    browser.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return false;
            }
        });