Is there any option to disable the CrossFade animation when navigating in Jetpack Compose?
In the source code, I see that the CrossFade is simply hardcoded, and in AnimatedNavHost from "accompanist" I can't locate such an option.
After digging i found the following solution:
In the AnimatedNavHost composable, we need to specify 'empty' transitions.
AnimatedNavHost(
navController = navController,
startDestination = "main",
enterTransition = { _, _ -> EnterTransition.None},
exitTransition = { _, _ -> ExitTransition.None}
)
Related
I am using navigation-compose along with bottom bar in jetpack compose. I want to show different bottom bar for different type of user. For that, I need to set conditional startDestination in NavHost. How do i do that?
I've tried below but it change startDestination but does not reflect in UI.
val user by remember { mutableStateOf(viewModel.user) }.collectAsState()
var startDestination = Screens.UserType1.route
LaunchedEffect(user) {
startDestination = if (loggedInUser?.userType == UserType.Seller) Screens.SellerHome.route else Screens.BuyerHome.route
}
While below code throws java.util.NoSuchElementException: List contains no element matching the predicate.
when (user?.userType) {
UserType.Seller -> {
startDestination = Screens.SellerHome.route
}
UserType.Buyer -> {
startDestination = Screens.BuyerHome.route
}
else -> {}
}
My NavHost
NavHost(
modifier = modifier,
navController = navController,
startDestination = startDestination
) {
...
}
Change this
var startDestination = Screens.UserType1.route
To this
var startDestination by remember { mutableStateOf(Screens.UserType1.route) }
How to handle Single NavHost in Single Activity approach in Multi Module structure in Android Jetpack compose.
Did not see any example with below scenario to handle single NavHost in Single Activity.
Navigation should start from splash screen.
Custom Splash Screen -> Sign in Screen -> Home Screen with BottomBar
NavHost(navController, startDestination = "Splash")
{
composable(route = "Splash") {
Splash()
}
composable(route = "Sign In") {
SignIn()
}
composable(route = "Home") {
Home()
}
}
BottomBar has 3 options to navigate between each bottom bar items.
Where to add BottomBar in above scenario?
Updated as per comments
Scaffold(
bottomBar = { BottomBar(navController = navController) }) {
// sub graph for Home?
}
I have a NavHost that looks something like this:
NavHost(
navController = navController,
startDestination = Screen.MAIN.route,
modifier = modifier
) {
dialog(
Screen.LOGIN.route,
dialogProperties = DialogProperties(
usePlatformDefaultWidth = false,
dismissOnBackPress = false,
dismissOnClickOutside = false
)
) { LoginScreen() }
composable(Screen.MAIN.route) { MainScreen() }
}
For composable screen I can configure my MainActivity's windowSoftInputMode to adjustResize as well as other parameters like:
WindowCompat.setDecorFitsSystemWindows(window, false)
But I can't seem to find a way to configure the way my dialog is displayed. It seems to use adjustPan functionality, as I can see, that when keyboard appears it pushes status bar up. How can I configure this dialog, or how can I at least change windowSoftInputMode of mentioned dialog?
EDIT
I tried accessing window from context and explicitly setting required parameters, but it doesn't seem to have any effect
val context = LocalContext.current
SideEffect {
val window = context.findWindow()!!
WindowCompat.setDecorFitsSystemWindows(window, false)
window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE)
}
I have started trying out Navigation for compose.
I created my 2 Composables and everything is working fine.
But what I'm missing is Animations (or Transitions) between the pages. I didn't find any resources pointing out how to do it in Compose.
I know all animations are based on states in Compose, but the only thing I know is the Navigation Back Stack.
You can use the composable I made to show enter animation (configure preferable effects in "enter" and "exit" parameters)
fun EnterAnimation(content: #Composable () -> Unit) {
AnimatedVisibility(
visible = true,
enter = slideInVertically(
initialOffsetY = { -40 }
) + expandVertically(
expandFrom = Alignment.Top
) + fadeIn(initialAlpha = 0.3f),
exit = slideOutVertically() + shrinkVertically() + fadeOut(),
content = content,
initiallyVisible = false
)
}
You can use it like this:
NavHost(
navController = navController,
startDestination = "dest1"
) {
composable("dest1") {
EnterAnimation {
FirstScreen(navController)
}
}
composable("dest2") {
EnterAnimation {
SecondScreen(navController)
}
}
}
Accompanist version 0.16.1 and above supports animation between destinations. Here is the article for more info.
implementation("com.google.accompanist:accompanist-navigation-animation:0.16.1")
import com.google.accompanist.navigation.animation.AnimatedNavHost
import com.google.accompanist.navigation.animation.composable
import com.google.accompanist.navigation.animation.rememberAnimatedNavController
val navController = rememberAnimatedNavController()
AnimatedNavHost(navController, startDestination = "first") {
composable(
route = "first",
enterTransition = { _, _ -> slideInHorizontally(animationSpec = tween(500)) },
exitTransition = { _, _ -> slideOutHorizontally(animationSpec = tween(500)) }
) {
FirstScreen()
}
composable(
route = "second",
enterTransition = { _, _ -> slideInHorizontally(initialOffsetX = { it / 2 }, animationSpec = tween(500)) },
exitTransition = { _, _ -> slideOutHorizontally(targetOffsetX = { it / 2 }, animationSpec = tween(500)) }
) {
SecondScreen()
}
}
Result:
In alpha-09 this is not supported. :(
Please, star this issue: https://issuetracker.google.com/issues/172112072
Due to yesterday's update (version 2.4.0-alpha05):
Navigation Compose’s NavHost now always uses Crossfades when navigating through destinations.
Lately Google Accompanist has added a library which provides Compose Animation support for Jetpack Navigation Compose.. Do check it out. 👍🏻
https://github.com/google/accompanist/tree/main/navigation-animation
You can use the library I did. It provides easy navigation and is written using AnimatedVisibility so you can use compose transitions to animate any screen state (enter, exit)
https://github.com/vldi01/AndroidComposeRouting
How can I use combination of Lottie json animation and Jetpack Compose UI in Android?
I already tried AndroidView(resId = R.layout.animation) where is com.airbnb.lottie.LottieAnimationView with field lottie_rawRes but is it the best way?
Lottie compose has been released:
Dependency:
implementation "com.airbnb.android:lottie-compose:$lottie_version"
Basic usage:
val composition by rememberLottieComposition(LottieCompositionSpec.RawRes(R.raw.my_animation))
LottieAnimation(composition)
As of compose version alpha05, Lottie seems to be working fine:
#Composable
fun CustomView(modifier: Modifier = Modifier
.fillMaxWidth()) {
val visibility = remember { mutableStateOf(0) }
val context = ContextAmbient.current
val customView = remember { LottieAnimationView(context) }
// Adds view to Compose
AndroidView({ customView }) { view ->
// View's been inflated - add logic here if necessary
with(view) {
setAnimation(R.raw.choose)
playAnimation()
repeatMode = LottieDrawable.INFINITE
foregroundGravity = Gravity.CENTER
}
}
}
First class support for Lottie has not been implemented yet but it will be eventually.
I'm not really sure if it is the best way to do it, but that's the current recommmended way to include an Android View in a #Composable function.
#Composable
fun <T : View> AndroidView(
viewBlock: (Context) -> T,
modifier: Modifier = Modifier,
update: (T) -> Unit = NoOpUpdate
)
It seems that they are working on it: https://github.com/airbnb/lottie-android/commits/gpeal/compose-1