Onboarding Screen with ViewPager2 kotlin

07-Jan-2025

Onboarding Screen with ViewPager2 kotlin

Output:






1/  Create an Empty views activity - ActivityOnboarding



2/ Modify the activity_onboarding.xml file 



<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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ActivityOnboarding">

<androidx.viewpager2.widget.ViewPager2
android:id="@+id/viewPager"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" />

<com.google.android.material.button.MaterialButton
android:id="@+id/nextButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Next"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_margin="16dp" />

<com.google.android.material.button.MaterialButton
android:id="@+id/skipButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Skip"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:layout_margin="16dp" />

</androidx.constraintlayout.widget.ConstraintLayout>



3/Modify a layout under res/layout - item_onboarding.xml.



<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
android:id="@+id/imageView"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_marginTop="32dp"
android:contentDescription="@string/app_name"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:src="@drawable/ic_launcher_foreground" />

<TextView
android:id="@+id/titleTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="Title"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintTop_toBottomOf="@id/imageView"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" />

<TextView
android:id="@+id/descriptionTextView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="Description"
android:gravity="center"
app:layout_constraintTop_toBottomOf="@id/titleTextView"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>




4/  Create a data class OnboardingItem.kt



package com.microappvalley.onboardingscreenwithviewpager2kotlin

data class OnboardingItem(
val imageResId: Int,
val title: String,
val description: String
)



5/  Create a OnboardingAdapter.kt



package com.microappvalley.onboardingscreenwithviewpager2kotlin

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView

class OnboardingAdapter(private val items: List<OnboardingItem>) : RecyclerView.Adapter<OnboardingAdapter.OnboardingViewHolder>() {

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): OnboardingViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.item_onboarding, parent, false)
return OnboardingViewHolder(view)
}

override fun onBindViewHolder(holder: OnboardingViewHolder, position: Int) {
holder.bind(items[position])
}

override fun getItemCount(): Int = items.size

inner class OnboardingViewHolder(view: View) : RecyclerView.ViewHolder(view) {
private val imageView: ImageView = view.findViewById(R.id.imageView)
private val titleTextView: TextView = view.findViewById(R.id.titleTextView)
private val descriptionTextView: TextView = view.findViewById(R.id.descriptionTextView)

fun bind(item: OnboardingItem) {
imageView.setImageResource(item.imageResId)
titleTextView.text = item.title
descriptionTextView.text = item.description
}
}
}




6/ Modify ActivityOnboarding.kt




package com.microappvalley.onboardingscreenwithviewpager2kotlin

import android.content.Intent
import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.viewpager2.widget.ViewPager2
import com.google.android.material.button.MaterialButton

class ActivityOnboarding : AppCompatActivity() {
private lateinit var viewPager: ViewPager2
private lateinit var nextButton: MaterialButton
private lateinit var skipButton: MaterialButton

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContentView(R.layout.activity_onboarding)
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
insets
}

viewPager = findViewById(R.id.viewPager)
nextButton = findViewById(R.id.nextButton)
skipButton = findViewById(R.id.skipButton)

val onboardingItems = listOf(
OnboardingItem(R.drawable.ic_launcher_foreground, "Welcome", "Welcome to this app"),
OnboardingItem(R.drawable.ic_launcher_foreground, "Explore", "Explore the whole app"),
OnboardingItem(R.drawable.ic_launcher_foreground, "Get Started", "Get started your journey")
)
val adapter = OnboardingAdapter(onboardingItems)
viewPager.adapter = adapter

nextButton.setOnClickListener {
if (viewPager.currentItem < onboardingItems.size - 1) {
viewPager.currentItem += 1
} else {
finishOnboarding()
}
}

skipButton.setOnClickListener {
finishOnboarding()
}
}

private fun finishOnboarding() {
startActivity(Intent(this, MainActivity::class.java))
finish()
}
}





7/ Modify AndroidManifest.xml file (change the intent filter from MainActivity to OnboardingActivity)



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

<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.OnboardingScreenWithViewPager2Kotlin"
tools:targetApi="31">
<activity
android:name=".ActivityOnboarding"
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"
android:exported="true">
</activity>
</application>

</manifest>



Comments