Webview loading dialog - java

My code below loads a progress dialogue into webview on the initial load but I'd like it to appear every time webview is loading a URL!
I think I need to use onpagestarted() before the progressbar but I'm a bit of a noob at all this and having trouble getting it working. Can anyone advise please?
//need to set progress bar condition here!
progressBar = ProgressDialog.show(MyActivity.this, "Loading title", "Loading...");
myWebView.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
Log.i(TAG, "Finished loading URL: " +url);
if (progressBar.isShowing()) {
progressBar.dismiss();
}
}
});
//web site load
myWebView.loadUrl("http://www.google.com");

boolean loadingFinished = true;
boolean redirect = false;
mWebView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String urlNewString) {
if (!loadingFinished) {
redirect = true;
}
loadingFinished = false;
webView.loadUrl(urlNewString);
return true;
}
#Override
public void onPageStarted(WebView view, String url) {
loadingFinished = false;
//SHOW LOADING IF IT ISNT ALREADY VISIBLE
}
#Override
public void onPageFinished(WebView view, String url) {
if(!redirect){
loadingFinished = true;
}
if(loadingFinished && !redirect){
//HIDE LOADING IT HAS FINISHED
} else{
redirect = false;
}
}
});
As shown here: How to listen for a WebView finishing loading a URL?

You should use the onPageStarted() method to show the ProgressDialog:
myWebView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return false;
}
public void onPageStarted(WebView view, String url, Bitmap favicon) {
progressBar.show();
}
public void onPageFinished(WebView view, String url) {
progressBar.hide();
}
});

Related

how to stop the web view and display a reload message if the page does not load for 30 seconds?

I have a web view and i have this code (P.S i am a beginer in programming)
private WebChromeClient getChromeClient() {
return new WebChromeClient() {
#Override
public void onProgressChanged(WebView view, int newProgress) {
progressDialog.show();
if (newProgress ==100){
progressDialog.dismiss();
}
super.onProgressChanged(view, newProgress);
mWebView.setVisibility(View.GONE);
}
};
}
I already have webview client and webchrome client, you can see and download my code files here (cloud.mail.ru/public/2hHz/25DMkxh3U)
class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return super.shouldOverrideUrlLoading(view, url);
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
mWebView.setVisibility(View.GONE);
super.onPageStarted(view, url, favicon);
}
#Override
public void onLoadResource(WebView view, String url) {
super.onLoadResource(view, url);
}
#Override
public void onPageFinished(WebView view, String url) {
if (mbErrorOccured == false && mbReloadPressed) {
hideErrorLayout();
mbReloadPressed = false;
}
super.onPageFinished(view, url);
mWebView.setVisibility(View.VISIBLE);
}
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
mbErrorOccured = true;
showErrorLayout();
super.onReceivedError(view, errorCode, description, failingUrl);
}
}
At the top you can see my code in Main activity.
You can check how to find if the webview loaded completely at this link :
here
Once you start loading the webview, you can start a handler and check if the page is loaded completely, or you can reload the webpage.
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
// check if the webview is loaded completely after 30 seconds here
//if the page is not loaded then call the below line to reload
// mWebview.loadUrl("http://www.google.com");
}
}, 30000);
Override Webviewclient class and start a countdowntimer in onPageStarted method and cancel the timer in onPageFinished , invoke your method in onfinish of countdowntimer.

trying to open in webview a link thats jumps another page

yes I searched the site and didn't find something that helps me
because every one are saying use "shouldOverrideUrlLoading"
but that's not helping me - its not working
I am trying to open - In the same page an URL that supposed to open a new page link, but when I press it - nothing happens
I am using it as a fragment
public class FlightFragment extends Fragment {
private WebView webView;
public FlightFragment(){};
private String currentUrl;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View rootView = inflater.inflate(R.layout.flight_fragment,container,false);
currentUrl = "https://flights.travelor.me/?locale=he";
webView = (WebView)rootView.findViewById(R.id.flightweb);
WebViewClient MyWebViewClient= new WebViewClient();
MyWebViewClient.shouldOverrideUrlLoading(webView, currentUrl);
ourWebViewClient(currentUrl);
webView.getSettings().setLoadsImagesAutomatically(true);
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
webView.setWebViewClient(new FlightFragment.MyBrowser());
webView.getSettings().setUseWideViewPort(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setSupportZoom(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setDisplayZoomControls(false);
webView.getSettings().setSupportMultipleWindows(true);
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View view, int i, KeyEvent keyEvent) {
if (keyEvent.getAction() == keyEvent.ACTION_DOWN) {
if (webView.canGoBack()) {
webView.goBack();
return true;
}
}
return false;
}
});
webView.loadUrl(currentUrl);
return rootView;
}
public void ourWebViewClient(String currentUrl) {
this.currentUrl = currentUrl;
}
private class MyBrowser extends WebViewClient {
#SuppressWarnings("deprecation")
#TargetApi(Build.VERSION_CODES.N)
#Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
if (request.equals(currentUrl)) {
view.loadUrl(request.getUrl().toString());
}
return true;
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.equals(currentUrl)) {
view.loadUrl(url);
}
return true;
}
}
}
You are returning false when your url is intercepted, try changing like this
webView.setWebViewClient(new WebViewClient() {
class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.equals(currentUrl)) {
Intent intent=new Intent(CurrentActivity.this,NewActivity.class);
intent.putExtra("url to load in NewActivity",url);
startActivity(intent); //get this url using getextra in new activity
}
}
}});
If your target version is >= 26
then override both the function below
#TargetApi(Build.VERSION_CODES.N)
#Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request)
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
Because shouldOverrideUrlLoading(WebView view, String url) is deprecated since Android N. Write the code for Android N and above in the first override method.
In your code remove this
webView.setWebViewClient(new WebViewClient() {
class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.equals(currentUrl)) {
return false;
}
view.loadUrl(url);
return true;
}
}});
as you are setting your WebViewClient again below this code and change your MyBrowser class
private class MyBrowser extends WebViewClient {
#SuppressWarnings("deprecation")
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#TargetApi(Build.VERSION_CODES.N)
#Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
view.loadUrl(request.getUrl().toString());
return true;
}
}
#golan Try to make your Webview fragment as a Activity which will work like a generic view and will open web pages as per requirement based on input url.
shouldOverrideUrlLoading loading method will required if you want to check the input urls.
Do all required web settings like
WebSettings webSettings = webviewBinding.detailWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setDisplayZoomControls(false);
webSettings.setLoadWithOverviewMode(false);
webSettings.setUseWideViewPort(true);
webSettings.setSupportZoom(true);
Override onBackPressed() method with following code:
if (detailWebView.canGoBack()) {
detailWebView.goBack();
}
else {
super.onBackPressed();
}
The canGoBack() will allow you to go back upto all web pages and if no more pages to go back it will redirect to you to last loaded activity or view.

