14 September 2022
8 Steps only.
In android applications, a splash screen is like a welcome screen that includes your brand name or app name, or logo in a full-screen mode. It’s like an introduction to your app in a few seconds and also it enhances the user experience.
I believe you should definitely include a launcher screen as your splash screen. It is super easy to understand as well as to implement.
The code is completely beginner friendly so even if you are new to android, you can easily implement it. We are using java and XML as our languages.
Pre-requisites:
Step 1: Open Android Studio and Click on New Project.
Step 2: Choose “Empty Activity” and click Next.
Step 3: Name your project as “Splash Screen Demo” and Click Finish.
Step 4: Copy your logo image and paste it into the drawable folder.
Make sure the logo image is png and the naming convention does not consist of uppercase letters and numbers otherwise it will create errors.
Step 5: Go to activity_main.xml and type the below code.
You can customize your own background color as per your brand as well as your logo.
In the activity_main.xml design section, click on the image.
Tip: Right-click on the image and Click on Center. You have to center the image horizontally as well as vertically to make it in the middle of the screen.
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/purple_500" //Add your background color here tools:context=".MainActivity"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/aklogo" //Add your logo here app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.5" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"/> </androidx.constraintlayout.widget.ConstraintLayout>
Step 6: Create second activity named as HomeActivity and type the below code in activity_home.xml
After the splash screen disappears, then the second activity will be opened.
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".HomeActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Welcome to Android Knowledge" android:textColor="@color/black" android:textSize="20sp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.5" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout>
Step 7: Type the below code in MainActivity.java
package com.example.splashscreendemo; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.os.Handler; public class MainActivity extends AppCompatActivity { public static int SPLASH_TIMER = 3000; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); new Handler().postDelayed(new Runnable() { @Override public void run() { Intent intent = new Intent(MainActivity.this, HomeActivity.class); startActivity(intent); finish(); } }, SPLASH_TIMER); } }
Step 8: Go to AndroidManifest.xml and type the below code.
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.splashscreendemo"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/Theme.SplashScreenDemo"> <activity android:name=".HomeActivity" android:exported="true" /> <activity android:name=".MainActivity" android:exported="true" android:theme="@style/Theme.MaterialComponents.DayNight.NoActionBar"> //Fullscreen <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
Yayyyy! We did it 🥳
Link – https://youtu.be/iU9KDNRo4TY
Check Our Previous Post – Android Versions Name
If you have any queries or errors related to the above context, please free to comment on this post.
You can check my youtube channel for detailed step-by-step implementation.
Thank you 😊👍