Android webview is not working in latest android version - android

I know that this question is ask many times in stackoverflow, but their solution is didnt work for me. Thats why I am asking this question.
Now the question is that I am developing an app by android webview, now this app is working perfect in older version of android version but didnt working in latest android version i.e. 9
I tried many things from other solution like
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
added this code in onCreate method but didnt work.
Another I try is add the #xml/network-security-config file but this also didnt work.
Here is my network-security-config code
<?xml version ="1.0" encoding ="utf-8"?>
<network-security-config>
<domain-config cleartextTrafficPermitted="true">
<domain>onlineawaz.in</domain>
</domain-config>
</network-security-config>
public class MainActivity extends AppCompatActivity {
WebView webView;
ProgressBar bar;
#SuppressLint("NewApi")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = (WebView) findViewById(R.id.webView2);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
bar =(ProgressBar) findViewById(R.id.progressBar2);
webView.loadUrl("http://onlineawaz.in/");
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new myWebClient());
}
public class myWebClient extends WebViewClient{
//page event
}
}

Try adding below code in the AndroidManifest.xml under application tag
android:usesCleartextTraffic="true"
tools:ignore="UnusedAttribute"
First-line will allow you to use HTTP URLs(Not recommended in Pie). The second line is used to ignore the warning for SDK less than 23.

Related

Android WebView and network-security-configuration

I'm developing on Android 8 (26 API, Oreo) and I use android.webkit.WebView in my app.
I would implement "secure network connection" when I load pages with my WebView (in other words I would avoid man-in-the-middle problems and self-signed certificates)
To do this I used network security configuration (on Android from version 7.0 N, 24 API)
So:
In res>xml>network_security_config.xml
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<domain-config>
<domain includeSubdomains="true">MY_DOMAIN.com</domain>
<pin-set>
<pin digest="SHA-256">MY_PIN</pin>
</pin-set>
</domain-config>
</network-security-config>
I found MY_PIN inserting MY_DOMAIN.com here: https://report-uri.com/home/pkp_hash
In manifest>AndoridManifest.xml
...
<application
android:networkSecurityConfig="#xml/network_security_config"
...
</application>
In the onCreate of my app I simply do:
WebView webView = new WebView(this);
webView.setWebViewClient(new WebViewClient() {
#Override
public void onReceivedSslError(..)..
#Override
public void onPageFinished()..
...});
webView.loadUrl(MY_DOMAIN.com);
According to Android docs I'm doing it right but I have a problem: it's like network_security_config.xml is never checked because I can set every "random" and "wrong" value for the pin and it works normally (URL MY_DOMAIN.com is loaded normally without blocking behavior).
So that means that if some man-in-the-middle return back one different pin of those I've written in res>xml>network_security_config.xml the application continue running well and with no secure behavior.
It also does not execute one of the overridden error method of WebViewClient.
Please help I can not understand my error.
[SOLVED]
In AndoridManifest.xml I declared
<application
android:networkSecurityConfig="#xml/network_security_config"
...
</application>
Editor warned about a problem related to the SDK version but I didn't see it.
This is the warning.
[SOLUTION]
Add this tools:targetApi="n" to the Manifest like the following:
<application
android:networkSecurityConfig="#xml/network_security_config"
...
tools:targetApi="n">
[EDIT]
SSL error is handled in public void onReceivedSslError(...) of WebViewClient (See the following code)
webView.setWebViewClient(new WebViewClient() {
public void onReceivedSslError(WebView view,
final SslErrorHandler handler, SslError error) {
//HANDLE HERE THE ERROR!!!
...
}
...
});

Android App not connecting to internet on emulator

The app does not connect to the internet. I have
<uses-permission android:name="android.permission.INTERNET" />
in my manifest and I checked the other applications on the emulator for example youtube and google search works but the application isn't able to load the page. This is my main java file.
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView myWebview ;
myWebview = (WebView) this.findViewById(R.id.webz);
myWebview.loadUrl("https://en.wikipedia.org/");
}
}
and this is my main activity
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.hoda.myapplication.MainActivity">
<WebView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/webz"></WebView>
</android.support.constraint.ConstraintLayout>
this is the error i get
[ERROR:gl_surface_egl.cc(289)] eglChooseConfig failed with error EGL_SUCCESS
Try enabling javascript and override the webviewclient to avoid redirection:
WebView myWebview ;
myWebview = (WebView) this.findViewById(R.id.webz);
myWebview.getSettings().setJavaScriptEnabled(true);
myWebview.setWebViewClient(new WebViewClient());
myWebview.loadUrl("https://en.wikipedia.org/");
and update the constraints of your WebView:
<WebView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:id="#+id/webz"></WebView>
The problem was with the emulator's graphics I believe as trying #diegoveloper 's solution on the emulator still did not load the page but trying it on an actual android device was successful.
Do this
Restart the emulator (as suggested in another post and in the comments of this post)
If that doesn't work, restart your device.
Worked for me.
Add activity in your AndroidManifest.xml
android:name="com.(your browser activity)"
android:screenOrientation="portrait">

