To create splash screen project for your android app, copy and paste the codes below to your project to create one of amazing splash screen project.
Open the android studio, create New Project and give Application Name ‘SplashScreen’ to it.
Leave the activity name as by default ‘MainActivity’ and hit the finish button to build the project.
Now create new Java Class under Java folder by right clicking on your package name > New > Java Class. We can give name ‘ SplashScreen.java’ to it. Copy & paste following code.
SplashScreen.java
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
|
package com.topitideas.splashscreen;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class SplashScreen extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
Thread timerThread = new Thread(){
public void run(){
try{
sleep(3000);
}catch(InterruptedException e){
e.printStackTrace();
}finally{
Intent intent = new Intent(SplashScreen.this,MainActivity.class);
startActivity(intent);
}
}
};
timerThread.start();
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
finish();
}
}
|
Similarly, create xml file under res folder by right clicking on layout > New > Layout resource file. We can give name ‘splash.xml’ to it. Copy & paste following code.
splash.xml
1
2
3
4
5
6
7
8
|
<?xml version=“1.0” encoding=“utf-8”?>
<LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”
android:layout_width=“match_parent”
android:layout_height=“match_parent”
android:background=“@drawable/your_image”
android:orientation=“vertical”>
</LinearLayout>
|
Make sure you have put an image in your drawable folder & image name should be ‘your_image’ as we mentioned in xml file.
In AndroidManifest.xml, we have to add both activities SplashScreen.java & splash.xml by which our splash screen would be appear before MainActivity (Home Screen). After adding some codes, AndroidManifest.xml will be,
AndroidManifest.xml
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
|
<?xml version=“1.0” encoding=“utf-8”?>
<manifest xmlns:android=“http://schemas.android.com/apk/res/android”
xmlns:action_android=“http://schemas.android.com/apk/res-auto”
package=“com.topitideas.splashscreen”>
<application
android:allowBackup=“true”
android:icon=“@mipmap/ic_launcher”
android:label=“@string/app_name”
android:theme=“@style/AppTheme” >
<activity
android:name=“.SplashScreen”
android:label=“@string/app_name” >
<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” >
<intent–filter>
<action android:name=“com.topitideas.splashscreen.MainActivity”/>
<category android:name=“android.intent.category.DEFAULT” />
</intent–filter>
</activity>
</application>
</manifest>
|
Keep in mind that MainActivity.java & activity_main.xml will not be modified. So leave both as it is by default. For precaution, coding will looks as following
MainActivity.java
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
|
package com.topitideas.splashscreen;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
|
activity_main.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<RelativeLayout 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=“@dimen/activity_horizontal_margin”
android:paddingRight=“@dimen/activity_horizontal_margin”
android:paddingTop=“@dimen/activity_vertical_margin”
android:paddingBottom=“@dimen/activity_vertical_margin” tools:context=“.MainActivity”>
<TextView android:text=“@string/hello_world”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content” />
</RelativeLayout>
|
That’s it, now run your app & Insha-Allah you will see a splash screen of few seconds showing an image before launching home screen. Your project about splash screen has been successfully completed once if you followed our above steps.