Android WebView File Upload

This is how i am using in my app private class MyWebChromeClient extends WebChromeClient { //The undocumented magic method override //Eclipse will swear at you if you try to put @Override here // For Android 3.0+ public void openFileChooser(ValueCallback uploadMsg, String acceptType) { mUploadMessage = uploadMsg; Intent intent = new Intent(Intent.ACTION_GET_CONTENT); intent.addCategory(Intent.CATEGORY_OPENABLE); intent.setType(“*/*”); startActivityForResult(Intent.createChooser(intent, “File … Read more

Letting WebView on Android work with prefers-color-scheme: dark

UPDATE 2022: AndroidX Webkit 1.5.0 fixed a lot of the quirkyness of how webview handles dark mode. For this to work you need to: Add the latest version of webkit to your project (at least 1.5.0): implementation ‘androidx.webkit:webkit:1.5.0’ Set the target version of your application to 33 or up targetSdkVersion 33 Make sure Android System … Read more

shouldOverrideUrlLoading in WebView for Android not running

After some research I conclude that despite what most of the tutorials out there say, shouldOverrideUrlLoading() does not get called when: You load a URL like loadUrl(“http://www.google.com”); The browser redirects the user automatically via an HTTP Redirect. (See the comment from @hmac below regarding redirects) It does however, get called when you you click on … Read more

Get HTTP Status Code in Android WebView

It’s not possible (as of the time i’m writing this). The docs for onReceiveError() are ambiguous at best, but it you look at this issue, http://code.google.com/p/android/issues/detail?id=968 It’s clear that HTTP status codes won’t be reported through that mechanism. How it’s possible that the developers wrote WebView with no way to retrieve the HTTP status code … Read more

Android – Open target _blank links in WebView with external browser

After visiting the above links, I come up with this code and hope this helps. wv.getSettings().setSupportMultipleWindows(true); wv.setWebChromeClient(new WebChromeClient() { @Override public boolean onCreateWindow(WebView view, boolean dialog, boolean userGesture, android.os.Message resultMsg) { WebView.HitTestResult result = view.getHitTestResult(); String data = result.getExtra(); Context context = view.getContext(); Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(data)); context.startActivity(browserIntent); return false; } });

How to get loaded web page title in Android WebView?

You’ll have to use a custom WebViewClient to get this done. You will override the onPageFinished() method so when a new page finishes loading you can set the webview to the appropriate title. Below is a sample implementation of the above: WebView mWebView = (WebView) findViewById(R.id.mwebview); mWebView.setWebViewClient(new WebViewClient() { @Override public void onPageFinished(WebView view, String … Read more

How to clear cookies and cache of webview on Android when not in webview?

I use the following approach in my app: @SuppressWarnings(“deprecation”) public static void clearCookies(Context context) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) { Log.d(C.TAG, “Using clearCookies code for API >=” + String.valueOf(Build.VERSION_CODES.LOLLIPOP_MR1)); CookieManager.getInstance().removeAllCookies(null); CookieManager.getInstance().flush(); } else { Log.d(C.TAG, “Using clearCookies code for API <” + String.valueOf(Build.VERSION_CODES.LOLLIPOP_MR1)); CookieSyncManager cookieSyncMngr=CookieSyncManager.createInstance(context); cookieSyncMngr.startSync(); CookieManager cookieManager=CookieManager.getInstance(); cookieManager.removeAllCookie(); cookieManager.removeSessionCookie(); cookieSyncMngr.stopSync(); cookieSyncMngr.sync(); } } Or … Read more