1
votes

Hi I am working with android . I had created a webview with splash screen with a progress bar. My app initially load the splash screen and the url load as background.when url load completely,the splash screen will disappear and page will be show.this part is working proper.But when I am using progress bar along with splash screen,the progress bar not working..How can I implement progress bar with splash screen...please help me ,I am new to android development and thanks :)

here is my code

    public class WebViewExample extends Activity {
   private WebView mWebView;
   ProgressBar progressBar;
   Context context;
    /** Called when the activity is first created. */


   @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE); // remove titlebar
    setContentView(R.layout.main);
    this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    if(isNetworkStatusAvialable (getApplicationContext())) {
        mWebView = (WebView) findViewById(R.id.webview);
         progressBar = (ProgressBar) findViewById(R.id.progressBar1);

            mWebView.getSettings().setJavaScriptEnabled(true);
            mWebView.setWebViewClient(new wapWebViewClient());
            mWebView.loadUrl("http://facebook.com");
    } else {
          Toast.makeText(getApplicationContext(), "internet is not avialable ", Toast.LENGTH_LONG).show();

    }


  }
    public static boolean isNetworkStatusAvialable (Context context) {
                    ConnectivityManager connectivityManager = (ConnectivityManager)   context.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (connectivityManager != null) 
    {
        NetworkInfo netInfos = connectivityManager.getActiveNetworkInfo();
        if(netInfos != null)
        if(netInfos.isConnected()) 
            return true;
    }
    return false;
}

     private class wapWebViewClient extends WebViewClient {

  @Override
  public boolean shouldOverrideUrlLoading(WebView view, String url) {
      view.loadUrl(url);
      return true;
  }

  @Override
  public void onPageFinished(WebView view, String url) {
      // when the page loaded splash screen has been invisible
      mWebView.setVisibility(View.VISIBLE); 
      progressBar.setVisibility(View.GONE);

  }

    @Override
    public void onReceivedError(WebView view, int errorCode,
            String description, String failingUrl) {
        // if any error occured this message will be showed
                     Toast.makeText(WebViewExample.this, "Error is occured, please try again..." + description, Toast.LENGTH_LONG).show();
    }
  }

   @Override
   public boolean onKeyDown(int keyCode, KeyEvent event) {
  if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
      // setting of back button
      mWebView.goBack();
      return true;
    }
    return super.onKeyDown(keyCode, event);
   }
  }
1
So what would you expect your progress bar to do? It's just a widget. You defined it but there is no update on it. You just are setting it to visible.Peter
i want to load along with the urlptm
It's a call to progressBar.setProgress(...) that's missing.Peter
Hello @Ptm: you want to show progreess bar when your webview is loading i am right? if yes please let me know then i will give you answerV.P.
@ V.P yes u said it..ptm

1 Answers

2
votes

First, you need to set the maximum value for your ProgressBar.

Hint: count all the resources that you need to load and set the max to this quantity.

progressBar.setMax(100); //100 for example.

As well as your code are running and your resources are being loaded, you need to keep incrementing the progressBar progress, use a Integer to do it, create a function to do it for you:

private void incrementPercentage(){
  mProgressStatus++;//i declared it as a private Integer on the activity class.
  progressBar.setProgress(mProgressStatus);
}

To increment your progress, just call incrementPercentage() on every resource loaded.

On the loading of the last resource, the status will be equal as the max value that you did set, and done, you have 100%.

In your case, I see that you use a WebView so the better solution to get the progress is here:

Well, you want a percentage too in your progress, so you need to create a TextView and align it to make it appear on the front center of your progressBar so use a RelativeLayout like that one:

<RelativeLayout
        android:id="@+id/LayoutProgressBar"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        android:gravity="top"
        android:padding="3dp"
        android:weightSum="10" >

        <ProgressBar
            android:id="@+id/progressBar1"
            style="?android:attr/progressBarStyleHorizontal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true" />

        <TextView
            android:id="@+id/lblPercentage"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="-2dp"
            android:text="0%"
            android:textAppearance="?android:attr/textAppearanceMedium" />

    </RelativeLayout>

Then, declare it on your code:

txtPercentage = (TextView) findViewById(R.id.lblPercentage);

And increment it too:

mWebView.setWebChromeClient(new WebChromeClient() {
            public void onProgressChanged(WebView view, int progress) 
               {
               if(progress < 100 && progressBar.getVisibility() == ProgressBar.GONE){
                   progressBar.setVisibility(ProgressBar.VISIBLE);
                   txtPercentage.setVisibility(ProgressBar.VISIBLE);
               }
               progressBar.setProgress(progress);
               txtPercentage.setText(String.valueOf(progress)+'%'); //here is the %
               if(progress == 100) {
                   progressBar.setVisibility(ProgressBar.GONE);
                   txtPercentage.setVisibility(ProgressBar.GONE);
               }
            }
        });