java – Android – Webview Progressbar
I added a progress bar to WebView
I want to have progress bar coverage on WebView. I want to display the percentage of progress bar I know CSS, but I don't know how to change the position of the progress bar 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;
}
}
Do you need to modify this code to display the progress bar percentage? Or can I extend functionality from this code?
Solution
To display the page load progress bar, use the following command:
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 be displayed at the top of the screen using the built-in style, just like the built-in browser
If you want to display and update your ProgressBar, the process is similar, get the handle of the progress bar:
ProgressBar myProgress = (ProgressBar)findViewById(R.id.progressBar1);
. . . And in the onprogresschanged() function, use:
myProgress.setProgress(progress * 100);
Not myactivity this. setProgress().
In addition, if you want the progressbar to appear in front of the WebView, you cannot use LinearLayout Use relativelayout with centerinparent = "true" and list the progressbar after WebView in the layout
