Table of Contents
In this post, we will see how to create Android splash screen.
Android splash screen is nothing but screen that appears when some background task (such as fetching data from database,loading images) is going on. These kind of task generally tasks long and at that time, we can show splash screen. Splash screen can be app icon, company logo , punch line. Once background task completes, another activity will get called.
For example:
When Xender app loads, you will see screen as below.
Create SplashActivity as above. We are creating a new thread and calling sleep for 10 secs on it. In real time, this will be replaced by actually task that will be performed such as fetching data from database or loading images.
Android splash screen is nothing but screen that appears when some background task (such as fetching data from database,loading images) is going on. These kind of task generally tasks long and at that time, we can show splash screen. Splash screen can be app icon, company logo , punch line. Once background task completes, another activity will get called.
For example:
When Xender app loads, you will see screen as below.
Source code:
Lets create Android splash screen example:
Step 1 :Creating Project
Create an android application project named “SplashScreenExampleApp”.
Step 2: Put any image in drawable folder with name “splash_logo”
Step 3 : Creating splash screen layout file
- Go to res -> layout
- Right click on layout
- Click on New -> File.
- Create a file named “splash_screen.xml” and paste below code in splash_screen.xml.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="16dp" android:paddingRight="16dp" android:paddingTop="16dp" android:paddingBottom="16dp"> <ImageView android:layout_width="match_parent" android:layout_height="wrap_content" android:src="@drawable/splash_logo" android:layout_gravity="center"/> </LinearLayout> |
Step 4: Create SplashActivity
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
package com.java2blog.splashscreenexampleapp; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import com.java2blog.splashscreenexampleapp.R; public class SplashActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.splash_screen); //creating new thread just for demonstration of background tasks Thread t=new Thread() { public void run() { try { //sleep thread for 10 seconds sleep(10000); //Call Main activity Intent i=new Intent(SplashActivity.this, com.java2blog.splashscreenexampleapp.MainActivity.class); startActivity(i); //destroying Splash activity finish(); } catch (Exception e) { e.printStackTrace(); } } }; //start thread t.start(); } } |
Step 5 : Creating Layout
Change res ->layout -> activity_main.xml as below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.java2blog.splashscreenexampleapp.MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World from Java2blog.com" /> </RelativeLayout> |
Step 6 : Creating MainActivity
Change src/main/packageName/MainActivity.java as below:
1 2 3 4 5 6 |
package com.java2blog.splashscreenexampleapp; import android.app.Activity; import android.os.Bundle; |
1 2 3 |
<span style="background-color: #e4e4ff; color: navy; font-weight: bold;">import</span> android.support.v7.app.AppCompatActivity; |
1 2 3 |
import com.java2blog.splashscreenexampleapp.R; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } } |
It is default MainActivity provided by HelloWorld app.
Step 7 : Changing AndroidManifest.xml
We need to put launcher activity as SplashActivity, so we need to do following changes in app -> src -> main -> AndroidManifest.xml as below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.java2blog.splashscreenexampleapp"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name="com.java2blog.splashscreenexampleapp.SplashActivity" android:label="@string/app_name" android:screenOrientation="portrait"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".MainActivity" android:label="@string/app_name"> </activity> </application> </manifest> |
Step 7 : Running the app
When you run the app, you will get below screen:
After 10 secs, you will get below screen:
Was this post helpful?
Let us know if this post was helpful. Feedbacks are monitored on daily basis. Please do provide feedback as that\'s the only way to improve.