How to Display Progress While Loading a Url to Webview in Android

Android ProgessBar while loading WebView

Check the source code. Help you and solve your problem...

public class AppWebViewClients extends WebViewClient {
private ProgressBar progressBar;

public AppWebViewClients(ProgressBar progressBar) {
this.progressBar=progressBar;
progressBar.setVisibility(View.VISIBLE);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
view.loadUrl(url);
return true;
}

@Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
progressBar.setVisibility(View.GONE);
}
}

I think it help you.

Thanks.

How to Show the progress bar while the url is loading in WebView Android

if you just want the progress bar to appear over the web view when loading change your layout to a RelativeLayout. Put the progress bar on the bottom so it is draw at the top of the view hierarchy.

<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"
android:orientation="vertical">

<WebView
android:id="@+id/activity_main_webview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone"/>

<ProgressBar
android:id="@+id/progressBar1"
style="?android:attr/progressBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:indeterminate="false"
android:layout_centerInParent="true" />
</RelativeLayout>

You also need to override a method on your webview client

mWebView.setWebViewClient(new com.c.MyAppWebViewClient() { 

@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
findViewById(R.id.progressBar1).setVisibility(View.VISIBLE);
}

@Override
public void onPageFinished(WebView view, String url) {
findViewById(R.id.progressBar1).setVisibility(View.GONE);

findViewById(R.id.activity_main_webview).setVisibility(View.VISIBLE);
}
});

How Show progress bar while loading webview load url in android

you should create a custom WebViewClient and overrider onPageStarted and onPageFinished.

Here's example code:

public class CustomWebViewClient extends WebViewClient {
...
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
//TODO: show progress bar here
}

@Override
public void onPageFinished(WebView view, String url) {
//TODO: hide progress bar here
}
...
}

Don't forget to set custom WebViewClient:

myWebView.setWebViewClient(new CustomWebViewClient());

How to display a progress bar when WebView loads a URL, in Android ?

try this code... you need webChoromeClient to track the webview load progress...

webView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
progressBar.setProgress(progress);
if (progress == 100) {
progressBar.setVisibility(View.GONE);

} else {
progressBar.setVisibility(View.VISIBLE);

}
}
});

Replace your webview client with this code....

private class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}

how to display progress while loading a url to webview in android?

set a WebViewClient to your WebView, start your progress dialog on you onCreate() method an dismiss it when the page has finished loading in onPageFinished(WebView view, String url)

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

public class Main extends Activity {
private WebView webview;
private static final String TAG = "Main";
private ProgressDialog progressBar;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

requestWindowFeature(Window.FEATURE_NO_TITLE);

setContentView(R.layout.main);

this.webview = (WebView)findViewById(R.id.webview);

WebSettings settings = webview.getSettings();
settings.setJavaScriptEnabled(true);
webview.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);

final AlertDialog alertDialog = new AlertDialog.Builder(this).create();

progressBar = ProgressDialog.show(Main.this, "WebView Example", "Loading...");

webview.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.i(TAG, "Processing webview url click...");
view.loadUrl(url);
return true;
}

public void onPageFinished(WebView view, String url) {
Log.i(TAG, "Finished loading URL: " +url);
if (progressBar.isShowing()) {
progressBar.dismiss();
}
}

public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Log.e(TAG, "Error: " + description);
Toast.makeText(activity, "Oh no! " + description, Toast.LENGTH_SHORT).show();
alertDialog.setTitle("Error");
alertDialog.setMessage(description);
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
return;
}
});
alertDialog.show();
}
});
webview.loadUrl("http://www.google.com");
}
}

your main.xml layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<WebView android:id="@string/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" />
</LinearLayout>

How to add a progress/loading bar in WebView

Try this i have make some changes in your code

import android.support.v4.widget.SwipeRefreshLayout;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.support.v7.widget.Toolbar;
public class Main2Activity extends AppCompatActivity {

WebView webView;

SwipeRefreshLayout swipe;
ProgressBar progressBar;
Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);

progressBar = (ProgressBar) findViewById(R.id.awv_progressBar);
swipe = (SwipeRefreshLayout) findViewById(R.id.swipe);
LoadWeb();

progressBar.setMax(100);
progressBar.setProgress(1);

swipe = (SwipeRefreshLayout) findViewById(R.id.swipe);
swipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
webView.reload();
}
});

