Reputation: 13
MainActivity.java
Okay, so this is where I load the url now progress bar is visible till the page is loaded and now when I click the links on the page, the progress bar does not appear, I'm searching a fix from 10 hours but none of them are suitable to this conditiion, the progress bar should start as the new url is loaded.
Please Help!
public class MainActivity extends Activity {
private WebView mWebView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWebView = (WebView) findViewById(R.id.activity_main_webview);
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mWebView.loadUrl("http://google.com/");
mWebView.setWebViewClient(new com.c.MyAppWebViewClient(){
@Override
public void onPageFinished(WebView view, String url) {
findViewById(R.id.progressBar1).setVisibility(View.GONE);
findViewById(R.id.activity_main_webview).setVisibility(View.VISIBLE);
}});
}
@Override
public void onBackPressed() {
if(mWebView.canGoBack()) {
mWebView.goBack();
} else {
super.onBackPressed();
}
}
public void aus (MenuItem menuItem){
findViewById(R.id.progressBar1).setVisibility(View.VISIBLE);
mWebView.loadUrl("http://www.google.com");
mWebView.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
findViewById(R.id.progressBar1).setVisibility(View.GONE);
}
});
}
public void cus (MenuItem menuItem){
findViewById(R.id.progressBar1).setVisibility(View.VISIBLE);
mWebView.loadUrl("http://www.google.com");
mWebView.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
findViewById(R.id.progressBar1).setVisibility(View.GONE);
}
});
}
activity_main.xml
<LinearLayout 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">
<ProgressBar
android:id="@+id/progressBar1"
style="?android:attr/progressBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:indeterminate="false"
android:layout_gravity="center" />
<WebView
android:id="@+id/activity_main_webview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone"/>
</LinearLayout>
Upvotes: 1
Views: 7301
Reputation: 4066
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);
}
});
Upvotes: 3
Reputation: 2087
webView.setWebViewClient(new WebViewClient() {
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
loadingBar.setVisibility(View.VISIBLE);
loadingBar.bringToFront();
super.onPageStarted(view, url, favicon);
}
@Override
public void onPageFinished(WebView view, String url) {
loadingBar.setVisibility(View.GONE);
super.onPageFinished(view, url);
}
});
and add this to your xml layout:
<LinearLayout 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"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:visibility="gone"
android:id="@+id/purchase_loadingbar">
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
Upvotes: 1