SPLASH SCREEN ACTIVITY

An Android app takes some time to start up, especially when the app is first launched on a device. A splash screen may display start up progress to the user or to indicate branding. This one is used when your activity is doing something in background while it’s launching (checking connection, username, password, logged in, etc.), or just showing your brand, logo or whatever you like.

 

This activity can be done in two ways:

  1. Standard way
  2. More direct approach (described in the next blog)

 

Splash Screen – standard:

Add this in you AndroidManifest.xml

 

In the AndroidManifest we have to have some things first so the SplashScreen will work properly.

– First in the SplashScreen activity tab you need to add <intent-filter>with <action> element ..MAIN/> and <category …LAUNCHER/>to know the app where to start from;

– Second we must declare the theme @style/AppTheme so it won’t have title bar (former action bar) at the top of the screen

– Third change theme accordingly with the themes that we are going to create later in the res/values/styles.xml

Add this style in res/values/styles.xml so it can be called later in your layout xml for the activity

 

Note: You must declare the colors in res/values/colors.xml so the app can call them

 

Then in your layout for the Splash Screen add this XML

Note: I have two ImageView-s for showing two images that I want to be shown on the SplashScreen. Here you can do what ever you like, put some background, color, image, just logo or just the app icon, whatever you like, the options are endless.

 

And finally the java code

In this you need to use Handler and Runnable so it can go from Splash Screen activity to any one you like. (Mine is HomeActivity.class).

– Declare Handler (is used for handling the situation) at the start of the activity class as private, because we will not use it anywhere else, add this in so we can delay the showing of SplashScreenActivity for some thime by calling the .postDelayed() method for newly created handler object with parameters runnable and 500L (in my case 500L, or 500 mili seconds, Long),

– make final Runnable in onCreate() method so we can use it to run the second activity by Intent (we are using SplashScreenActivity.THIS intent from which we will go in to another HomeActivity.class – we must declare it like class so the app can locate the designated activity to execute ),

– call startActivity(intent) (we have called this method by passing intent attribute as parameter to know what to start)

– and in the end – finish() so we can’t go back in the SplashScreenActvity by clicking back on our app.

A Handler allows you to send and process Message and Runnable objects associated with a thread’s MessageQueue. Each Handler instance is associated with a single thread and that thread’s message queue. When you create a new Handler, it is bound to the thread / message queue of the thread that is creating it — from that point on, it will deliver messages and runnables to that message queue and execute them as they come out of the message queue.

There are two main uses for a Handler: (1) to schedule messages and runnables to be executed as some point in the future; and (2) to enqueue an action to be performed on a different thread than your own.

* if you want more info on Handler visit this link:

https://developer.android.com/reference/android/os/Handler.html

The Runnable interface should be implemented by any class whose instances are intended to be executed by a thread. The class must define a method of no arguments called run.

This interface is designed to provide a common protocol for objects that wish to execute code while they are active. For example, Runnable is implemented by class Thread. Being active simply means that a thread has been started and has not yet been stopped.

* if you want more info on Runnable visit this link

https://developer.android.com/reference/java/lang/Runnable.html

Spread the love

Leave a Reply

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