Native Mobile App Development With Java

Splash Screen Android Example Java

07-Oct-2024

Splash Screen Android Example Java

1/  Open manefist and the the activity location 


Splash will replace Main activity .


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.OnubadPlatform"
tools:targetApi="31">


<activity
android:name=".MainActivity"
android:exported="false" />

<activity
android:name=".SplashScreen"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>



1/  Create A New Empty Views Activity(splash1) . activity_splash1.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SplashScreen"
android:background="@color/black">

<ImageView
android:id="@+id/splashscreenLogo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/splashscreen"
/>
//use any image

</LinearLayout>




3/  Splash screen java code


package com.example.onubadplatform;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class SplashScreen extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_screen);


getWindow().setStatusBarColor(getResources().getColor(R.color.black));


// Delay for 3 seconds
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
// Start main activity
Intent intent = new Intent(SplashScreen.this, MainActivity.class);
startActivity(intent);
finish();
}
}, 3000); // 3000 milliseconds = 3 seconds

}
}


4 / Run the app and  See the output


Comments