I assume you have one or more applications on your phone that uses splash screen. Splash screen simply refers to the first activity that causes a delay for some seconds while in most cases the logo of the application is displayed or something similar to that is done before taking you to the main activity where you begin to make use of the functionalities of that app.
Some persons find it annoying while others don't. So I suggest you shouldn't over use them in your applications. As the saying goes, "too much of everything is bad."
In this tutorial, I will be teaching you how you can create splash screen in your android application. Feel free to ask questions in the comment section below and I will be glad to answer them.
I don't advice potential programmers or developers to copy and paste codes. I suggest you type everything yourself. It gives you a better understanding.
Open Android Studio, create New Project and give the Application Name ‘SplashScreen’.
Leave the activity name as ‘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.
SplashScreen.java
package com.technology.airwaves.splashscreen; import android.app.Activity; import android.content.Intent; import android.os.Bundle; /** * Created by Joe Gee on 9/9/2016. */ public class SplashScreen extends Activity { private boolean backBtnPressed; private static final int SPLASH_SCREEN_DURATION = 3000; //private Handler handler; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_splash_screen); Thread timerThread = new Thread(){ public void run(){ try{ sleep(SPLASH_SCREEN_DURATION); }catch(InterruptedException e){ e.printStackTrace(); }finally { Intent intent = new Intent(getApplicationContext(),MainActivity.class); startActivity(intent); } } }; timerThread.start(); } @Override protected void onPause() { super.onPause(); finish(); } }
Create XML file under res folder by right clicking on layout > New > Layout resource file. We'll give it the name ‘splash.xml’.
splash.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:background="#013498" android:layout_height="match_parent"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/applogo" android:layout_centerVertical="true" android:src="@drawable/app_logo" android:layout_centerHorizontal="true" android:background="#013498" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/loading" android:layout_below="@id/applogo" android:text="Loading" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:textColor="#fff" android:textSize="30sp" /> </RelativeLayout>
Make sure you have the image used in the ImageView in your drawable folder and the name should correspond to the name used
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
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.airwaves.splashscreen"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".SplashScreen"> <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" /> </application> </manifest>
What we did in the AndroidManifest.xml file is to change the launcher category. By default, the main activity which is always known as MainActivity.java should be launched first when the application is launched. But now we have changed it in a manner that the SplashScreen.java activity will execute first and cause a delay of 3 seconds before launching the second activity known as MainActivity.java.
Moving from one activity to the other is done using Intent. I discussed more on Intent in one of my tutorials. Read more on Intent if you are not clear on it.
As it is, that's how you can create a splash screen activity in your Android application. Thank you.
0 comments:
Post a Comment