Rajan Phatak
Rajan Phatak

Reputation: 524

Full Screen Webview without URL Address bar

We are trying to wrap a website within a mobile application. This website supports Chrome, IE9 and above and Safari. While trying to create an Android application to wrap the website, below is the layout which is created

<?xml version="1.0" encoding="utf-8"?>
 <WebView
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/webview"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent" />

Here is my code

protected void onCreate(Bundle savedInstanceState) {
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_web);
    new GcmRegistrationAsyncTask(this).execute();

    WebView myWebView = (WebView) findViewById(R.id.webview);

    Intent intent = getIntent();

    String url = intent.getStringExtra(Constants.URL);
    myWebView.setWebChromeClient(new WebChromeClient());
    //myWebView.setWebViewClient(new WebViewClient());
    myWebView.getSettings().setJavaScriptEnabled(true);
    myWebView.loadUrl(url);
}

If I just use WebViewClient, website works properly in full screen mode but as it is not compatible with Android Viewer, website is not seen properly. If i use WebChromeClient, it opens up the website properly in separate browser (Chrome) but shows URL Address bar. I went through lot of posts on the same but couldn't figure out the solution. Here is my end goal

I need to open the website in mobile application and i want to hide the address title bar. How can i achieve this?

Upvotes: 1

Views: 5629

Answers (2)

Elvis Chweya
Elvis Chweya

Reputation: 1530

I use the following and it works just like you want

WebView webView = (WebView) findViewById(R.id.webview);
ProgressBar progressBar = (ProgrssBar) findViewById(R.id.progressBar);  

String url = "https://www.google.com";

progressBar.setIndeterminate(false);
progressBar.setMax(100);

WebSettings.ZoomDensity zoomDensity = WebSettings.ZoomDensity.FAR;
webView.getSettings().setJavaScriptEnabled(true);

webView.getSettings().setDefaultTextEncodingName("utf-8");
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setDefaultZoom(zoomDensity);
webView.getSettings().setSupportZoom(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.requestFocus(View.FOCUS_DOWN);

webView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NORMAL);
webView.setWebChromeClient(webChromeClient);
webView.setWebViewClient(this.getWebViewClient());

progressBar.setProgress(0);
progressBar.setVisibility(View.VISIBLE);
webView.loadUrl(url);

And the WebChromeClient and WebViewClient implementations as;

WebChromeClient webChromeClient = new WebChromeClient() {

    @Override
    public void onProgressChanged(WebView view, int newProgress) {

        super.onProgressChanged(view, newProgress);
        progressBar.setProgress(newProgress);

    }
};

public WebViewClient getWebViewClient() {

    return new WebViewClient() {

        @Override
        public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
            super.onReceivedError(view, request, error);
            progressBar.setVisibility(View.GONE);
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);

            progressBar.setVisibility(View.GONE);
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {

            progressBar.setVisibility(View.VISIBLE);

            //Some checks
            if (url.contains("load.elsewhere.com")) {
                //If you want to handle where load.elsewhere.com is loaded, say in another external browser
                progressBar.setIndeterminate(true);

                //startActivity to load elsewhere

                return true;
            }

            return super.shouldOverrideUrlLoading(view, url);
        }
    };
}

The ProgressBar is something I add to show some progress

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

    <ProgressBar
        android:id="@+id/progressBar"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="top" />

</FrameLayout>

Upvotes: 1

Dan S
Dan S

Reputation: 9189

WebView does not have an address bar by default something else must be forwarding it to your browser. The API Guide for WebViews shows this example to override shouldOverrideUrlLoading:

private class MyWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (Uri.parse(url).getHost().equals("www.example.com")) {
            // This is my web site, so do not override; let my WebView load the page
            return false;
        }
        // Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        startActivity(intent);
        return true;
    }
}

This will keep your app in your app and offsite links can be handled by the device's browser.

Upvotes: 0

Related Questions