Android show progress bar when webview load url

I am new in android apps development and learning it.

I have developed an application which load a website link using webview. And it’s working fine.

But i am facing problem to display and hide progress bar when webview load a page.

I want to display progress bar (Just spinner in center) when webview load main link and internal links of website and hide progress bar when page loaded completely.

My java codes are :

package in.matebook.myapplication;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;


public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    WebView myWebView = (WebView) findViewById(R.id.webView);
    myWebView.setWebViewClient(new MyWebViewClient());
    WebSettings webSettings = myWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    myWebView.loadUrl("https://www.labnol.org");
}



private class MyWebViewClient extends WebViewClient {

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {

        if (Uri.parse(url).getHost().equals("www.labnol.org")) {
            // This is my web site, so do not override; let my WebView load the page
            //or we can add more allowed host in if condition
            return false;
        }
        // Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        startActivity(intent);
        return true;
    }


}



@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return false;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
}

And my layout XML codes are :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">


<WebView  xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/webView"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    />

<ProgressBar
    android:id="@+id/progressBar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    style="?android:attr/progressBarStyleLarge"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true" />

</RelativeLayout>

Right now progress bar is displaying but do not hide.

When i create progressbar object and add below line in java code then progress bar do not display.

progressbar.setVisibility(View.GONE);

I have also try other answer in Stack Overflow like

Android. How can i show progress bar while loading data to webview?

But progress bar do not work when i click on internal links.

  1. I want to display progress bar when main page load and hide progress bar when main page loaded completely.

  2. i want to display progress bar again when user click on internal links of website and same as above hide progress bar when page loaded completely.

Please help i am beginner in android apps development.


Source: xml

Leave a Reply