Hướng dẫn này minh họa cách tạo Splash Screen chuyên nghiệp sử dụng theme chuyên biệt và layer-list drawable, tránh việc dùng Thread.sleep() chặn main thread.
Bước 1: Tạo dự án mới
Mở Android Studio, chọn File → New Project.
Bước 2: Màu sắc (res/values/colors.xml)
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#008577</color>
<color name="colorPrimaryDark">#00574B</color>
<color name="colorAccent">#D81B60</color>
<color name="splash_background_color">#5456e1</color>
</resources>
Bước 3: Background Splash (res/drawable/splash_screen_background.xml)
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="https://schemas.android.com/apk/res/android">
<item>
<color android:color="@color/splash_background_color"/>
</item>
<item>
<bitmap
android:src="@drawable/ic_splash_logo"
android:tileMode="disabled"
android:gravity="center"/>
</item>
</layer-list>
Đặt file logo (ví dụ: ic_splash_logo.png) vào res/drawable/.
Bước 4: Theme Splash (res/values/styles.xml)
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="SplashTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowBackground">@drawable/splash_screen_background</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowFullscreen">true</item>
</style>
</resources>
Bước 5: Splash Activity (src/SplashActivity.java)
package com.example.sample;
import android.content.Intent;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
public class SplashActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Không cần setContentView - theme đã xử lý background
// Chuyển sang MainActivity sau 2-3 giây
new android.os.Handler().postDelayed(() -> {
startActivity(new Intent(SplashActivity.this, MainActivity.class));
finish(); // Đóng SplashActivity, không cho phép back về
}, 2500); // 2.5 giây
}
}
Bước 6: Main Activity (src/MainActivity.java)
package com.example.sample;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
Bước 7: Layout chính (res/layout/activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="20dp"
tools:context=".MainActivity">
<TextView
android:id="@+id/hello_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Chào mừng đến với ứng dụng!"
android:textSize="20sp" />
</RelativeLayout>
Bước 8: Manifest (AndroidManifest.xml)
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android" package="com.example.sample">
<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/AppTheme">
<!-- Splash Activity là launcher, dùng SplashTheme -->
<activity
android:name=".SplashActivity"
android:theme="@style/SplashTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity" />
</application>
</manifest>
✅ Lợi ích của cách này
- Không chặn main thread — dùng Handler.postDelayed thay vì Thread.sleep()
- Không nháy màn hình — windowBackground hiển thị ngay lập tức trước khi app khởi động xong
- Chuẩn Material Design — hỗ trợ adaptive icon, fullscreen
- Tách biệt logic — SplashActivity riêng, dễ bảo trì
Chạy ứng dụng
Kết nối thiết bị, nhấn Run. Màn hình splash hiển thị logo trong 2.5s rồi chuyển sang màn hình chính.