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"
>
Related
I have a webView in Android and I open a html webpage in it. But it's full of links and images: when I click one of them it loads in my webview.I want to disable this behaviour, so if I click on a link, don't load it and go back to the first page.
I've tried this solution and edited a bit for myself, but it does not work:
webSettings.setJavaScriptEnabled(myWebViewClient.equals(true));
This opens a white page but I want to open the main URL. My webviewclient code:
public class MainActivity extends Activity {
public static String URL = "http://www.example.com/";
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);
MyWebViewClient myWebViewClient = new MyWebViewClient();
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(myWebViewClient.equals(true));
webView.reload();
webView.loadUrl(URL);
webSettings.setDisplayZoomControls(true);
webView.setWebViewClient(new WebViewClient());
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(event.getAction() == KeyEvent.ACTION_DOWN) {
switch(keyCode) {
case KeyEvent.KEYCODE_BACK:
if(webView.canGoBack()){
webView.goBack();
return true;
}
break;
}
}
return super.onKeyDown(keyCode, event);
}
public void onBackPressed(){
if (webView.canGoBack()){
webView.goBack();
}
else{
super.onBackPressed();
}
}
}
you need to overwrite onPageStarted in WebViewClient:
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
//here check url i.e equals to your that html page
// if url equals not load url
and for go to back page check:
if(view.canGoBack())
// load first page url
}
You need a WebViewClient that overrides loading url:
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// url is the url to be loaded
return true; // override url (don't load it)
return false; // let url load
}
});
You can always return true to stay on the same page.
To open Main Url and stay on it, use this:
webview.loadUrl(MainUrl);
and then override loading url and always return true:
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return true;
}
});
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.
I am loading a website in webview,we have used Ajax in website and its working fine on web browser and mobile browser also but in android webview ajax is not working no error is there in the console.Here is my code:-
public class Activity_WebView extends AppCompatActivity implements
ConnectivityReceiver.ConnectivityReceiverListener {
WebView webview;
ProgressDialog pro_dialog;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web_view);
webview = (WebView) findViewById(R.id.webview);
webview.getSettings().setPluginState(WebSettings.PluginState.ON);
webview.setWebViewClient(new loadinsame());
pro_dialog = new ProgressDialog(Activity_WebView.this);
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setDomStorageEnabled(true);
webview.getSettings().setAllowUniversalAccessFromFileURLs(true);
boolean connection = checkConnection();
if (connection) {
webview.loadUrl("website url");
} else {
Toast.makeText(Activity_WebView.this, "Sorry! Not connected to
internet", Toast.LENGTH_SHORT).show();
dialog_Show(webview, "Please check you Inernet connect and Reload.",
false);
}
}
#Override
public void onNetworkConnectionChanged(boolean isConnected) {
if (!isConnected) {
Toast.makeText(Activity_WebView.this, "Sorry! Not connected to
internet", Toast.LENGTH_SHORT).show();
}
}
private class loadinsame extends WebViewClient {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
pro_dialog.setCancelable(false);
pro_dialog.setMessage("Loading...");
pro_dialog.show();
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
pro_dialog.dismiss();
}
#Override
public void onReceivedError(final WebView webview, WebResourceRequest
request, WebResourceError error) {
super.onReceivedError(webview, request, error);
pro_dialog.dismiss();
// dialog_Show(webview, "Error Occur, Do you want to Reload?", true);
}
}
#Override
public void onBackPressed() {
if (webview.canGoBack()) {
webview.goBack();
} else {
super.onBackPressed();
}
}
private boolean checkConnection() {
boolean isConnected = ConnectivityReceiver.isConnected();
return isConnected;
}
#Override
protected void onResume() {
super.onResume();
MyApplication.getInstance().setConnectivityListener(this);
}
}
when i inspect the website in chrome using emulator, find that my ajax remain pending and then cancel after sometime.
Thanks in advance.
Just promoting #Jeff Thomas comment, by setting
mWebView.getSettings().setDomStorageEnabled(true);
worked!!
When i enabled the java script, its working.
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.loadUrl("");
myWebView.getSettings().setDomStorageEnabled(true);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.setWebViewClient(new WebClient());
Solutions
If you want to load web url in WebView so you need follow my code It's code working fine and In web any type of script language used in web page.
public void webviewCallBack(String coverUrl) {
WebView WebView webView = (WebView) findViewById(R.id.web_view);
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webView.getSettings().setPluginState(WebSettings.PluginState.ON);
webView.getSettings().setAppCacheEnabled(true);
webView.getSettings().setDatabaseEnabled(true);
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.clearView();
webView.measure(100, 100);
webView.getSettings().setUseWideViewPort(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.setWebViewClient(new WebViewClient() {
// For api level bellow 24
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.d("weburl__", url);
if (url.startsWith("http") || url.startsWith("https")) {
// Return false means, web view will handle the link
return false;
} else if (url.startsWith("tel:")) {
// Handle the tel: link
handleTelLink(url);
// Return true means, leave the current web view and handle the url itself
return true;
}
return false;
}
// From api level 24
#RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
#Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
// Get the tel: url
String url = request.getUrl().toString();
Log.d("weburl_____", url);
if (url.startsWith("http") || url.startsWith("https")) {
// Return false means, web view will handle the link
return false;
} else if (url.startsWith("tel:")) {
// Handle the tel: link
handleTelLink(url);
// Return true means, leave the current web view and handle the url itself
return true;
}
return false;
}
});
// Load the url into web view
webView.loadUrl(coverUrl);
hoadler();
}
public void hoadler() {
Runnable task = new Runnable() {
public void run() {
material_design_progressbar.setVisibility(View.VISIBLE);
}
};
worker.schedule(task, 5, TimeUnit.SECONDS);
}
I hope this code is helpful for you!
Here is how to edit the AndroidManifes.xml:
Find AndroidManifest.xml file in application folder at:
android/app/src/main/AndroidManifest.xml
Locate the application subelement.
Add the following tag:
android:usesCleartextTraffic=”true”
The application subelement should now look like:
<application
android:name=”io.flutter.app.Test”
android:label=”bell_ui”
android:icon=”#`enter code
here`mapmap/ic_launcher”
android:usesCleartextTraffic=”true”>
Save the AndroidManifest.xml file.
Now im having trouble getting the hardware back button to work. The app loads fine as does page and everything else but as soon as i hit the phones back button it crashes then forces close. Google's instructions sucked also as i am a beginner and they presume you know a certain amount.
Please advise where i should place the code if it is in the wrong place and also if there is anything wrong with it itself.
Cheers!
public class MainActivity extends Activity
{
final Activity activity = this;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
setContentView(R.layout.activity_main);
final WebView webView = (WebView) findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
webSettings.setPluginState(PluginState.ON);
webView.getSettings().setUserAgentString("Mozilla/5.0 (Windows; U; Windows NT 6.1; en-GB; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2 (.NET CLR 3.5.30729)");
webView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress)
{
activity.setTitle("Učitavanje...");
activity.setProgress(progress * 100);
if(progress == 100)
activity.setTitle(R.string.app_name);
}
});
webView.setWebViewClient(new WebViewClient() {
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
{
try {
webView.stopLoading();
} catch (Exception e) {
}
try {
webView.clearView();
} catch (Exception e) {
}
if (webView.canGoBack()) {
webView.goBack();
}
webView.loadUrl("file:///android_asset/greska/greska.html");
super.onReceivedError(webView, errorCode, description, failingUrl);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
return true;
}
});
webView.loadUrl("http://mobile.myweb.rs");
}
public boolean shouldOverrideUrlLoading(WebView webview, String url)
{
webview.loadUrl(url);
return true;
}
WebView webView;
#Override
public void onBackPressed (){
if (webView.isFocused() && webView.canGoBack()) {
webView.goBack();
}
else {
super.onBackPressed();
finish();
}
}
}
Don't call super.onBackPressed AND finish(). Use just one of them.
EDITED: Write
webView = (WebView) findViewById(R.id.webview);
instead of
final WebView webView = (WebView) findViewById(R.id.webview);
Then declare
WebView webView
before your activity onCreate method.
You're getting crach beacause you're trying to use a WebView variable which is actually null.
Good morning,
I want that when the user opens my custom WebView, when he clicks the Menu Button, the standard browser's menu will appear (next, previous, share link, bookmarks, etc.).
How?
Here's my code:
public class WebViewActivity extends Activity {
private static final String TAG = "WebViewActivity";
WebView mWebView;
ProgressBar loadingProgressBar, loadingTitle;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
Shared.openedActivities.add(this);
String url = this.getIntent().getExtras().getString("url");
mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl(url);
mWebView.setWebViewClient(new MyWebViewClient());
WebSettings webSettings = mWebView.getSettings();
webSettings.setPluginState(PluginState.ON);
loadingProgressBar = (ProgressBar) findViewById(R.id.progressbar_Horizontal);
mWebView.setWebChromeClient(new WebChromeClient() {
// this will be called on page loading progress
#Override
public void onProgressChanged(WebView view, int newProgress) {
super.onProgressChanged(view, newProgress);
loadingProgressBar.setProgress(newProgress);
// hide the progress bar if the loading is complete
if (newProgress == 100) {
loadingProgressBar.setVisibility(View.GONE);
} else {
loadingProgressBar.setVisibility(View.VISIBLE);
}
}
});
}
public void onDestroy() {
super.onDestroy();
Trace.w(TAG, "onDestroy()");
Shared.unbindDrawables(findViewById(R.id.rootWebViewActivity));
System.gc();
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
finish();
}
return super.onKeyDown(keyCode, event);
}
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
}
Sorry but that won't work, you have to implement this features by yourself. WebView is not the Browser, it is just a View to render Webpages. Everything else is a function of the Browser-App not the WebView.
Nevertheless you can access the bookmarks of the browser as described here: Get browser history and search result in android