ADD PROGRESS BAR IN WEBVIEW:
In this chapter,i explained how to add progress bar in webview which shows when website is loading and hides when webview is finished,simple way to add an progress bar in webview is explained here,android webview progress bar setup is very simple,to show progress bar while loading the url follow these steps:
androidmanifest.xml:
<uses-permission android:name="android.permission.INTERNET" />
add these line above <application> tag
this gives the user permission to load any url on your android application
activity_main.xml:
add these line to add an progress bar in your android application
<?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">
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:layout_editor_absoluteX="33dp"
tools:layout_editor_absoluteY="200dp"
android:layout_centerHorizontal="true"/>
<WebView
android:id="@+id/webView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@id/progressBar">
</WebView>
</RelativeLayout>
</RelativeLayout> MainActivity.xml:
public class MainActivity extends Activity{
private ProgressBar progressBar;
@SuppressLint("SetJavaScriptEnabled")
@Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
progressBar.setVisibility(View.GONE);
WebView webView = (WebView) findViewById(R.id.webView);
webView.setWebViewClient(new WebViewClientDemo());
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://www.google.com");
}
private class WebViewClientDemo extends WebViewClient {
@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);
progressBar.setVisibility(View.GONE);
progressBar.setProgress(100);
}
@Override public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
progressBar.setVisibility(View.VISIBLE);
progressBar.setProgress(0);
}
}
}
NOTE: IF it shows red line on any of code lines click that text
and press ALT+ENTER