In continuation to our how-to tutorials on Android Studio, today we’ll see how to create a Start/Splash screen for your Android application. For the demonstration purpose, we will create a very simple splash screen that will load an image at the start and removes it after a specific time. If you are new to Android Studio, then quickly jump to our guide : Getting Started with Android Studio to learn about the new development layout.
Ok! Let start now!
Step 1: Define the Start screen layout.
Jump to activity_main.xml file located under ‘res > layout‘ and paste the below code.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView android:id="@+id/splashscreen" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:src="@drawable/splash"
android:layout_gravity="center|top" android:contentDescription="@string/splash"/>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="@string/hello_world" android:layout_gravity="center"/>
</LinearLayout>
Once done, remember to create appropriate ‘id’ resource and ‘string’ resource via the properties window.
Step 2: Get ready with your Start/Splash screen image. For the demonstration, I created a ‘Techglimpse Image’. Well, that’s not so impressive, so you can create your own image.
Step 3: Create a ‘drawable‘ resource directory by right-clicking on the ‘res‘ folder. Name it as drawable and Resource type as ‘drawable’.
Step 4: Copy the image to the project’s drawable location. In my case, it looks as below,
//localhome//henry//AndroidStudioProjects//StartScreen//StartScreen//src//main//res//drawable
Step 5: Add a handler to activity class.
private static final int STOPSPLASH = 0;
//time in milliseconds
private static final long SPLASHTIME = 3000;
private ImageView splash;
//handler for splash screen
private Handler splashHandler = new Handler() {
/* (non-Javadoc)
* @see android.os.Handler#handleMessage(android.os.Message)
*/
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case STOPSPLASH:
//remove SplashScreen from view
splash.setVisibility(View.GONE);
break;
}
super.handleMessage(msg);
}
};
Checkout the inline comments for the explanation.
Step 6: Make changes to Oncreate method to create a new message and send it delayed to the handler:
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.activity_main);
splash = (ImageView) findViewById(R.id.splashscreen);
Message msg = new Message();
msg.what = STOPSPLASH;
splashHandler.sendMessageDelayed(msg, SPLASHTIME);
}
and that’s it! Run the app and you should see the splash screen staying for few seconds in the beginning.
Download the source code and apk here.
I downloaded the code and tried to view it with the emulator in my android studio. However, I am getting the error message : ” Cannot start compiler: the SDK is not specified for module StartScreen- StartScreen. specify the SDK at the Project Structure Dialog” Unfortunately, I don’t know which SDK to modify. is it “android sdk location” or the “JDK location”. Please help! I’m new to android development.