Enhanced WebView component for Android that works as intended out of the box
Enhanced WebView component for Android that works as intended out of the box
Enhanced WebView component for Android that works as intended out of the box
Declare the Gradle repository in your root build.gradle
allprojects {
repositories {
maven { url "https://jitpack.io" }
}
}
Declare the Gradle dependency in your app module's build.gradle
dependencies {
implementation 'com.github.delight-im:Android-AdvancedWebView:v3.2.1'
}
<uses-permission android:name="android.permission.INTERNET" />
<im.delight.android.webview.AdvancedWebView
android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
…
android.app.Fragment)Note: If you're using the Fragment class from the support library (android.support.v4.app.Fragment), please refer to the next section (see below) instead of this one.
…
android.support.v4.app.Fragment)Use the code for normal Fragment usage as shown above
Change
mWebView.setListener(this, this);
to
mWebView.setListener(getActivity(), this);
Add the following code to the parent FragmentActivity in order to forward the results from the FragmentActivity to the appropriate Fragment instance
public class MyActivity extends FragmentActivity implements AdvancedWebView.Listener {
@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
if (mFragment != null) {
mFragment.onActivityResult(requestCode, resultCode, intent);
}
}
}
-keep class * extends android.webkit.WebChromeClient { *; }
-dontwarn im.delight.android.webview.**
If you want to serve sites or just single resources over plain http instead of https, there’s usually nothing to do if you’re targeting Android 8.1 (API level 27) or earlier. On Android 9 (API level 28) and later, however, cleartext support is disabled by default. You may have to set android:usesCleartextTraffic="true" on the <application> element in AndroidManifest.xml or provide a custom network security configuration.
Optimized for best performance and security
Features are patched across Android versions
File uploads are handled automatically (check availability with AdvancedWebView.isFileUploadAvailable())
multiple attribute in HTML) are supported on Android 5.0+. The application that is used to pick the files (i.e. usually a gallery or file manager app) must provide controls for selecting multiple files, which some apps don't.JavaScript and WebStorage are enabled by default
Includes localizations for the 25 most widely spoken languages
Receive callbacks when pages start/finish loading or have errors
@Override
public void onPageStarted(String url, Bitmap favicon) {
// a new page started loading
}
@Override
public void onPageFinished(String url) {
// the new page finished loading
}
@Override
public void onPageError(int errorCode, String description, String failingUrl) {
// the new page failed to load
}
Downloads are handled automatically and can be listened to
…
Enable geolocation support (needs <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />)
mWebView.setGeolocationEnabled(true);
Add custom HTTP headers in addition to the ones sent by the web browser implementation
mWebView.addHttpHeader("X-Requested-With", "My wonderful app");
Define a custom set of permitted hostnames and receive callbacks for all other hostnames
mWebView.addPermittedHostname("example.org");
and
@Override
public void onExternalPageRequest(String url) {
// the user tried to open a page from a non-permitted hostname
}
Prevent caching of HTML pages
boolean preventCaching = true;
mWebView.loadUrl("http://www.example.org/", preventCaching);
Check for alternative browsers installed on the device
if (AdvancedWebView.Browsers.hasAlternative(this)) {
AdvancedWebView.Browsers.openUrl(this, "http://www.example.org/");
}
Disable cookies
// disable third-party cookies only
mWebView.setThirdPartyCookiesEnabled(false);
// or disable cookies in general
mWebView.setCookiesEnabled(false);
Allow or disallow (both passive and active) mixed content (HTTP content being loaded inside HTTPS sites)
mWebView.setMixedContentAllowed(true);
// or
mWebView.setMixedContentAllowed(false);
Switch between mobile and desktop mode
mWebView.setDesktopMode(true);
// or
// mWebView.setDesktopMode(false);
Load HTML file from “assets” (e.g. at app/src/main/assets/html/index.html)
mWebView.loadUrl("file:///android_asset/html/index.html");
Load HTML file from SD card
// <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
mWebView.getSettings().setAllowFileAccess(true);
mWebView.loadUrl("file:///sdcard/Android/data/com.my.app/my_folder/index.html");
}
Load HTML source text and display as page
myWebView.loadHtml("<html>...</html>");
// or
final String myBaseUrl = "http://www.example.com/";
myWebView.loadHtml("<html>...</html>", myBaseUrl);
Enable multi-window support
…
All contributions are welcome! If you wish to contribute, please create an issue first so that your feature, problem or question can be discussed.
This project is licensed under the terms of the MIT License.
No open issues yet, or sync has not completed.