Get clicked url in web view in Android

I need to get a url when user press somethings in a webview.
I use this code:
webView = (WebView) rootView.findViewById(R.id.webView);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.loadUrl("my url");
webView.setWebViewClient(new MyWebClient());
MyWebClien
private class MyWebClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.e("URL", url);
return true;
}
}
But, if i click something, the event is never called.
Thank you
shouldOverrideUrlLoading(WebView view, String url) will get called on clicking a link on a webview.
usage example:
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
try {
// do whatever you want to do on a web link click
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
});
webView.loadUrl("url to open first webpage on a webview");
Based on this: how to get the current page url from the web view in android
WebView webview = new WebView(context);
webview.setWebViewClient(new WebViewClient()
{
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
System.out.println("your current url when webpage loading.." + url);
}
#Override
public void onPageFinished(WebView view, String url) {
System.out.println("your current url when webpage loading.. finish" + url);
super.onPageFinished(view, url);
}
#Override
public void onLoadResource(WebView view, String url) {
// TODO Auto-generated method stub
super.onLoadResource(view, url);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
System.out.println("when you click on any interlink on webview that time you got url :-" + url);
return super.shouldOverrideUrlLoading(view, url);
}
});

WebView clicking on link results in about:blank

I have a WebView in my application and I do a search on google. When I click a link there, let's say stackoverflow.com, it redirects me to a white page and the url is about:blank (new tab?).
I already tried this
webview.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return false;
}
});
but it does not have any effect on this behaviour. Can anyone help me?
I just fixed it with this post here
#Override
public void onPageFinished(WebView view, String url)
{
System.out.println("onPageFinished: " + url);
if ("about:blank".equals(url) && view.getTag() != null)
{
view.loadUrl(view.getTag().toString());
}
else
{
view.setTag(url);
}
}
That normally happens while accessing https URLs which have untrusted certificates. Have you tried this yet?
webview.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return false;
}
#Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
handler.proceed(); // Ignore SSL certificate errors
}
});

webview android with progress dialog

im trying to make an webview application for android with progress bar, i tried a lot of examples from here but none was working .. my problem is that on a frame page or a page with facebook likes or comment plugin the progressdialog never stoped .. here is my code.. please test it and tell me what is wrong.
private WebView webview;
private ProgressDialog progressDialog;
boolean loadingFinished = true;
boolean redirect = false;
int nr = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
webview = (WebView) findViewById(R.string.webview);
final Activity activity = this;
webview.getSettings().setJavaScriptEnabled(true);
webview.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (!loadingFinished) {
redirect = true;
}
loadingFinished = false;
webview.loadUrl(url);
return true;
}
public void onLoadResource(WebView view, String url) {
if (progressDialog == null) {
progressDialog = new ProgressDialog(activity);
progressDialog.setTitle("PitziWorld");
progressDialog.setMessage(progressDialog.toString());
progressDialog.setCancelable(isFinishing());
progressDialog.show();
}
loadingFinished = false;
}
public void onPageFinished(WebView view, String url) {
if(!redirect){
loadingFinished = true;
}
if(loadingFinished && !redirect){
if (progressDialog.isShowing() || progressDialog!=null) {
progressDialog.hide();
progressDialog = null;
}
} else{
redirect = false;
}
}
});
webview.loadUrl("http://www.pitziworld.ro");
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
WebView webView = (WebView) findViewById(R.string.webview);
//Verifica daca tasta apasata a fost back si daca exista istoric
if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {
webView.goBack();
return true;
}
// Daca nu a fost tasta back sau nu este istoric, returnam valoare buton
`
this is what you are looking for!
public void onPageStarted(WebView view, String url, Bitmap favicon) {
mProgress.show();
}
to load progress bar in every page
this is it!

Categories

Resources