Android Tutorial – Splash Screen with Progress Bar

In today’s tutorial, we will create simple Android Splash Screen with Progress bar.

Splash screen is the first screen in any app. Generally, splash screen is used to notify users that the app is in process of loading. Splash screen can be plain image,  a simple screen with logo or it can have some animation. Developer can choose any of the type to create splash screen.

Let us see step by step how we can create Splash Screen.

Step 1 – Create xml layout file, activity_splash.xml. This will have a simple TextView, ImageView and ProgressBar

<?xml version=“1.0” encoding=“utf-8”?>

<RelativeLayout xmlns:android=“http://schemas.android.com/apk/res/android”

  xmlns:app=“http://schemas.android.com/apk/res-auto”

  xmlns:tools=“http://schemas.android.com/tools”

  android:layout_width=“match_parent”

  android:layout_height=“match_parent”

  android:orientation=“vertical”

  android:background=“#f2ebf5”>

  <TextView

      android:layout_width=“match_parent”

      android:layout_height=“wrap_content”

      android:layout_marginTop=“30dp”

      android:text=“Purplechai”

      android:layout_centerHorizontal=“true”

      android:textColor=“#462776”

      android:textSize=“27dp”

      android:layout_marginLeft=“10dp”

      android:layout_marginRight=“10dp”

      android:padding=“10dp”

      android:id=“@+id/purplechai”

      android:layout_marginBottom=“10dp”

      android:gravity=“center”/>

  <LinearLayout

      android:layout_width=“match_parent”

      android:layout_gravity=“center_horizontal”

      android:gravity=“center”

      android:layout_below=“@+id/purplechai”

      android:layout_height=“wrap_content”>

      <ImageView

          android:layout_width=“150dp”

          android:layout_height=“150dp”

          android:layout_gravity=“center_horizontal”

          android:background=“@drawable/logo”

          />

  </LinearLayout>

  <ProgressBar

      android:id=“@+id/progress_bar”

      android:layout_width=“match_parent”

      android:layout_height=“wrap_content”

      android:layout_centerInParent=“true”

      style=“@android:style/Widget.DeviceDefault.ProgressBar”

      android:layout_marginRight=“5dp” />

</RelativeLayout>

Step 2 – Create Android Activity file SplashActivity. In doLoadingWork method , all the important loading tasks can be executed. While the loading process is in progress, the current thread performs sleep.

public class SplashActivity extends AppCompatActivity {

 private ProgressBar progressBar;

  @Override

  public void onCreate(Bundle savedInstanceState) {

      super.onCreate(savedInstanceState);

      // Show the splash screen

      setContentView(R.layout.activity_splash);

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

      // Start lengthy operation in a background thread

      new Thread(new Runnable() {

          public void run() {

              doLoadingWork();

              // add your code to redirect to main app screen

              finish();

          }

      }).start();

  }

  // Perform loading tasks in this method, also set the progress bar

  private void doLoadingWork() {

      for (int progress=0; progress<50; progress+=10) {

          try {

              Thread.sleep(1000);

              progressBar.setProgress(progress);

          } catch (Exception e) {

              e.printStackTrace(); }

      }

  }

}

Splash Screen with Progress Bar

Leave a Reply

Your email address will not be published. Required fields are marked *