एंड्रॉइड ऐप में स्प्लैश स्क्रीन लागू करना
यह उदाहरण दिखाता है कि एंड्रॉइड 12+ के SplashScreen API और पुराने वर्जन के लिए पारंपरिक तरीके से स्प्लैश स्क्रीन कैसे बनाई जाती है।
🎯 आधुनिक तरीका: Android 12+ SplashScreen API (अनुशंसित)
Android 12 (API 31) से स्प्लैश स्क्रीन के लिए बिल्ट-इन सपोर्ट है।
1. निर्भरता जोड़ें
dependencies {
implementation "androidx.core:core-splashscreen:1.0.1"
}
2. स्प्लैश थीम बनाएं
res/values/themes.xml:
<resources>
<style name="Theme.App.Starting" parent="Theme.SplashScreen">
<item name="windowSplashScreenBackground">@color/splash_background</item>
<item name="windowSplashScreenAnimatedIcon">@drawable/ic_splash_logo</item>
<item name="postSplashScreenTheme">@style/Theme.YourApp</item>
</style>
</resources>
3. MainActivity में इंस्टॉल करें
import androidx.core.splashscreen.SplashScreen;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
SplashScreen.installSplashScreen(this); // setContentView से पहले कॉल करें
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
4. Manifest में थीम लागू करें
<activity
android:name=".MainActivity"
android:theme="@style/Theme.App.Starting"
android:exported="true">
<intent-filter>...</intent-filter>
</activity>
📱 पारंपरिक तरीका (सभी वर्जन के लिए)
चरण 1: नया प्रोजेक्ट बनाएं
चरण 2: स्प्लैश बैकग्राउंड ड्रॉएबल
res/drawable/splash_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 android:gravity="center">
<bitmap
android:src="@drawable/ic_app_logo"
android:tileMode="disabled" />
</item>
</layer-list>
चरण 3: रंग परिभाषित करें
res/values/colors.xml:
<resources>
<color name="splash_background_color">#5456E1</color>
<color name="colorPrimary">#008577</color>
<color name="colorPrimaryDark">#00574B</color>
<color name="colorAccent">#D81B60</color>
</resources>
चरण 4: स्प्लैश थीम
res/values/themes.xml:
<resources>
<style name="SplashTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowBackground">@drawable/splash_background</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowFullscreen">true</item>
</style>
</resources>
चरण 5: स्प्लैश एक्टिविटी
SplashActivity.java:
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
public class SplashActivity extends AppCompatActivity {
private static final int SPLASH_DURATION = 3000; // 3 सेकंड
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView की आवश्यकता नहीं - थीम बैकग्राउंड दिखाता है
new Handler(Looper.getMainLooper()).postDelayed(() -> {
startActivity(new Intent(SplashActivity.this, MainActivity.class));
finish();
}, SPLASH_DURATION);
}
}
चरण 6: मुख्य एक्टिविटी
MainActivity.java:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
चरण 7: Manifest कॉन्फ़िगरेशन
<?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/Theme.AppCompat.Light.DarkActionBar">
<!-- स्प्लैश एक्टिविटी लॉन्चर के रूप में -->
<activity
android:name=".SplashActivity"
android:theme="@style/SplashTheme"
android:exported="true">
<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>
⚡ बेस्ट प्रैक्टिस
- Thread.sleep() का उपयोग न करें - यह मुख्य थ्रेड को ब्लॉक करता है
- Handler.postDelayed() या Kotlin Coroutines का उपयोग करें
- स्प्लैश स्क्रीन 3 सेकंड से अधिक न हो
- आवश्यक इनिशियलाइज़ेशन स्प्लैश के दौरान करें (प्रीफेच डेटा, टोकन वैलिडेट, आदि)
ऐप चलाएं
डिवाइस कनेक्ट करें, Run पर क्लिक करें। ऐप लॉन्च होने पर 3 सेकंड के लिए स्प्लैश स्क्रीन दिखेगी, फिर मुख्य स्क्रीन खुलेगी।