Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7621537
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T04:13:11+00:00 2026-05-31T04:13:11+00:00

I have a simple function that counts from 0 to 5000 and does funky

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T04:13:13+00:00Added an answer on May 31, 2026 at 4:13 am

    you should use AsyncTask for this problem.

     private class YourTask extends AsyncTask<String, Integer, Long> {
     protected Long doInBackground(String... strings) {
         for(int i = 0; i <= 5000; i++)
         {             
             // your code here
             publishProgress((int) ((i / (float) 5000) * 100));
         }
         return i;
     }
    
     protected void onProgressUpdate(Integer... progress) {
         progress_bar.setProgress(progress);
     }
    
     protected void onPostExecute(Long result) {
         //Do something after this task finish
     }
    

    }

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple recursive array function that looks like this: function recursive_array($results) {
I have a simple function that I want to call in the code behind
I have a simple function that returns an NSString after decoding it. I use
I have a very simple function that takes a list of comma separated (x,y)
I have a simple recursive function RCompare() that calls a more complex function Compare()
I have this simple code that speaks for itself. <script language='javascript"> function check() {}
I have a simple tornado server running like this: import json import suds from
I'm trying to create a simple timer in javascript that counts down from 5
I'd like a simple example of exporting a function from a C++ Windows DLL.
I have a simple JavaScript file that has three jQuery $document.ready functions. All three

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.