video splash screen in android studio tutorial | kotlin android tutorial for beginners | Android
Hello, and Welcome to the #CodingWithDev channel! In this tutorial, We will learn how to create a Splash screen with a video instead of an image, When the video playback is complete, then automatically start the next activity in Android studio using Kotlin.
Step 1: Prepare the video file:
First, make sure you have a video file (e.g., splash_video.mp4
) that you want to display in the splash screen. Place the video file in the res/raw
directory of your Android project.
Step 2: Design the splash screen layout (activity_splash.xml
):
<!-- res/layout/activity_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:layout_height="match_parent">
<VideoView
android:id="@+id/videoView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
Step 3: Create the splash screen activity (SplashActivity.kt
):
// src/main/java/com.example.yourpackage/SplashActivity.kt
import android.net.Uri
import android.os.Bundle
import android.widget.MediaController
import android.widget.VideoView
import androidx.appcompat.app.AppCompatActivity
class SplashActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_splash)
val videoView = findViewById<VideoView>(R.id.videoView)
val videoUri = Uri.parse("android.resource://${packageName}/${R.raw.splash_video}")
videoView.setVideoURI(videoUri)
val mediaController = MediaController(this)
videoView.setMediaController(mediaController)
videoView.setOnPreparedListener { mediaPlayer ->
mediaPlayer.isLooping = true
mediaPlayer.start()
}
videoView.setOnCompletionListener { mediaPlayer ->
navigateToMainActivity()
}
}
private fun navigateToMainActivity() {
val intent = Intent(this, MainActivity::class.java)
startActivity(intent)
finish() // Prevent the user from navigating back to the splash screen
}
}
Step 4: Set the splash screen as the launcher activity in AndroidManifest.xml
:
<!-- AndroidManifest.xml -->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.yourpackage">
<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">
<activity
android:name=".SplashActivity"
android:theme="@style/Theme.AppCompat.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:theme="@style/Theme.AppCompat.Light.DarkActionBar" />
</application>
</manifest>
Show More Tutorials And Feel free to SUBSCRIBE to my YOUTUBE channel Thank You :-)