We have bottom navigation in our app and we want to add swipe behavior into our screens so that if a user swipe to right/left then s/he should be navigated into next screen.
I know that Accompanist has HorizontalPager with Tabs. But I wonder if we can achieve that behavior with bottom navigation.
As you can see in the Material Design Guidelines:
Using swipe gestures on the content area does not navigate between views.
Also:
Avoid using lateral motion to transition between views.
But, if you really want to do this, you can do the this:
fun BottomNavSwipeScreen() {
// This scope is necessary to change the tab using animation
val scope = rememberCoroutineScope()
// I'm using a list of images here
val images = listOf(R.drawable.img1, ...)
// This page state will be used by BottomAppbar and HorizontalPager
val pageState = rememberPagerState(pageCount = images.size)
val scaffoldState = rememberScaffoldState()
Scaffold(
scaffoldState = scaffoldState,
bottomBar = {
BottomAppBar(
backgroundColor = MaterialTheme.colors.primary,
content = {
for (page in images.indices) {
BottomNavigationItem(
icon = {
Icon(Icons.Filled.Home, "Page $page")
},
// here's the trick. the selected tab is based
// on HorizontalPager state.
selected = page == pageState.currentPage,
onClick = {
// When a tab is selected,
// the page is updated
scope.launch {
pageState.animateScrollToPage(page)
}
},
selectedContentColor = Color.Magenta,
unselectedContentColor = Color.LightGray,
label = { Text(text = "Page $page") }
)
}
}
)
},
) {
HorizontalPager(
state = pageState,
offscreenLimit = 2
) { page ->
Image(
painterResource(id = images[page]),
null,
modifier = Modifier
.fillMaxSize(),
contentScale = ContentScale.Crop
)
}
}
}
Here is the result:
you can achieve this by using the animation library from compose:
https://developer.android.com/jetpack/compose/animation
And using the slideIntoContainer animation you can simulate the swipe effect:
composable("route1",
enterTransition = {
slideIntoContainer(
towards = AnimatedContentScope.SlideDirection.Right,
animationSpec = tween(
durationMillis = 250,
easing = LinearEasing // interpolator
)
)
},
exitTransition = {
slideOutOfContainer(
towards = AnimatedContentScope.SlideDirection.Left,
animationSpec = tween(
durationMillis = 250,
easing = LinearEasing
)
)
}) {
Screen1()
}
composable("route2",
enterTransition = {
slideIntoContainer(
towards = AnimatedContentScope.SlideDirection.Left,
animationSpec = tween(
durationMillis = 250,
easing = LinearEasing // interpolator
)
)
},
exitTransition = {
slideOutOfContainer(
towards = AnimatedContentScope.SlideDirection.Right,
animationSpec = tween(
durationMillis = 250,
easing = LinearEasing
)
)
}) {
Screen2()
}
Related
Could you pelase help me?
I want to use swipe to delete and tried a lot of things, but nothing works good. The best way is:
items(listactivity.size) { index ->
val activityItem = listactivity.getOrNull(index)
if (activityItem != null) {
key(activityItem) {
val dismissState = rememberDismissState()
LaunchedEffect(dismissState.currentValue) {
if (dismissState.currentValue == DismissValue.DismissedToStart) {
dashboardViewModel.activityForDelete = activityItem
println("ACT ITEM " + activityItem.activityType?.activityTypeName)
visibleDeleteDialog.value = true
dismissState.snapTo(DismissValue.Default)
}
}
if (visibleDeleteDialog.value) {
BaseAlertDialog(
onExit = {
visibleDeleteDialog.value = false
},
onSuccess = {
removeActivity()
visibleDeleteDialog.value = false
},
disclaimerFirst = R.string.confirm_delete_activity,
disclaimerSecond = R.string.confirm_delete_activity_text,
successName = R.string.delete_session
)
}
SwipeToDismiss(
state = dismissState,
directions = setOf(DismissDirection.EndToStart),
background = {
val direction =
dismissState.dismissDirection ?: return#SwipeToDismiss
val alignment = when (direction) {
DismissDirection.StartToEnd -> Alignment.CenterStart
DismissDirection.EndToStart -> CenterEnd
}
Box(
modifier = Modifier
.fillMaxSize()
.background(Black)
.padding(horizontal = Spacing_12),
contentAlignment = alignment
) {
Text(
text = stringResource(id = R.string.delete),
color = Color.Red,
fontSize = Text_16,
fontWeight = FontWeight.Bold
)
}
},
dismissContent = {
GameCard(activityItem)
}
)
}
}
}
However th lisitem do not update items after delete immidiately (only after scroll i can see what item was deleted).
What am i doing wrong?
I tried SwipeToDismiss and other internet things.
I know google has a bug in this function items() some times,
but please try to replace your function with itemsIndexed and add another parameter
itemsIndexed(listactivity.size) { index , item ->
something like this
// import this at the top
import androidx.compose.foundation.lazy.itemsIndexed
//code
//*
//*
//*
itemsIndexed(listactivity.size) { index , item ->
val activityItem = listactivity.getOrNull(index)
if (activityItem != null) {
key(activityItem) {
val dismissState = rememberDismissState()
LaunchedEffect(dismissState.currentValue) {
if (dismissState.currentValue == DismissValue.DismissedToStart) {
dashboardViewModel.activityForDelete = activityItem
println("ACT ITEM " + activityItem.activityType?.activityTypeName)
visibleDeleteDialog.value = true
dismissState.snapTo(DismissValue.Default)
}
}
if (visibleDeleteDialog.value) {
BaseAlertDialog(
onExit = {
visibleDeleteDialog.value = false
},
onSuccess = {
removeActivity()
visibleDeleteDialog.value = false
},
disclaimerFirst = R.string.confirm_delete_activity,
disclaimerSecond = R.string.confirm_delete_activity_text,
successName = R.string.delete_session
)
}
SwipeToDismiss(
state = dismissState,
directions = setOf(DismissDirection.EndToStart),
background = {
val direction =
dismissState.dismissDirection ?: return#SwipeToDismiss
val alignment = when (direction) {
DismissDirection.StartToEnd -> Alignment.CenterStart
DismissDirection.EndToStart -> CenterEnd
}
Box(
modifier = Modifier
.fillMaxSize()
.background(Black)
.padding(horizontal = Spacing_12),
contentAlignment = alignment
) {
Text(
text = stringResource(id = R.string.delete),
color = Color.Red,
fontSize = Text_16,
fontWeight = FontWeight.Bold
)
}
},
dismissContent = {
GameCard(activityItem)
}
)
}
}
}
I don't have the time to go over all of your code to make more changes that maybe will make it a better fit for your code and needs. but I am sure you are able to do so!
you can see my code when I had a similar problem maybe it will help to solve your problem.
itemsIndexed(celebsSearchList) { index, item ->
if (celebsSearchList.isEmpty()) {
println("no celebs")
} else {
val celebie = item
// celebsSearchList.forEach { celebie ->
Card(
modifier = Modifier
.fillMaxWidth()
.padding(start = 20.dp, end = 20.dp, top = 10.dp, bottom = 10.dp),
elevation = 8.dp
) {
Row(
modifier = Modifier
.fillMaxWidth()
.clip(MaterialTheme.shapes.medium),
horizontalArrangement = Arrangement.SpaceAround,
verticalAlignment = Alignment.CenterVertically
) {
Text(
text = celebie.FirstName + " " + celebie.LastName,
style = MaterialTheme.typography.h6
)
Button(
onClick = {
CelebForCelebProfile = celebie
navController.navigate(Screen.CelebProfileScreen.route)
}, colors = ButtonDefaults.buttonColors(
backgroundColor = Color.White, contentColor = Color.Gray
)
) {
Text(text = "View Profile")
}
}
}
}
}```
if you like me please upvote me :) help me gain more points on stack over flow and comment if you have any questions.
I made LazyColumn with vertical scroll bar and it's work good, but when i scroll mouse, column just jumping(not smooth), but when I scroll vert. bar, it's smooth
#ExperimentalFoundationApi
#OptIn(ExperimentalMaterial3Api::class, ExperimentalComposeUiApi::class)
#Composable
fun App() {
Box(modifier = Modifier.fillMaxSize().padding(10.dp)
) {
val animatedpr = remember { androidx.compose.animation.core.Animatable(initialValue = 0.8f) }
val stateVertical = rememberLazyListState(0)
LaunchedEffect(Unit){animatedpr.animateTo(targetValue = 1f, animationSpec = tween(300, easing = LinearEasing))}
LazyColumn(modifier = Modifier.fillMaxSize(), horizontalAlignment = Alignment.CenterHorizontally, state = stateVertical) {
items(1100) {
OutlinedCard(modifier = Modifier.size(500.dp, 100.dp).padding(20.dp).animateItemPlacement().graphicsLayer(scaleY = animatedpr.value, scaleX = animatedpr.value)) {
}
}
}
VerticalScrollbar(
modifier = Modifier.align(Alignment.CenterEnd).fillMaxHeight(),
adapter = rememberScrollbarAdapter(
scrollState = stateVertical
)
)
}
}
better sollution
if(it.changes.first().scrollDelta.y == 1.0f){
scope.launch { stateVertical.animateScrollBy(200.0f) }
}
else{
scope.launch {
scope.launch { stateVertical.animateScrollBy(-200.0f) }
}
}
The problem is that you use a single scroll state for two different scroll views. This is causing the jumping while recomposing.
I fixed it by adding "scrollhandler"(onPointerEvent(PointerEventType.Scroll))
val scope = rememberCoroutineScope() // coroutine for scroling(idk, i know coroutines very bad)
val stateVertical = rememberLazyListState(0)
.....
LazyColumn(modifier = Modifier.fillMaxSize().onPointerEvent(PointerEventType.Scroll){
var currentItem = stateVertical.layoutInfo.visibleItemsInfo[0].index
val itemsToScrolling = stateVertical.layoutInfo.visibleItemsInfo.size/2 // how many items we scrolling
if(it.changes.first().scrollDelta.y == 1.0f){ // scroll down
scope.launch { stateVertical.animateScrollToItem(currentItem+itemsToScrolling) }
}
else{ // scroll up
if(currentItem < itemsToScrolling){currentItem = itemsToScrolling} // because we cannot animate to negative number
scope.launch { stateVertical.animateScrollToItem(currentItem-itemsToScrolling) }
}
}, state = stateVertical){
*items*
}
I am having an issue where the accessibility focus is going to controls behind the PopUp Window when using a ExposedDropdownMenuBox
If there is a single ExposedDropdownMenuBox everything works as expected, but when I add a second ExposedDropdownMenuBox or another control the focus goes to the second ExposedDropdownMenuBox before going to the PopUp Window.
GIF of single dropdown behavior
https://giphy.com/gifs/gapy0XK1CGmbyltJxU
GIF of two dropdowns on the same screen
https://giphy.com/gifs/WkL5TcMWlumfcGHPmD
Source
#Composable
fun Screen() {
Column (
modifier = Modifier
.wrapContentSize(Alignment.TopCenter)
.padding(top = 48.dp)
) {
Text(
text = stringResource(id = R.string.greeting),
fontSize = 30.sp,
modifier = Modifier.padding(bottom = 24.dp)
)
LocaleDropdownMenu()
Spacer(modifier = Modifier.height(8.dp))
// LocaleDropdownMenu()
}
}
#OptIn(ExperimentalMaterialApi::class)
#Composable
fun LocaleDropdownMenu() {
val localeOptions = mapOf(
R.string.en to "en",
R.string.fr to "fr",
R.string.hi to "hi",
R.string.ja to "ja"
).mapKeys { stringResource(it.key) }
// boilerplate: https://developer.android.com/reference/kotlin/androidx/compose/material/package-summary#ExposedDropdownMenuBox(kotlin.Boolean,kotlin.Function1,androidx.compose.ui.Modifier,kotlin.Function1)
var expanded by remember { mutableStateOf(false) }
ExposedDropdownMenuBox(
expanded = expanded,
onExpandedChange = {
expanded = !expanded
}
) {
TextField(
readOnly = true,
value = stringResource(R.string.language),
onValueChange = { },
trailingIcon = {
ExposedDropdownMenuDefaults.TrailingIcon(
expanded = expanded
)
}
)
ExposedDropdownMenu(
expanded = expanded,
onDismissRequest = {
expanded = false
}
) {
localeOptions.keys.forEach { selectionLocale ->
DropdownMenuItem(
onClick = {
expanded = false
// set app locale given the user's selected locale
AppCompatDelegate.setApplicationLocales(
LocaleListCompat.forLanguageTags(
localeOptions[selectionLocale]
)
)
},
content = { Text(selectionLocale) }
)
}
}
}
}
A repository that reproduces this issue is here:
https://github.com/dazza5000/ExposedDropdownMenuBox-accessibility-issue
I'm struggling with implementing the UI component.
I want to achive something like this:
A box with only bottom shadow.
For now I'm able to add elevation but it add's shadow in every direction.
This is my current code and it's preview:
#Composable
fun PushNotificationsDisabledInfo(onTap: () -> Unit) {
Surface(
elevation = dimensionResource(R.dimen.card_elevation_big),
shape = RoundedCornerShape(dimensionResource(R.dimen.corner_radius_large)),
modifier = Modifier
.background(
color = colorResource(
id = R.color.white
)
)
.padding(dimensionResource(R.dimen.grid_2_5x))
) {
Box(
Modifier
.clip(shape = RoundedCornerShape(dimensionResource(R.dimen.corner_radius_large)))
.background(
color = colorResource(R.color.white)
)
.clickable(
onClick = { onTap() },
interactionSource = remember { MutableInteractionSource() },
indication = rememberRipple(bounded = true),
)
) {
Row(Modifier.padding(dimensionResource(R.dimen.grid_2x))) {
Image(
painter = painterResource(R.drawable.ic_error_big),
contentDescription = stringResource(R.string.empty_content_description),
modifier = Modifier.size(dimensionResource(R.dimen.grid_4x))
)
Spacer(modifier = Modifier.width(dimensionResource(R.dimen.grid_2x)))
Column {
Text(
text = stringResource(R.string.notifications_settings_push_notifications_disabled_title),
style = SiemensTextStyle.caption1,
color = colorResource(R.color.red)
)
Text(
text = stringResource(R.string.notifications_settings_push_notifications_disabled_message),
style = SiemensTextStyle.caption2,
color = colorResource(R.color.black)
)
}
}
}
}
}
Any ideas how to implement only bottom shadow using Compose?
Copy-paste the following extension & use it in your Card's Modifier:
private fun Modifier.bottomElevation(): Modifier = this.then(Modifier.drawWithContent {
val paddingPx = 8.dp.toPx()
clipRect(
left = 0f,
top = 0f,
right = size.width,
bottom = size.height + paddingPx
) {
this#drawWithContent.drawContent()
}
})
I'm having trouble going through tutorials of compose desktop.
I have two question regarding the basics of compose desktop.
First, is there a way to add elevation to the entire windows?
Second, is there a way to set the mininum size of the window?
When I click on the edge of the windows, even without any arrows showing, the windows can be resized, but I can't find a way to add constraints on it's minimum size. It will shink until it disappears.
My current preview looks like this.
My code looks like this.
fun main() = application {
val windowState = rememberWindowState()
windowState.size = WindowSize(392.dp, 642.dp)
var isVisible by remember { mutableStateOf(true) }
val trayState = rememberTrayState()
Tray(
state = trayState,
icon = TrayIcon,
hint = "PickMessengerV2",
onAction = {
isVisible = true
},
menu = {
Item(
"Exit",
onClick = {
exitApplication()
}
)
}
)
Window(
onCloseRequest = {
isVisible = false
},
state = windowState,
undecorated = true,
resizable = true,
visible = isVisible
) {
AppTheme(darkTheme = false) {
Row(modifier = Modifier.fillMaxSize()) {
var menuIdx by remember {
mutableStateOf(value = 0)
}
WindowDraggableArea {
Column(
modifier = Modifier.width(67.dp).fillMaxHeight().background(Color(0xFFf3f5f6)),
horizontalAlignment = Alignment.CenterHorizontally
) {
val onClick: (Int) -> Unit = { myIndex ->
menuIdx = myIndex
}
mainMenu(
menuIdx,
0,
listOf("LeftMenu_friend_ON.png", "LeftMenu_friend_OFF.png"),
marginTop = 36,
onClick = onClick
)
mainMenu(
menuIdx,
1,
listOf("LeftMenu_ Orga_ON.png", "LeftMenu_ Orga_OFF.png"),
onClick = onClick
)
mainMenu(
menuIdx,
2,
listOf("LeftMenu_chat_ON.png", "LeftMenu_chat_OFF.png"),
onClick = onClick
)
mainMenu(
menuIdx,
3,
listOf("LeftMenu_letter_OFF_2.png", "LeftMenu_letter_OFF.png"),
onClick = onClick
)
mainMenu(
menuIdx,
4,
listOf("LeftMenu_file_ON.png", "LeftMenu_file_OFF.png"),
onClick = onClick
)
mainMenu(
menuIdx,
5,
listOf("LeftMenu_ Coop_ON.png", "LeftMenu_ Coop_OFF.png"),
onClick = onClick
)
mainMenu(
menuIdx,
6,
listOf("LeftMenu_bnad_ON.png", "LeftMenu_band_OFF.png"),
onClick = onClick
)
}
}
Column(modifier = Modifier.fillMaxSize().background(MaterialTheme.colors.background)) {
WindowDraggableArea{
Row(
modifier = Modifier.fillMaxWidth().height(31.dp),
verticalAlignment = Alignment.CenterVertically
) {
Spacer(modifier = Modifier.weight(1f))
Box(modifier = Modifier.fillMaxHeight().clickable {
windowState.isMinimized = true
}.padding(start = 6.dp, end = 6.dp), contentAlignment = Alignment.Center) {
NsImage("win_1.png")
}
Box(modifier = Modifier.fillMaxHeight().clickable {
windowState.placement = if(windowState.placement == WindowPlacement.Fullscreen){
WindowPlacement.Floating
}else {
WindowPlacement.Fullscreen
}
}.padding(start = 6.dp, end = 6.dp), contentAlignment = Alignment.Center) {
NsImage("win_2.png")
}
Box(modifier = Modifier.fillMaxHeight().clickable {
isVisible = false
}.padding(start = 6.dp, end = 6.dp), contentAlignment = Alignment.Center) {
NsImage("win_3.png")
}
Spacer(modifier = Modifier.width(6.dp))
}
}
}
}
}
}
}
Here's the solution for minimum window size:
You need to access the AWT API through the FrameWindowScope
fun FrameWindowScope.setMinSize() {
window.minimumSize = Dimension(yourMinWidth, yourMinHeight)
}
And then you just call the setMinSize() in your Window function.