user3164874
user3164874

Reputation: 29

Creating Progress Bar

I have created one android program.Now I want a screen with my application name and a progress bar with loading symbol launch first.The progress bar should wait for 3 seconds and then my program must load.How can I implement this?

Upvotes: 0

Views: 117

Answers (2)

Chintan Soni
Chintan Soni

Reputation: 25267

The thing you want to implement is known as Splashscreen. For this:

create an activity SplashScreenActivity.java:

public class SplashScreenActivity extends Activity {

ProgressDialog mProgressDialog;

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

    mProgressDialog = ProgressDialog.show(this, 
                     "Loading", "Please wait...", true);

    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {

            mProgressDialog.dismiss();
            startActivity(new Intent(SplashScreenActivity.this, SecondActivity.class));
            finish();
        }
    }, 3000);
}
}

And then, create another activity SecondActivity.java. Make your SplashScreenActivity.java as Launcher Activity and then run your project.

Edit:

To make your activity as a Launcher Activity, simply add the below lines in the relevant activity tag:

<intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>

Upvotes: 1

InnocentKiller
InnocentKiller

Reputation: 5234

pgbar.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<ProgressBar
    android:id="@+id/progressBar1"
    style="?android:attr/progressBarStyleLarge"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

</LinearLayout>

pgbar.java

public class SplashScreen extends Activity {

private static int SPLASH_TIME_OUT = 3000;
ProgressBar mProgress;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.pgbar);

     mProgress = (ProgressBar) findViewById(R.id.progressBar1);
     mProgress .setCancelable(true);
     mProgress .setMessage("Loading Please wait ...");
     mProgress .setProgress(0);
     mProgress .setMax(100);
     mProgress .show();


    new Handler().postDelayed(new Runnable() {

        @Override
        public void run() {
            Intent i = new Intent(pgbar.this, NextActivity.class);
            startActivity(i);
            finish();
        }
    }, SPLASH_TIME_OUT);
}

}

Upvotes: 0

Related Questions