Horizontal progress bar does not update its status

I'm trying to show a horizontal progress bar " Not ProgressDialog " in my activity like this enter image description here

this 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 the AsyncTask class by setting pdialog.setProgress () but not showing any progress, it works with progressdialog, but not with horizontal progress indication.

 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) { } } } 
+7
source share
3 answers

If the load variable changes correctly:

Instead of this:

 MainActivity.this.runOnUiThread(new Runnable() { @Override public void run() { pdialog.setProgress(load); } }); 

You can use:

 publishProgress(load); 

which automatically invokes the user interface 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 indicated in another answer.

+4
source

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

+4
source

I had the same question, but I found that the problem is with the View.OnClickListener () interface. When pbar was removed from btn.setOnClickListener (new View.OnClickListener () {}), it worked well. Otherwise, it was not updated. Then I created a constructor that passed pbar to the OnXXXClickListener.

 private class OnXXXClickListener implements View.OnClickListener() { private ProgressBar bar; public OnXXXClickListener(ProgressBar bar) { this.bar = bar; } @Override public void onClick(View v) { bar.setProgess(50); new DownloadFilesTask().execute(); } } 

Then pbar may work well.

0
source

All Articles