Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Horizontal progress bar does not update its status

I am trying to show horizontal progress bar "Not ProgressDialog" on my activity like this enter image description here

here is what my xml file contains

<ProgressBar
    android:id="@+id/pdialog"
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"        
    android:indeterminate="true"
     />

I am trying to update its status using AsyncTask Class by setting pdialog.setProgress() but its not showing any progress, it works with progressdialog but not with horizontal progress bar.

public class MainActivity extends Activity {

private SQLiteDatabase db;
private Cursor cursor;
private ProgressBar pdialog;
private ImageButton btn;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.start);
    btn = (ImageButton) findViewById(R.id.startbtn);
    pbar = (ProgressBar) findViewById(R.id.progressBar1);
    pdialog = (ProgressBar) findViewById(R.id.pdialog);
    pdialog.setMax(100);
    pdialog.setProgress(20);
    btn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            pdialog.setVisibility(View.VISIBLE);
            new DownloadFilesTask().execute();

        }
    });
}

private class DownloadFilesTask extends AsyncTask<Void, Integer, Integer> {

    int load = 1;

    protected Integer doInBackground(Void... params) {

        try {
                load = 10 * i;
                MainActivity.this.runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        pdialog.setProgress(load);
                    }
                });

            }

        } catch (Exception e) {     

        }   

    }

    protected void onProgressUpdate(Integer... progress) {

        if (progress[0] == 100) {
            pdialog.setVisibility(View.INVISIBLE);          

        }
    }

    protected void onPostExecute(Integer params) {      

    }

}
}
like image 445
Vivek Singh Avatar asked Mar 07 '13 06:03

Vivek Singh


2 Answers

If load variable gets changed correctly:

Instead of this:

MainActivity.this.runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        pdialog.setProgress(load);
                    }
                });

You could use:

publishProgress(load);

which would automatically call on UI Thread:

protected void onProgressUpdate(Integer... progress) {
    int p = progress[0];
    if (p >= 100) {
        pdialog.setVisibility(View.INVISIBLE);          
    }else{
       pdialog.setProgress(p);
    }
}

UPDATE:

remove android:indeterminate="true" as pointed out in other answer.

like image 170
S.D. Avatar answered Nov 14 '22 21:11

S.D.


In your layout file please remove the following attribute android:indeterminate="true" from ProgressBar element.

like image 42
sujith Avatar answered Nov 14 '22 21:11

sujith