Reputation: 579
Is it possible to show a progressbar while loading a url? I need to show the level of progress in the progressbar for which i can use a custom progressbar. But am not able to find out the code for getting he exact progress of how much the url has loaded. This is my current code:
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
browser = (WebView)findViewById(R.id.webView1);
progressBar = (ProgressBar)findViewById(R.id.progressBar);
browser.setWebViewClient(new MyBrowser());
browser.setWebChromeClient(new MyCustomChromeClient());
mContext=this.getApplicationContext();
browser.loadUrl(target_url);
MainActivity.this.progressBar.setProgress(0);
}
private class MyBrowser extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
String host = Uri.parse(url).getHost();
if (host.equals(target_url_prefix))
{
if(mWebviewPop!=null)
{
mWebviewPop.setVisibility(View.GONE);
baseLayout.removeView(mWebviewPop);
mWebviewPop=null;
}
return false;
}
if(host.equals("m.facebook.com"))
{
return false;
}
view.loadUrl(url);
return true;
}
@Override
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
noInternet();
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
visible();
}
@Override
public void onPageFinished(WebView view, String url) {
unvisible();
System.out.println("\n" +view.getUrl());
if(url.startsWith("https://m.facebook.com/v2.1/dialog/oauth")){
if(mWebviewPop!=null)
{
mWebviewPop.setVisibility(View.GONE);
baseLayout.removeView(mWebviewPop);
mWebviewPop=null;
}
view.loadUrl(redirectUrl);
return;
}
super.onPageFinished(view, url);
}
}
private class MyCustomChromeClient extends WebChromeClient
{
@Override
public boolean onCreateWindow(WebView view, boolean isDialog,
boolean isUserGesture, Message resultMsg) {
mWebviewPop = new WebView(mContext);
mWebviewPop.setVerticalScrollBarEnabled(false);
mWebviewPop.setHorizontalScrollBarEnabled(false);
mWebviewPop.setWebViewClient(new MyBrowser());
mWebviewPop.getSettings().setJavaScriptEnabled(true);
mWebviewPop.getSettings().setSavePassword(false);
mWebviewPop.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
baseLayout.addView(mWebviewPop);
WebView.WebViewTransport transport = (WebView.WebViewTransport) resultMsg.obj;
transport.setWebView(mWebviewPop);
resultMsg.sendToTarget();
return true;
}
@Override
public void onCloseWindow(WebView window) {
}
@Override
public void onProgressChanged(WebView view, int newProgress) {
MainActivity.this.setValue(newProgress);
super.onProgressChanged(view, newProgress);
}
}
public void setValue(int progress) {
this.progressBar.setProgress(progress);
}
}
How to get the exact progress of loading a url. With this code, i can get the progressbar loading, but its shown as an indeterminate progressbar. I want to show a progressbar which is a customised view. It should display the percentage of succesful loading of the url. So maye i can inflate a customised view, but again i dont know how to get exact percentage. Can someone help me with this code.
Upvotes: 2
Views: 1172
Reputation: 361
The onProgressChanged() of WebChromeClient gives you the progress about the status of page loading. See the below info about the function from the android api docs.
public void onProgressChanged (WebView view, int newProgress)
Added in API level 1
Tell the host application the current progress of loading a page.
Parameters
view The WebView that initiated the callback.
newProgress Current page loading progress, represented by an integer between 0 and 100.
Upvotes: 3
Reputation: 1069
I am not familiar with your Webview. But I open URLs in doInBackground AsyncTask ussualy. So I cannot find a way to express this in percentage directly. But what you could do is maybe trick around, with something like:
Set 20% when your app has confirmed that there is a INTERNET connection enables on the Device.
Set 50% when it reaches the AsyncTask to open URL (or however you initialize .connect())
And finally set 100% when URL is opened.
This is a dummy method. But otherwise you should somehow work with Time (seconds). But an App cannot forsee how long will it need to open the URL. The 100% would represent seconds needed to open the URL, but this is not accessable during loading the URL, since App doesn't know at that time how much it needs to open it.
You could do a test run and time the seconds. Set a timer, when you start opening the URL, and stop the timer when it opens it. But that time would just be in that particular case.. if someone uses a slow Android Device, or Bad Internet Connection, it would probably take longer.
Hope my answer is alteast a bit helpful. If there exists a method to do something more precisely, I am sure guys here will write it.
Upvotes: 1