Java – Android – Webview Progressbar

androidjava

I have added the progressbar above the webview.
Whenever the i click a link, I just made the progressbar to visible.

I want to have the progressbar overlay on the webview and i want to show the percentage of progress bar. I know css, but i don't know how to change the position of progressbar in android

<?xml version="1.0" encoding="utf-8"?>
<WebView  xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/webview01"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
/> 

MainActivity.java

public class MainActivity extends Activity {

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

    web = (WebView) findViewById(R.id.webview01);
 // request the progress-bar feature for the activity
    getWindow().requestFeature(Window.FEATURE_PROGRESS);

    // set a webChromeClient to track progress    
    web.setWebChromeClient(new WebChromeClient()
    {
        public void onProgressChanged(WebView view, int progress)
        {
            // update the progressBar
            MainActivity.this.setProgress(progress * 100);
        }
    });


    web.getSettings().setJavaScriptEnabled(true);
    web.loadUrl("url");

}

// To handle "Back" key press event for WebView to go back to previous screen.
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
    if ((keyCode == KeyEvent.KEYCODE_BACK) && web.canGoBack()) {
        web.goBack();
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

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

}

To display progressbar percentage, do i need to modify this code? or can i extend the functionality from this code?

Best Solution

To show a "page load" progress bar, use this:

in onCreate():

// request the progress-bar feature for the activity
getWindow().requestFeature(Window.FEATURE_PROGRESS);

// set a webChromeClient to track progress    
myWebView.setWebChromeClient(new WebChromeClient()
{
 public void onProgressChanged(WebView view, int progress)
 {
   // update the progressBar
   MyActivity.this.setProgress(progress * 100);
 }
});

This will show using built-in styling at the top of the screen, just like the built-in browser.

If instead you want to display and update your own ProgressBar, the process is similar, get a handle to your progress bar:

ProgressBar myProgress = (ProgressBar)findViewById(R.id.progressBar1);

. . . and in the onProgressChanged() function, use this:

myProgress.setProgress(progress * 100);

instead of MyActivity.this.setProgress().

Further, can't use a LinearLayout if you want to have ProgressBar appear in front of the WebView. Use a RelativeLayout with centerInParent="true", and list the ProgressBar after the WebView in your layout.