How to get ProgressBar to show while OnClickListener is running?
<p>I have a simple Android program that calculates how long it takes the phone to compute a certain mathematical problem. I want the mathematical problem to start when I hit the button, and while it is running I want a spinning progress bar to be displayed, and I want it to disappear after the math problem is done. This is the code I currently have:</p>

<pre><code>public class main extends AppCompatActivity {
private TextView mScore;
private Button mRunButton;
private TextView mScoreText;
private ProgressBar mSpinner;
@Override


protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mScore = (TextView) findViewById(R.id.score);
    mRunButton = (Button) findViewById(R.id.runbutton);
    mScoreText = (TextView) findViewById(R.id.scoreText);
    mSpinner = (ProgressBar) findViewById(R.id.progress);

    mSpinner.setVisibility(View.GONE);
    View.OnClickListener listener = new View.OnClickListener(){
        @Override

        public void onClick(View v) {
            mSpinner.setVisibility(View.VISIBLE);

            long startTime = System.nanoTime();
            long start  = System.currentTimeMillis();

            long count = 0l;
            for(long x=0;x&lt;Integer.MAX_VALUE ;x++){
                count+=1;
            }
            long end = System.currentTimeMillis();

            long endTime = System.nanoTime();
            long duration = ((endTime - startTime) / 1000000);

            mScore.setText(duration + "");
            mSpinner.setVisibility(View.GONE);

        }
    };

    mRunButton.setOnClickListener(listener);
}
</code></pre>

<p>}</p>

<p>From what I can tell, nothing in the view of the app updates until after the phone is done with the entire onClick method, which is not what I want it to do. I want the progress bar to be displayed ONLY while the program is 'working'. How would I go about doing this?</p>

<p>Thank you</p>