Skip to content Skip to sidebar Skip to footer

Display Progress Bar While Loading

I have one button in the main.xml which will link to another xml which include information from server. I include progress bar to avoid the blank screen while the system is loading

Solution 1:

The mistake you are doing here is you are dumping specific time into your code You never know how much it will take to get response. You should follow following approach

Step 1 Show progress dialog on screen

Step 2 Let download take its own time.But it should be done in new thread

Step 3 Once download is complete it will raise message that task is done,now remove that progress dialog and proceed.

I am pasting sample code here.Hope it will help you.

package com.android.myApps;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

publicclassMainScrextendsActivity
{
    private Handler handler;
    private ProgressDialog progress;
    private Context context;

    @OverridepublicvoidonCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        context = AncActivity.this;
        progress = newProgressDialog(this);
        progress.setTitle("Please Wait!!");
        progress.setMessage("Wait!!");
        progress.setCancelable(false);
        progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);

        handler = newHandler()
        {

            @OverridepublicvoidhandleMessage(Message msg)
            {
                progress.dismiss();
                IntentmainIntent=newIntent(context, Category.class);
                startActivity(mainIntent);
                super.handleMessage(msg);
            }

        };
        progress.show();
        newThread()
        {
            publicvoidrun()
            {
                // Write Your Downloading logic here// at the end write this.
                handler.sendEmptyMessage(0);
            }

        }.start();

    }

}

Solution 2:

Did you try Asyntask? Your doing process will be update in UI.

public final classHttpTaskextendsAsyncTask<String/* Param */, Boolean/* Progress */, String/* Result */> {

    privateHttpClient mHc = newDefaultHttpClient();

    @OverrideprotectedStringdoInBackground(String... params) {
        publishProgress(true);
        // Do the usual httpclient thing to get the resultreturn result;
    }

    @OverrideprotectedvoidonProgressUpdate(Boolean... progress) {
        // line below coupled with //    getWindow().requestFeature(Window.FEATURE_INDETERMINATE_PROGRESS) //    before setContentView // will show the wait animation on the top-right cornerMyActivity.this.setProgressBarIndeterminateVisibility(progress[0]);
    }

    @OverrideprotectedvoidonPostExecute(String result) {
        publishProgress(false);
        // Do something with result in your activity
    }
}

Post a Comment for "Display Progress Bar While Loading"