Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

Progress Bar Anroid studio

Hello everyone!

I need to create an app with android studio and i want to create a progress bar when the app is first up and then move it to the second activity after a few seconds when it spinning can somebody help please?

Here is my code :

public class MainActivity extends AppCompatActivity {

public static String TAG = MainActivity.class.getCanonicalName();


private ProgressBar mProgress;
private int mProgressStatus = 0;

private Handler mHandler = new Handler();


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

    mProgress = (ProgressBar) findViewById(R.id.progressBar);

    new Thread(new Runnable() {
        public void run() {
            while (mProgressStatus < 100) {
                mProgressStatus ++;
                // Update the progress bar
                mHandler.post(new Runnable() {
                    public void run() {
                        mProgress.setProgress(mProgressStatus);

                        }

                    }
                });
            }
        }
    }).start();

}

private void startPlaces() {
    Intent intent = new Intent(this, Places.class);
    startActivity(intent);
}

}

Second activity:

public class Places extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);
}

}

2 Answers

Roman Kolesnik
Roman Kolesnik
7,119 Points

Good, but in future if you will need to do some real job during progress bar and do something immideately when the job is done I highly recommend to use delegate, for visual effect hardcoded values is okay, but for real job as database update or some API task hardcoded way is very bad.

Ok thank you very much :)

Roman Kolesnik
Roman Kolesnik
7,119 Points

So you need do some work in first activity and during it show progressbar and after finishing go to second activity - I recommend you to use delegate, here you can check correct answer http://stackoverflow.com/questions/12575068/how-to-get-the-result-of-onpostexecute-to-main-activity-because-asynctask-is-a

Hi thanks for answer but actually i have some code her what you think?

public class MainActivity extends AppCompatActivity { public static String TAG = MainActivity.class.getCanonicalName();

private ProgressBar mProgress;
private int mProgressStatus = 0;

private Handler mHandler = new Handler();


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

    mProgress = (ProgressBar) findViewById(R.id.progressBar);

    new Thread(new Runnable() {
        public void run() {
            while (mProgressStatus < 100) {

                mProgressStatus +=1;
                try {
                    Thread.sleep(30);

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            // Update the progress bar
            mProgress.post(new Runnable() {
                public void run() {
                    mProgress.setVisibility(View.GONE);
                    startActivity();
                }
            });
        }

    }).start();

}

private void startActivity() {
    Intent intent = new Intent(this, SecondActivity.class);
    startActivity(intent);

Log.d(TAG, "code is running");

}

}

and it work