Where should I setup AppBarConfiguration for DrawerLayout - android

In this guide, it says:
AppBarConfiguration appBarConfiguration =
new AppBarConfiguration.Builder(navController.getGraph())
.setDrawerLayout(drawerLayout)
.build();
but it does not say how navController is obtained. In the next paragraph, it says
call setupWithNavController() from your main activity's onCreate() method, as shown below:
NavHostFragment navHostFragment = supportFragmentManager.findFragmentById(R.id.nav_host_fragment);
NavController navController = navHostFragment.getNavController();
NavigationView navView = findViewById(R.id.nav_view);
NavigationUI.setupWithNavController(navView, navController);
which indicates that the 2 snippets are in different files. So where should I place the first snippet (initializing appBarConfiguration)? Currently, I put them all in my main activity, and it seems to work fine.

you have to attach the appBarConfiguration to the NavigationUI with NavController using setupActionBarWithNavController() method. Code below shows an example,
AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
setupWithNavController() method is to attach NavigationView to NavigationUI

Related

Toolbar title disappears after launching fragment using NavGraph

I have setup the toolbar title in NavGraph with
`private void setUpNavigationToolbar(){
final NavHostFragment navHostFragment = (NavHostFragment) getSupportFragmentManager().findFragmentById(R.id.container_fragment;
NavController navController = navHostFragment.getNavController();
AppBarConfiguration appBarConfiguration =
new AppBarConfiguration.Builder(navController.getGraph()).build();
NavigationUI.setupWithNavController(
toolbar, navController, appBarConfiguration);
}`
and toolbar title is set using
android:label="Sample title" inside the Nav graph
When the fragment is launched the title appears and disappear in a moment.
I am looking for a solution for the toolbar title persists even after launching the fragment

Conditional navigation start destination in jetpack compose

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) }

Nothing is displayed on the screen when using AnimatedNavHost

I am using Accompanist animation library:
implementation "com.google.accompanist:accompanist-navigation-animation:0.24.0-alpha"
And I have the following AnimatedNavHost:
val navController = rememberAnimatedNavController()
AnimatedNavHost(
navController = navController,
startDestination = "auth",
enterTransition = { EnterTransition.None },
exitTransition = { ExitTransition.None }
) {
composable(
route = "auth"
) {
AuthScreen(
navController = navController
)
}
composable(
route = "profile"
) {
ProfileScreen(
navController = navController
)
}
}
My AuthScreen is as simple as:
Box(
modifier = Modifier.fillMaxSize().padding(bottom = 48.dp),
contentAlignment = Alignment.BottomCenter
) {
Button(
onClick = {
signIn()
}
) {
Text(
text = SIGN_IN,
fontSize = 18.sp
)
}
When I launch the app nothing is displayed on the screen? No crash. Can anyone help? Thanks
P.S. I'm also using:
implementation "androidx.hilt:hilt-navigation-compose:1.0.0-rc01"
I'm using the following imports:
import com.google.accompanist.navigation.animation.AnimatedNavHost
import com.google.accompanist.navigation.animation.rememberAnimatedNavController
import androidx.compose.animation.EnterTransition
import androidx.compose.animation.ExitTransition
As per the Accompanist Navigation Animation migration guide:
To migrate from using the Navigation Compose APIs do the following:
Replace rememberNavController() with rememberAnimatedNavController()
Replace NavHost with AnimatedNavHost
Replace import androidx.navigation.compose.navigation with import com.google.accompanist.navigation.animation.navigation
Replace import androidx.navigation.compose.composable with import com.google.accompanist.navigation.animation.composable
I suspect you haven't actually done the last one - you need to use the Accompanist version of the composable NavGraphBuilder extension if you want your destination to be picked up by AnimatedNavHost.

Can't remove hamburguer icon from Toolbar

im trying to remove the hamburguer icon because I already have one in my custom Toolbar, for remove it I have this on the activity:
Toolbar toolbar = findViewById(R.id.toolbar_main);
setSupportActionBar(toolbar);
final DrawerLayout drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
mAppBarConfiguration = new AppBarConfiguration.Builder(
R.id.navigation_dieta, R.id.navigation_calendario, R.id.navigation_alimentos, R.id.navigation_perfil)
.setDrawerLayout(drawer)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);
BottomNavigationView bottomNavigationView = findViewById(R.id.bottom_nav_view);
NavigationUI.setupWithNavController(bottomNavigationView, navController);
toolbar.setNavigationIcon(null);
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
getSupportActionBar().setHomeButtonEnabled(false);
drawerIcon = findViewById(R.id.toolbar_main_menu_icon);
drawerIcon.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
drawer.openDrawer(GravityCompat.START);
}
});
The icon is hidden at startup, but when I select a menu item it reappears.
After more than 10 hours I found the answer, just delete this:
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);

How to scroll-to-top by tap the bottom navigation icon?

my image
Hello guys
I use templater Bottom Navigation Activity and dont know how to scroll to top RecyclerView in my Home Fragment
My MainActivity code:
AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
NavigationUI.setupWithNavController(navView, navController);
The question is quit old, but maybe it helps others.
This is the code you have to put in the fragment (in Kotlin):
val nav = activity?.findViewById<BottomNavigationView>(R.id.bottom_navigation)
nav?.setOnItemReselectedListener { item ->
if (item.itemId == R.id.mainFragment) {
recyclerView.smoothScrollToPosition(position)
}
}

Categories

Resources