Walkthrough / Onboarding Screen in Jetpack Compose
Most applications usually require an onboarding screen. Onboarding screens help users get started with the app. Most onboarding screens are usually three screens where the users have to swipe horizontally and see all the screens required and when they are done, they can click a button to get started with the app. Let us see what we want to achieve below.
..
To create an Onboarding layout, we need to add dependency in buld.gradle which will provide pager to Onboarding screens.
implementation 'com.google.accompanist:accompanist-pager:0.24.13-rc'
..
OnBoardingItems.kt
How is the screen/page structured? Each has a title, description, and image. For this, we will create a class to represent each screen and will create a list of pages with three items as our data source as below
class OnBoardingItems(
val image: Int,
val title: Int,
val desc: Int
) {
companion object{
fun getData(): List<OnBoardingItems>{
return listOf(
OnBoardingItems(R.drawable.intro1, R.string.onBoardingTitle1, R.string.onBoardingText1),
OnBoardingItems(R.drawable.intro2, R.string.onBoardingTitle2, R.string.onBoardingText2),
OnBoardingItems(R.drawable.intro3, R.string.onBoardingTitle3, R.string.onBoardingText3)
)
}
}
}
..
OnBoarding.kt
We create a composable to represent each page, a simple column comprising an image, title, and description.
package compose.material.theme
import androidx.compose.animation.core.Spring
import androidx.compose.animation.core.animateDpAsState
import androidx.compose.animation.core.spring
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.outlined.KeyboardArrowLeft
import androidx.compose.material.icons.outlined.KeyboardArrowRight
import androidx.compose.material3.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.google.accompanist.pager.ExperimentalPagerApi
import com.google.accompanist.pager.HorizontalPager
import com.google.accompanist.pager.rememberPagerState
import kotlinx.coroutines.launch
@ExperimentalPagerApi
@Preview
@Composable
fun OnBoarding() {
val items = OnBoardingItems.getData()
val scope = rememberCoroutineScope()
val pageState = rememberPagerState()
Column(modifier = Modifier.fillMaxSize()) {
TopSection(
onBackClick = {
if (pageState.currentPage + 1 > 1) scope.launch {
pageState.scrollToPage(pageState.currentPage - 1)
}
},
onSkipClick = {
if (pageState.currentPage + 1 < items.size) scope.launch {
pageState.scrollToPage(items.size - 1)
}
}
)
HorizontalPager(
count = items.size,
state = pageState,
modifier = Modifier
.fillMaxHeight(0.9f)
.fillMaxWidth()
) { page ->
OnBoardingItem(items = items[page])
}
BottomSection(size = items.size, index = pageState.currentPage) {
if (pageState.currentPage + 1 < items.size) scope.launch {
pageState.scrollToPage(pageState.currentPage + 1)
}
}
}
}
@ExperimentalPagerApi
@Composable
fun TopSection(onBackClick: () -> Unit = {}, onSkipClick: () -> Unit = {}) {
Box(
modifier = Modifier
.fillMaxWidth()
.padding(12.dp)
) {
// Back button
IconButton(onClick = onBackClick, modifier = Modifier.align(Alignment.CenterStart)) {
Icon(imageVector = Icons.Outlined.KeyboardArrowLeft, contentDescription = null)
}
// Skip Button
TextButton(
onClick = onSkipClick,
modifier = Modifier.align(Alignment.CenterEnd),
contentPadding = PaddingValues(0.dp)
) {
Text(text = "Skip", color = MaterialTheme.colorScheme.onBackground)
}
}
}
@Composable
fun BottomSection(size: Int, index: Int, onButtonClick: () -> Unit = {}) {
Box(
modifier = Modifier
.fillMaxWidth()
.padding(12.dp)
) {
// Indicators
Indicators(size, index)
// FAB Next
/* FloatingActionButton(
onClick = onButtonClick,
// backgroundColor = MaterialTheme.colorScheme.primary,
// contentColor = MaterialTheme.colorScheme.onPrimary,
modifier = Modifier.align(Alignment.CenterEnd)
) {
Icon(imageVector = Icons.Outlined.KeyboardArrowRight, contentDescription = "Next")
}*/
FloatingActionButton(
onClick = { /* do something */ },
containerColor = Color.Black,
modifier = Modifier
.align(Alignment.CenterEnd)
.clip(RoundedCornerShape(15.dp, 15.dp, 15.dp, 15.dp))
) {
Icon(Icons.Outlined.KeyboardArrowRight,
tint = Color.White,
contentDescription = "Localized description")
}
}
}
@Composable
fun BoxScope.Indicators(size: Int, index: Int) {
Row(
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.spacedBy(12.dp),
modifier = Modifier.align(Alignment.CenterStart)
) {
repeat(size) {
Indicator(isSelected = it == index)
}
}
}
@Composable
fun Indicator(isSelected: Boolean) {
val width = animateDpAsState(
targetValue = if (isSelected) 25.dp else 10.dp,
animationSpec = spring(dampingRatio = Spring.DampingRatioMediumBouncy)
)
Box(
modifier = Modifier
.height(10.dp)
.width(width.value)
.clip(CircleShape)
.background(
color = if (isSelected) MaterialTheme.colorScheme.primary else Color(0XFFF8E2E7)
)
) {
}
}
@Composable
fun OnBoardingItem(items: OnBoardingItems) {
Column(
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center,
modifier = Modifier.fillMaxSize()
) {
Image(
painter = painterResource(id = items.image),
contentDescription = "Image1",
modifier = Modifier.padding(start = 50.dp, end = 50.dp)
)
Spacer(modifier = Modifier.height(25.dp))
Text(
text = stringResource(id = items.title),
style = MaterialTheme.typography.headlineMedium,
// fontSize = 24.sp,
color = MaterialTheme.colorScheme.onBackground,
fontWeight = FontWeight.Bold,
textAlign = TextAlign.Center,
letterSpacing = 1.sp,
)
Spacer(modifier = Modifier.height(8.dp))
Text(
text = stringResource(id = items.desc),
style = MaterialTheme.typography.bodyLarge,
color = MaterialTheme.colorScheme.onBackground,
fontWeight = FontWeight.Light,
textAlign = TextAlign.Center,
modifier = Modifier.padding(10.dp),
letterSpacing = 1.sp,
)
}
}
..
In our main Activity: MainActivity.kt
package compose.material.theme
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import com.google.accompanist.pager.ExperimentalPagerApi
import compose.material.theme.ui.theme.Material3ComposeTheme
class MainActivity : ComponentActivity() {
@OptIn(ExperimentalPagerApi::class)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
Material3ComposeTheme {
// A surface container using the 'background' color from the theme
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
PreviewFunction()
}
}
}
}
@ExperimentalPagerApi
@Preview(showBackground = true)
@Composable
fun PreviewFunction(){
Surface(modifier = Modifier.fillMaxSize()) {
OnBoarding()
}
}
}
..
..
Full source code:
..
Comments
Post a Comment