Android Indeterminate ProgressBar does not spin

android, progress-bar

Solution

Assuming there is an UI blocking issue, you can extend `AsyncTask` and use it in this way:

ProgressDialog m_dialog;

protected void readData(final String whatToFind) {

    if (whatToFind.length() == 0) {
        adapter.notifyDataSetChanged();
        return;
    }

    m_dialog = new ProgressDialog(this);
    new LengthyTask().execute();
}

//AsyncTask<Params, Progress, Result>
//Params: type passed in the execute() call, and received in the doInBackground method
//Progress: type of object passed in publishProgress calls
//Result: object type returned by the doInBackground method, and received by onPostExecute()
private class LengthyTask extends AsyncTask<Object, Integer, Object> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        // initialize the dialog
        m_dialog.setTitle("Searching...");
        m_dialog.setMessage("Please wait while searching...");
        m_dialog.setIndeterminate(true);
        m_dialog.setCancelable(true);
        m_dialog.show();
    }

    @Override
    protected Object doInBackground(Object... params) {
        // do the hard work here
        // call publishProgress() to make any update in the UI
    }

    @Override
    protected void onProgressUpdate(Integer... values) {
        // called from publishProgress(), you can update the UI here
        // for example, you can update the dialog progress
        // m_dialog.setProgress(values[0]); --> no apply here, because we made it indeterminate
    }

    @Override
    protected void onPostExecute(Object result) {
        super.onPostExecute(result);

        // close the dialog
        m_dialog.dismiss();
        //do any other UI related task
    }
}

For a more in-depth explanation of `AsyncTask`, read here and here.

Problem

Sorry if this has been asked before but I cannot find the answer for this (I have searched!) I have tried to follow the google/android documentation to make an indeterminate progressbar appear and spin while my app does a lengthy task. ``` protected void readData(final String whatToFind) { try { if (whatToFind.length() == 0) { adapter.notifyDataSetChanged(); return; } mProgress.setVisibility(View.VISIBLE); mProgressText.setVisibility(View.VISIBLE); m_dialog = new ProgressDialog(this); m_dialog.setTitle("Searching..."); m_dialog.setMessage("Please wait while searching..."); m_dialog.setIndeterminate(true); m_dialog.setCancelable(true); m_dialog.show(); new Thread(new Runnable() { public void run() { mHandler.post(new Runnable() { public void run() { while (LotsOfWorkGoingOn) { // Update the progress bar mHandler.post(new Runnable() { public void run() { mProgress.setProgress(m_i); /// vain attempt to make the progressbar spin } }); mProgress.setVisibility(View.GONE); mProgressText.setVisibility(View.GONE); m_dialog.hide(); } }); } }).start(); } catch (Exception ex) { Log.e(LOGid, "Error listing items:" + ex.getStackTrace()); } ```

Original source

Related problems