Loading...

Android WebView Suite

A WebView that does not delay Activity creation, has built-in Progressbar and configurable through XML.

A WebView that:
  1. Does not delay Activity creation (See this post)
  2. Built-in ProgressBars (You can also override using your own ProgressBar)
  3. Configure WebView client through XML
  4. Largely reduce your code needed in creating a simple WebView

Usage

Step 1: Add to Project

First make sure jcenter() is included as a repository in your project's build.gradle:
allprojects {
    repositories {
        google()
        jcenter()
    }
}
And then add the below to your app's build.gradle:
    implementation 'com.asksira.android:webviewsuite:1.0.3'

Step 2: Add WebViewSuite to XML

    <com.asksira.webviewsuite.WebViewSuite
        android:id="@+id/webViewSuite"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:url="@string/url"
        app:webViewProgressBarStyle="linear"
        app:inflationDelay="100"
        app:enableJavaScript="false"
        app:overrideTelLink="true"
        app:overrideEmailLink="true"
        app:overridePdfLink="true"
        app:showZoomControl="false"
        app:enableVerticalScrollBar="false"
        app:enableHorizontalScrollBar="false"/>
Attribute NameDefaultAllowed Values
webViewProgressBarStylelinearlinear / circular / none
inflationDelay100any integer (represent ms)
enableJavaScriptfalsetrue / false
overrideTelLinktruetrue / false
overrideEmailLinktruetrue / false
overridePdfLinktruetrue / false
showZoomControlfalsetrue / false
enableVerticalScrollBarfalsetrue / false
enableHorizontalScrollBarfalsetrue / false
url(emptyString)any String

Override onBackPressed() in your Activity (Suggested)

    @Override
    public void onBackPressed() {
        if (!webViewSuite.goBackIfPossible()) super.onBackPressed();
    }

Advanced Usage

Load URL Programmatically

webViewSuite.startLoading(myURL);
You don't need to worry about WebView not being inflated here.
If WebView is not yet inflated, myURL will be loaded automatically after WebView is inflated.

I want to use my own ProgressBar

webViewSuite.setCustomProgressBar (myProgressBar);
myProgressBar will automatically change visibility with page loads.

I want to bind a refresh button to the WebView

webViewSuite.refresh()

Customizing WebViewClient

This is needed if you need to add more behavior to the WebViewClient.
The most common use-case is to override more URLs according to your project needs.
        webViewSuite.customizeClient(new WebViewSuite.WebViewSuiteCallback() {
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                //Do your own stuffs. These will be executed after default onPageStarted().
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                //Do your own stuffs. These will be executed after default onPageFinished().
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                //Override those URLs you need and return true.
                //Return false if you don't need to override that URL.
            }
        });

Customizing WebView Settings

WebView has a bunch of settings.
If you need to change some of them, DO NOT call getWebView() and change its settings directly. This is because webView may not yet been inflated.
Instead, use the below callback:
        webViewSuite.interfereWebViewSetup(new WebViewSuite.WebViewSetupInterference() {
            @Override
            public void interfereWebViewSetup(WebView webView) {
                WebSettings webSettings = webView.getSettings();
                //Change your WebView settings here
            }
        });
    }

Loading static HTML data

Instead of startLoading(), use startLoadData() instead:
webViewSuite.startLoadData(data, mimeType, encoding);

Set Action after opening PDF

PDF files are opened using simply an ACTION_VIEW intent with the PDF URL.
And it depends, sometimes it may be weird if you go back to the WebView after pressing back button while viewing the PDF file (e.g. The PDF file was the first page, thus when you back, you simply see a blank WebView)
        webViewSuite.setOpenPDFCallback(new WebViewSuite.WebViewOpenPDFCallback() {
            @Override
            public void onOpenPDF() {
                finish();
            }
        });

How does it work?

Since Android 5.0, the first time inflation of a WebView is very slow.
If you do not delay its inflation, It will freeze the user when user is trying to open an Activity that has a WebView. See this post for more information.
By using ViewStub with Handler.postDelayed(), the WebView is inflated after the Activity is completely created and visible to user. So when the WebView is inflating, what user sees is a created Activity with running progressbar. It perfectly looks like the time needed for the inflation of WebView is just the time needed to load that webpage.
This trick greatly improved user experience.
Actually, such trick is not only applicable to WebView, but applicable to all View elements that delays Activity creation due to slow inflation.
Download complete project source code from GitHub
WebView 3510658286182855260
Home item