Progress bar.setProgress(variable) in a background thread

962 Views Asked by At

this is my first question on stack (great community, thanks!)

So, I have this problem:

I'm developing an android application with a tab layout. Whenever I navigate to the "Status" tab, I'd like to show a circular ProgressBar that is updated according to the value of a variable. Let's call this variable prog. Moreover, I'd like to display in the center of the ProgressBar a TextView that displays the same value of prog.

I have came up with this part of code:

public class StatusFragment extends Fragment
{

    private ProgressBar torqueRefProgressBar;
    private TextView torqueRefTextView;


    RunningThreadExample r;
    private Thread t1;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        //Inflate the layout for this fragment
        return ...;

    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState)
    {
        // Initializations and other parts not relevant for the question...

        this.torqueRefProgressBar   =   getView().findViewById(R.id.torqueRefProgressBar);

        this.torqueRefTextView      =   getView().findViewById(R.id.torqueRefTextView);


        t1 = new Thread( this.torqueRefProgressBar, this.torqueRefTextView)); // passing arguments by value but values are references to the objects, therefore I'm passing by ref

        t1.start();


    }

}

In this Fragment I create the ProgressBar and the TextView and then pass them to the Runnable as you can see below

public class RunningThreadExample implements Runnable
{

    ProgressBar torqueRefProgBar_thread;
    TextView torqueRefTextView_thread;

    public RunningThreadExample(ProgressBar progressBar, TextView textView)
    {
        this.torqueRefProgBar_thread = progressBar;
        this.torqueRefTextView_thread = textView;
        this.endScale = this.torqueRefProgBar_thread.getMax();
    }

    @Override
    public void run()
    {
        android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);

       float i = 0;

       double temp = 0;


        while(Running)
        {
            try
            {
                Thread.sleep(1000/60); // update freq 60Hz
            }
            catch (InterruptedException e)
            {
                e.printStackTrace();
            }

            temp = getCurrentVariableValue(); // Not shown here (returns a double)

            this.torqueRefProgBar_thread.setProgress((int) temp);
            this.torqueRefTextView_thread.setText(String.valueOf(temp));

        }

        Log.i(INFO_TAG,"Finishing thread!!!!!");

    }


}


I'd like this update to run all the time the app is working (for this reason I've neglected AsyncTask). After a while, it happens that the ProgressBar stops updating, while the TextView continues to work without any problem.

I've been reading that a possible cause could be that the calling setProgress(temp) might stuck the UI. However I can't understand while the TextView is still working.

Can you suggest a better way to make the progressBar update?

Thank you!

2

There are 2 best solutions below

1
On

See this thread:

Android basics: running code in the UI thread

So to summarize, Android requires certain types and sections of code to run in a specific context. Many tools such as Handlers are available to help with these tasks but it was difficult to learn to begin with.

1
On

You cannot affect View from any Thread other than Main Thread. Do the following in run() method

runOnUiThread(new Runnable() { 
// 
run(){

 this.torqueRefProgBar_thread.setProgress((int) temp);
      }      this.torqueRefTextView_thread.setText(String.valueOf(temp));
);