I have a simple function that counts from 0 to 5000 and does funky math with those numbers, and I would like to display progress bar showing where it is currently counting at (each iteration roughly takes 1 second).
My ProgressBar is the following
<TableRow
android:visibility="gone"
android:id="@+id/progress_bar_row" >
<ProgressBar
android:id="@+id/progress_bar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="wrap_content"
android:layout_span="2"
android:layout_height="wrap_content" />
</TableRow>
When user presses button my function starts, it makes the row visible, and I would like to refresh the ProgressBar with each iteration. My function is the following
private void find_number(View v)
{
String response = "";
TextView answer = (TextView)findViewById(R.id.answer);
TableRow progress_bar_row = (TableRow)findViewById(R.id.progress_bar_row);
ProgressBar progress_bar = (ProgressBar)findViewById(R.id.progress_bar);
progress_bar.setProgress(0);
progress_bar.setMax(5000);
progress_bar_row.setVisibility(View.VISIBLE);
answer.setText("");
for(int i = 0; i <= 5000; i++)
{
progress_bar.setProgress(i);
// a lot of heavy math is being performed here and answer is stored inside response string
}
//progress_bar.dismiss();
progress_bar_row.setVisibility(View.GONE);
answer.append(response);
}
I can’t seem to figure out a way of going about refreshing the screen. I poked around and got a suggestion to use threads and handlers, I never used threads before, nor would I know how to change my function to adapt to thread use. Or if there is a simpler way without using any threads and simply calling some kind of refresh function I would appreciate it too. Any help or guidance would be very welcome. Thanks in advance
you should use AsyncTask for this problem.
}