Android webview doesn't display web page in some cases

I have an application that's based on alfresco android sdk. After user login to the server MainActivity starts. The MainActivity has few fragments in itself. One fragment contains webview, some buttons and textview. Here is the code of xml layout:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/prop"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/video_layout"
android:layout_width="match_parent"
android:layout_height="192dp"
android:orientation="horizontal"
android:gravity="center" >
<WebView
android:id="#+id/video_web_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
.
.
.
</LinearLayout>
</ScrollView>
When the layout is displayed webview should load url of page with HTML5 video but only blank page is shown. After a while blank page changes to grey. I observed that it means page is load and it show the page with html5 video after user scroll layout. This happens with every url what I've try.
In the test activity I use the same layout and the page with video is loaded and displayed correctly.
In the fragment and in the test activity I use the same code for setting webview and loading the url. Javascript is enabled and I use WebChromeClient like is recommended in WebView docummentation. Also i have INTERNET permission in applications Manifest.
Here is the code from onCreate method from test activity :
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.web_video);
web = (WebView)findViewById(R.id.video_web_view);
.
.
.
web.setWebChromeClient(new WebChromeClient());
WebSettings webSettings = web.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
webSettings.setAllowContentAccess(true);
webSettings.setAllowFileAccess(true);
webSettings.setPluginState(PluginState.ON);
webSettings.setDomStorageEnabled(true);
web.setHorizontalScrollBarEnabled(false);
web.setVerticalScrollBarEnabled(false);
webSettings.setRenderPriority(RenderPriority.HIGH);
webSettings.setUseWideViewPort(false);
webSettings.setCacheMode(WebSettings.LOAD_DEFAULT);
web.loadUrl(someUrl);
}
The same code contains onCreateView in fragment. Only difference is that user have to be login to the server for displaying fragment.
I almost forgotten on errors from Logcat :
02-28 09:34:20.832: V/chromium(9079): external/chromium/net/host_resolver_helper/host_resolver_helper.cc:66: [0228/093420:INFO:host_resolver_helper.cc(66)] DNSPreResolver::Init got hostprovider:0x5354b220
02-28 09:34:20.832: V/chromium(9079): external/chromium/net/base/host_resolver_impl.cc:1515: [0228/093420:INFO:host_resolver_impl.cc(1515)] HostResolverImpl::SetPreresolver preresolver:0x018ee018
02-28 09:34:21.182: V/WebRequest(9079): WebRequest::WebRequest, setPriority = 1
02-28 09:34:21.382: V/chromium(9079): external/chromium/net/disk_cache/hostres_plugin_bridge.cc:52: [0228/093421:INFO:hostres_plugin_bridge.cc(52)] StatHubCreateHostResPlugin initializing...
02-28 09:34:21.392: V/chromium(9079): external/chromium/net/disk_cache/hostres_plugin_bridge.cc:57: [0228/093421:INFO:hostres_plugin_bridge.cc(57)] StatHubCreateHostResPlugin lib loaded
02-28 09:34:21.392: V/chromium(9079): external/chromium/net/disk_cache/hostres_plugin_bridge.cc:63: [0228/093421:INFO:hostres_plugin_bridge.cc(63)] StatHubCreateHostResPlugin plugin connected
02-28 09:34:21.392: V/chromium(9079): external/chromium/net/http/http_cache.cc:1167: [0228/093421:INFO:http_cache.cc(1167)] HttpCache::OnBackendCreated HostStat created
02-28 09:34:21.392: E/chromium(9079): external/chromium/net/disk_cache/stat_hub.cc:213: [0228/093421:ERROR:stat_hub.cc(213)] StatHub::Init - App org.alfresco.mobile.android.samples isn't supported.
02-28 09:34:21.392: E/chromium(9079): external/chromium/net/disk_cache/stat_hub.cc:213: [0228/093421:ERROR:stat_hub.cc(213)] StatHub::Init - App org.alfresco.mobile.android.samples isn't supported.
02-28 09:34:22.222: D/skia(9079): notifyPluginsOnFrameLoad not postponed
Does anyone know what i do wrong? Have anyone some suggestion taht could help me?
Thanks for your answer and sorry for my bad english.
So I figured out what I did wrong. It had something to do with acceleration. I simply added following line to my code:
mWebView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
and It solved my problem.
I had the same problem. below code segment solved the problem
WebView webView = (WebView) findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
webView.setWebChromeClient(new WebChromeClient());
webView.loadUrl(url);
Check that you have included the INTERNET PERMISSION in AndroidManifest file .
<uses-permission android:name="android.permission.INTERNET" />
please add
android:hardwareAccelerated="true"
in your webview activity class file. because in android 3.0+ its supports only if we add this line in android manifest file.
After looking at this post and doing some other research I finally figured out what my issue was. My manifest was right, my layout and webview setup was all correct. However, the URL that I was passing into my WebViewActivity class was not formatted correctly. The difference is very subtle...
I had my URL formatted a certain way:
FORMAT: "scheme://www.websitename.com//"
EXAMPLE: "http://www.awebsite.com//"
It turns out however some websites are not formatted exactly this way and the job of a browser is to fill in the blanks for you. For instance, you can just type "google.com" into a browser and it will know to take you to "https://www.google.com/". The Android WebView does not do this.
The fix:
Visit the website that you are trying to load in a desktop browser and copy the URL after the website loads.
Paste it directly into your Java code:
final String websiteURL = "http://awebsite.com//";
Note that there is no "www." on this URL. Some websites require "www." and some do not. Again, copy the URL from your desktop web browser to determine whether or not the "www." is needed. Another thing to note is that on the end of this URL there is a double-slash "//".
Bottom line, the Android WebView is finicky and it requires that the string URL be exactly right or it will start tossing up errors like this one:
12-16 13:40:26.518: E/chromium(13869): external/chromium/net/disk_cache/stat_hub.cc:216: [1216/134026:ERROR:stat_hub.cc(216)] StatHub::Init - App com.nucitrus.thestory isn't supported.
I hope this helps!