webView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {

progressBar.setProgress(progress);
}
});

webView.setWebViewClient(new WebViewClient() {

@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
progressBar.setVisibility(View.VISIBLE);
}

public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {

webView.loadUrl("file:///android_asset/error.html");
}

public void onLoadResource(WebView view, String url) { //Doesn't work
//swipe.setRefreshing(true);
}

public void onPageFinished(WebView view, String url) {

//Hide the SwipeReefreshLayout
progressBar.setVisibility(View.GONE);
swipe.setRefreshing(false);
}

});

}

public void LoadWeb() {

webView = (WebView) findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setAppCacheEnabled(true);
webView.loadUrl("https://www.google.com/");
swipe.setRefreshing(true);
}

@Override
public void onBackPressed() {

if (webView.canGoBack()) {
webView.goBack();
} else {
finish();
}
}

}

XML.LAYOUT

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/rel_parent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".Main2Activity">

<ProgressBar
android:id="@+id/awv_progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="15dp"
android:layout_marginTop="7dp"
android:indeterminate="true" />

<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipe"
android:layout_width="match_parent"
android:layout_height="match_parent">

<WebView
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent" />

</android.support.v4.widget.SwipeRefreshLayout>

</LinearLayout>

add this theme to your activity

<style name="AppTheme3" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>

OUTPUT

enter image description here

Show a progressbar on top while loading webview app

You create MyWebChromeClient

public class MyWebChromeClient extends WebChromeClient {
private ProgressListener mListener;

public MyWebChromeClient(ProgressListener listener) {
mListener = listener;
}

@Override
public void onProgressChanged(WebView view, int newProgress) {
mListener.onUpdateProgress(newProgress);
super.onProgressChanged(view, newProgress);
}

public interface ProgressListener {
public void onUpdateProgress(int progressValue);
}
}

in Your MainActivity

public class MainActivity extends AppCompatActivity implements MyWebChromeClient.ProgressListener{
private WebView mWebView;
private ProgressBar mProgressBar;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mWebView = (WebView) findViewById(R.id.webView);
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);

// add progress bar
mProgressBar = (ProgressBar) findViewById(R.id.progressBar);
mWebView.setWebChromeClient(new MyWebChromeClient(this));
mWebView.setWebViewClient(new WebViewClient() {

public boolean shouldOverrideUrlLoading(WebView view, String url) {

view.loadUrl(url);

return true;
}

@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
mProgressBar.setVisibility(View.VISIBLE);
}

@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
mProgressBar.setVisibility(View.GONE);

}

});

}

@Override
public void onUpdateProgress(int progressValue) {
mProgressBar.setProgress(progressValue);
if (progressValue == 100) {
mProgressBar.setVisibility(View.INVISIBLE);
}
}
}

in activity_main.xml

<RelativeLayout
android:id="@+id/relative_web_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="fill_parent"
android:layout_height="@dimen/progress_bar_height"
android:progressDrawable="@drawable/bg_progress_bar_webview" />

<WebView
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/progressBar" />
</RelativeLayout>

in drawable create bg_progress_bar_webview.xml

 <?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

<item
android:id="@android:id/background"
android:drawable="@android:color/transparent"/>
<item android:id="@android:id/secondaryProgress">
<scale
android:drawable="@color/progress_bar_second"
android:scaleWidth="100%" />
</item>
<item android:id="@android:id/progress">
<scale
android:drawable="@color/progress_bar_runing"
android:scaleWidth="100%" />
</item>

</layer-list>

Hope !it helps you

Android: How can i show progress bar while loading data into WebView?

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

final ProgressBar progress = (ProgressBar) findViewById(R.id.progress);

WebView webView = (WebView) findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(final WebView view, final String url) {
return false;
}

@Override
public void onPageStarted(final WebView view, final String url, final Bitmap favicon) {
progress.setVisibility(View.VISIBLE);
super.onPageStarted(view, url, favicon);
}

@Override
public void onPageFinished(final WebView view, final String url) {
progress.setVisibility(View.GONE);
super.onPageFinished(view, url);
}
});

webView.loadUrl("http://google.com");
}
}

And R.layout.activity_main:

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

<WebView
android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent" />

<ProgressBar
android:id="@+id/progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
android:layout_centerInParent="true"/>
</RelativeLayout>


Related Topics



Leave a reply



Submit