WebView broken in Android API 23 and below - android

Lately, I have been in trouble running webviews on emulators with API 23 and below.
Webpages is running like this (same code): API 29 and API 21, side by side
Already updated Android Studio and AVD manager, but the problem remains
My code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.loadUrl("https://stackoverflow.com/");
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
}

try my code :
in layout.xml :
<WebView
android:id="#+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
Activity.Java :
public class MainActivity extends Activity {
private WebView webView = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.webView = (WebView) findViewById(R.id.webview);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
WebViewClientImpl webViewClient = new WebViewClientImpl(this);
webView.setWebViewClient(webViewClient);
webView.loadUrl("https://stackoverflow.com/");
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && this.webView.canGoBack()) {
this.webView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
NOTE: do not forget to add INTERNET permission in your manifest.xml :

Related

webview doesn't open some sites in recent versions of android

I have an app that open a webview and I found that I can't open some sites in recent versions of android.
Before I got the error Page not found but I was searching and I found this link where it say:
android:usesCleartextTraffic
Indicates whether the app intends to use cleartext network traffic, such as cleartext HTTP. The default value for apps that target API level 27 or lower is "true". Apps that target API level 28 or higher default to "false".
...
So I add android:usesCleartextTraffic="true" in AndroidManifest.xml and now the webview is white and doesn't show nothing
my webview activity:
public class WebviewUrl extends AppCompatActivity {
private WebView webView;
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().hide();
setContentView(R.layout.activity_webview_url);
openURL();
}
private void openURL() {
Intent getUrl = getIntent();
String url = getUrl.getStringExtra("url");
webView = findViewById(R.id.webview);
webView.setWebViewClient(new WebViewClient());
webView.loadUrl(url);
webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
WebSettings webSettings = webView.getSettings();
webSettings.setDomStorageEnabled(true);
webSettings.setJavaScriptEnabled(true);
webSettings.setLoadsImagesAutomatically(true);
webView.getSettings().setSupportMultipleWindows(true);
webView.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
webView.setWebViewClient(new WebViewClient() {
#Override public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
webView.loadUrl("file:///android_asset/error.html");
} });
webView.addJavascriptInterface(new WebAppInterface(this), "Android");
}
#Override
public void onBackPressed() {
if (webView.canGoBack()) {
webView.goBack();
}
else {
super.onBackPressed();
}
}
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
webView.saveState(outState);
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {
webView.goBack();
return true;
}
else
{
finish();
}
return super.onKeyDown(keyCode, event);
}
}
Someone can help?
UPDATE: Now reappeared that the page was not found
I solved it. In my AndroidManifest.xml I had this 2 lines 'together' and I removed android:networkSecurityConfig="#xml/network_security_config":
<application
...
android:networkSecurityConfig="#xml/network_security_config" <!--I removed that one -->
android:usesCleartextTraffic="true"
>

err_unknown_url_scheme WebView

I have a WebView app that contain a wordpress site, built with elementor pro. I use a button that will allow the user to call or to navigate to an adress via waze. The issue is that it keeps giving me the
err_unknown_url_scheme
Few fixes that did't work.
public class MainActivity extends AppCompatActivity {
private WebView myWebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myWebView = (WebView) findViewById(R.id.webView);
WebSettings webSettings = myWebView.getSettings();
myWebView.loadUrl("https://perfectskin.co.il/app/");
myWebView.setWebViewClient(new WebViewClient());
webSettings.setJavaScriptEnabled(true);
}
#Override
public void onBackPressed() {
if (myWebView.canGoBack()) {
myWebView.goBack();
}else {
super.onBackPressed();
}
}
}
Try to add call permission to manifest file.
"android.permission.CALL_PHONE"

in webview external link not work in my app

I have a problem with webview. In my app when i click a link it open with the default browser. But I want the link open with my own app. Please help how I do this
public class MainActivity extends AppCompatActivity {
private WebView webView ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView =findViewById(R.id.webView);
WebSettings webSettings=webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient());
webView.loadUrl("http://www.somoynews.tv/");
}
}
Try this :
webView = (WebView) findViewById(R.id.webView);
WebSettings webSettings=webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.loadUrl("http://www.somoynews.tv/");
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
view.loadUrl(request.getUrl().toString());
return false;
}
});
}

Android : Magento 2 e-commerce Website not Working in android Webview

When I am trying to load a Magento 2 e-commerce website in android WebView, i'm getting following error message:
"Unable to send the cookie. Maximum number of cookies would be
exceeded."
What's the cause of this error.? How can I fix this.?
Could anyone help me.?
Android Code
WebView webView=(WebView) findViewById(R.id.disp);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setLoadsImagesAutomatically(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setAppCacheEnabled(false);
webView.getSettings().setAllowContentAccess(true);
webView.getSettings().setAllowFileAccess(true);
webView.setWebViewClient(new MyViewClient());
try {
webView.loadUrl("https://sweetroomksa.com/");
}catch (Exception e){
}
Create your custom WebViewClient class and then try to load WebView url like below:
WebViewClientImpl.java
public class WebViewClientImpl extends WebViewClient {
private Activity activity = null;
public WebViewClientImpl(Activity activity) {
this.activity = activity;
}
#Override
public boolean shouldOverrideUrlLoading(WebView webView, String url) {
if(url.indexOf("/sweetroomksa.com/") > -1 )
return false;
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
activity.startActivity(intent);
return true;
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity {
WebView webView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = (WebView) findViewById(R.id.webView);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
WebViewClientImpl webViewClient = new WebViewClientImpl(this);
webView.setWebViewClient(webViewClient);
webView.loadUrl("https://sweetroomksa.com/");
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && this.webView.canGoBack()) {
this.webView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
Above code working perfectly in my device.
Hope above answer may help you.

How can we call webview from the click of a link in a custom listview?

I am having a listView and a custom adapter, and i made an onclicklistener to a text view, now i want to open a link from that textview in webview, how can i do it?
After a lot of research i found the answer, it was really easy.
You have to set the onclickListener in custom adapter class, thats it and make a class to show a webview send a string to that class using intent.putextra();. The class where i am showing webview is this
public class Main extends Activity {
WebView myWebView;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
Intent in = getIntent();
String url = in.getStringExtra(("url"));
myWebView = (WebView) findViewById(R.id.webview);
myWebView.getSettings().setBlockNetworkImage(true);
myWebView.getSettings().setBlockNetworkLoads(true);
myWebView.setWebViewClient(new WebViewClient());
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setBuiltInZoomControls(true);
myWebView.getSettings().setDomStorageEnabled(true);
myWebView.getSettings().setPluginState(PluginState.ON);
myWebView.getSettings().setUserAgentString("Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1A543a Safari/419.3");
myWebView.loadUrl(url);
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) {
myWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
class MyWebViewClient extends WebViewClient {
#Override
// show the web page in webview but not in web browser
public boolean shouldOverrideUrlLoading(WebView view, String url) {
//1 option
getApplicationContext().startActivity(
new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
// 2 option
view.loadUrl(url);
return true;
}
}
}

Categories

Resources