Android Splash Screen With Progress Bar Example

In this post, we are going to create a simple Android Splash Screen with a progress bar.

1. splash_screen.xml, the layout file for the splash screen.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorPrimary">
    <ProgressBar
        android:id="@+id/splash_screen_progress_bar"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        style="@android:style/Widget.DeviceDefault.ProgressBar.Horizontal"
        android:layout_marginRight="5dp" />
</RelativeLayout>

2. SplashScreen.java, the splash screen activity. The doWork method is a dummy method for doing some dummy sleeps, replace the content of this method with some of your important tasks that need to be done before the app starts.

public class SplashScreen extends Activity {
    private ProgressBar mProgress;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Show the splash screen
        setContentView(R.layout.splash_screen);
        mProgress = (ProgressBar) findViewById(R.id.splash_screen_progress_bar);

        // Start lengthy operation in a background thread
        new Thread(new Runnable() {
            public void run() {
                doWork();
                startApp();
                finish();
            }
        }).start();
    }

    private void doWork() {
        for (int progress=0; progress<100; progress+=10) {
            try {
                Thread.sleep(1000);
                mProgress.setProgress(progress);
            } catch (Exception e) {
                e.printStackTrace();
                Timber.e(e.getMessage());
            }
        }
    }

    private void startApp() {
        Intent intent = new Intent(SplashScreen.this, MainActivity.class);
        startActivity(intent);
    }
}

3. Make the SplashScreen the launcher class, in the manifest file AndroidManifest.xml

        <activity
            android:name=".SplashScreen"
            android:screenOrientation="portrait"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

Search within Codexpedia

Custom Search

Search the entire web

Custom Search