Disable address bar in Android webview

How do disable and hide the address bar from a WebView?
There is no address bar in a WebView.
If you think you have a WebView, and you see an address bar, that is not your WebView. Rather, you are looking at the Browser application. Most likely, the URL you told the WebView to load did a redirect, and you did not intercept that redirect using a WebViewClient and shouldOverrideURLLoading().
Adding myView.setWebViewClient(new WebViewClient()); disabled the address bar for me.
import android.webkit.WebView;
import android.webkit.WebViewClient;
...
WebView myView = findViewById(R.id.myExampleView);
myView.setWebViewClient(new WebViewClient());
myView.getSettings().setJavaScriptEnabled(true);
myView.loadUrl("https://www.stackoverflow.com");
XML Snippet
<WebView android:id="#+id/myExampleView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:keepScreenOn="true"
android:gravity="center" />
source: (Japanese site):
http://www.techdoctranslator.com/android/webapps/webview
Finally I Try with this. Its worked for me..
Here is the working code
private WebView webview ;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ebook);
//webview use to call own site
webview =(WebView)findViewById(R.id.webView);
webview.setWebViewClient(new WebViewClient());
webview .getSettings().setJavaScriptEnabled(true);
webview .getSettings().setDomStorageEnabled(true);
webview.loadUrl("http://www.google.com");
}
and your entire main.xml(res/layout) look should like this:
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/webView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
don't go to add layouts.
webview.setWebViewClient(new WebViewClient());
solved the problem for me..
Kotlin code as following
myWebView.setWebViewClient(WebViewClient())

How to Enable Flash Plugin in Webview?

How to Enable Flash Plugin in Webview Browser?
You just need to enable the plugins for the webview, like this :
WebView mWebView = (WebView) findViewById(R.id.WebView01);
mWebView.getSettings().setPluginsEnabled(true);
I think you also need Flash to be installed, like in Android 2.2 and above.
Hope this helps.
This method is now deprecated, I'll try to explain this more in detail. See here
I believe that you can
WebSettings webSettings = myWebView.getSettings();
webSettings.setPluginState(PluginState.ON);
will work.
setPluginsEnabled() and setPluginsEnabled() are deprecated.
To make Flash Player work in a WebView you need to enable hardware acceleration in your AdroidManifest.xml.
<application
android:allowBackup="true"
android:hardwareAccelerated="true"
android:icon="#mipmap/appicon"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
You can also individually enable it for your WebView by using
mWebView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
try to use this lines of code
webView.getSettings().setPluginState(PluginState.ON);
instead of using
webView.getSettings().setPluginsEnabled(true);
Its work fine...
webView.getSettings().setPluginState(PluginState.ON);
is deprecated now, instead use this:
webView.getSettings().setMediaPlaybackRequiresUserGesture(true);
package com.ageofwar2;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.webkit.WebView;
public class MainActivity extends Activity {
/** com.ageofwar2 */
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
String url ="file:///android_asset/Flash.swf"; //AgeOfWar2.swf
WebView mWebView=(WebView) findViewById(R.id.web_engine);
webView.getSettings().setPluginState(PluginState.ON);
webview.loadUrl();

Categories

Resources