A WebView that:
- Does not delay Activity creation (See this post)
- Built-in ProgressBars (You can also override using your own ProgressBar)
- Configure WebView client through XML
- Largely reduce your code needed in creating a simple WebView
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'
<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 Name | Default | Allowed Values |
---|---|---|
webViewProgressBarStyle | linear | linear / circular / none |
inflationDelay | 100 | any integer (represent ms) |
enableJavaScript | false | true / false |
overrideTelLink | true | true / false |
overrideEmailLink | true | true / false |
overridePdfLink | true | true / false |
showZoomControl | false | true / false |
enableVerticalScrollBar | false | true / false |
enableHorizontalScrollBar | false | true / false |
url | (emptyString) | any String |
@Override
public void onBackPressed() {
if (!webViewSuite.goBackIfPossible()) super.onBackPressed();
}
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.
webViewSuite.setCustomProgressBar (myProgressBar);
myProgressBar
will automatically change visibility with page loads.
webViewSuite.refresh()
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.
}
});
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
}
});
}
Instead of startLoading()
, use startLoadData()
instead:
webViewSuite.startLoadData(data, mimeType, encoding);
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();
}
});
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.
v1.0.3
- Added ability of opening PDF URLs using
Intent(ACTION_VIEW)
v1.0.2
- Flattened View Hierarchy
- Made
toggleProgressBar()
public
v1.0.1
- Added support for static HTML data loading
Copyright 2017 Sira Lam
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
associated documentation files (the WebViewSuite), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or
substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.