Android WebPage does not refresh its content - java

I have an android app with this code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setLoadsImagesAutomatically(true);
mWebView.setWebViewClient(new WebViewClient());
mWebView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
mWebView.setScrollbarFadingEnabled(false);
mWebView.loadUrl(url);
}
The WebView opens me the ABC.html page correctly. When I edit the ABC.html page and I open it again using my device, I don't see the new updates I made. It still loads the old page. I added this line:
mWebView.reload();
But the WebView loads the old page again. What can I do?

try:
mWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
and maybe also
mWebView.getSettings().setAppCacheEnabled(false);
although this last line should not matter.

Related

get content html page with web view in android

I am working on an Android project in which I am loading an HTML page from the internet, not locally and then displaying it in WebView. It works fine, but unfortunately, when I click on any of the element in WebView, it opens the link in Browser.
What changes do I need to make so that any links clicked within the web-view will be opened by Web-view itself. Thank you.
public class ContainerActivity extends Activity {
private WebView mWebView;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.contain);
mWebView = (WebView)findViewById(R.id.wvPortal);
mWebView.loadUrl("www.domain.com/path/to/index.html");
WebSettings mWebSettings = mWebView.getSettings();
mWebSettings.setJavaScriptEnabled(true);
}
}
Try to use setWebViewClient:
mWebView = (WebView)findViewById(R.id.wvPortal);
mWebView.setWebViewClient(new WebViewClient());
mWebView.loadUrl("www.domain.com/path/to/index.html");

How can I open pdf files from a web page via android app

I recently build an android app using webview in Android Studio but it doesn't open/download the pdf files from the website. Is there a way to let users open pdf files from a web page without adding them to the app. I want to update the pdf files on the website without having to change the app.
This is my code now:
package com.something.app;
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();
webSettings.setJavaScriptEnabled(true);
myWebView.loadUrl("something");
myWebView.setWebViewClient(new WebViewClient());
}
#Override
public void onBackPressed() {
if (myWebView.canGoBack()) {
myWebView.goBack();
} else {
super.onBackPressed();
}
}
}
If you want to open the PDF in device in-built browser:
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(pdf_url));
startActivity(browserIntent);
Or instead in web view:
Webview webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl(pdf_url);
Try using pdfviewer for that. It's much better than using webview.
The link to the github is this: https://github.com/barteksc/AndroidPdfViewer
This is more useful because it's easy to use.
All you need is this code:
build gradle (app):
implementation 'com.github.barteksc:android-pdf-viewer:2.8.2'
main.java
pdfView = (PdfView) findViewById(R.id.pdfview);
Uri a = new Uri("*your URL*");
pdfView.fromUri(a).load();

Android WebChrome Client Promts/Opens Link in browser rather than WebView

Upon launch of the activity, the webview should load the designated url but in the simulator it launches the native browser and on the physical device it prompts to open the url in in the browser.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_webview);
WebView wv = findViewById(R.id.my_webview);
WebSettings webSettings = wv.getSettings();
wv.setWebChromeClient(new WebChromeClient());
webSettings.setJavaScriptEnabled(true);
wv.loadUrl("http://google.com");
}
Trying to get it so that the webview neither launches in native browser or prompts the user to open in browser. Also all embedded links should stay in the webview if clicked.
I think you have to implement the shouldOverrideUrlLoading() method:
shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return false;
}
This happens if you don't add a WebViewClient to your WebView instance. In order to enable navigation in the same WebView, you need to set a WebViewClient to your WebView instance wv. Add the following line:
wv.setWebViewClient(new WebViewClient());

Android Studio - WebView - open internal links inside WebView, external in default browser

I am trying to add an WebView to my app, but would like to allow to open inside my app only links which are connected with my domain (www.example.com). All other links which doesn't contain the name of my domain (example.com) should be opened externaly by default browser. Using information from stockoverflow I have prepared the following code, but it still doesn't work and all links are opened inside my app, despite shouldOverrideUrlLoading is created ("if (Uri.parse(url).getHost().contains("example.com"))"). Is anyone able to help to resolve this problem? I was trying a lot of samples, but still cannot find the way how to fix it, but I fell that it can be just a small mistake in the code.
public class MainActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView myWebView = (WebView) findViewById(R.id.www);
myWebView.setWebViewClient(new WebViewClient());
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.loadUrl("http://www.example.com");}
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().contains("example.com")) {
return false;
}
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
Resolved! I am not sure that the code is 100% correct, but works fine and maybe it will be useful for someone (I am not the author, I just used a few samples from stackoverflow and put them together):
public class MainActivity extends AppCompatActivity {
private WebView webView;
private ProgressDialog progress;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setContentView(R.layout.activity_main);
WebView myWebView = (WebView) findViewById(R.id.www);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.setWebViewClient(new MyWebViewClient());
myWebView.loadUrl("https://www.example.uk");
}
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().contains("example.com")) {
return false;
}
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
}

WebView size in android studio

when I run my material design site in Chrome, it is displayed like:
But when I run in WebView, it is displayed like
I want to display it in WebView like it does in Chrome
Code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView myWebView = (WebView)findViewById(R.id.webView);
myWebView.setWebViewClient(new WebViewClient());
myWebView.setWebChromeClient(new WebChromeClient());
myWebView.loadUrl("javascript:document.body.style.zoom = "+String.valueOf(scale)+";");
myWebView.loadUrl("http://site");
}

Categories