Cant understanding why it's not working...
another analog project bnv working correctly...
in-app app:startDestination="#+id/navigate_hot" work correctly, but on click bnv items - nothing happens
mainActivity.XML
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bttm_nav"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#color/white"
android:elevation="4dp"
app:itemIconSize="35dp"
app:itemTextColor="#color/bnv_tab_item_foreground"
app:itemIconTint="#color/bnv_tab_item_foreground"
app:layout_constraintBottom_toBottomOf="parent"
app:menu="#menu/bottom_nav_menu"/>
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<fragment
android:id="#+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="#navigation/bnv_navigation" />
MainActivity.kt
override fun onSupportNavigateUp(): Boolean {
return findNavController(this#ActivityMain,R. id.nav_host_fragment).navigateUp()
}
private fun setupNavigation() {
val navController = findNavController(this#ActivityMain, R.id.nav_host_fragment)
bttm_nav.setupWithNavController(navController)
}
bnv_navigation.XML
<navigation 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/nav_graph"
app:startDestination="#+id/navigate_hot">
<fragment
android:id="#+id/navigate_news"
android:name="com.example.fatsale.fragments.NewsFragment"
android:label="News"
tools:layout="#layout/fragment_news" />
<fragment
android:id="#+id/navigate_hot"
android:name="com.example.fatsale.fragments.HotFragment"
android:label="Collection"
tools:layout="#layout/fragment_hot" />
<fragment
android:id="#+id/navigate_profile"
android:name="com.example.fatsale.fragments.ProfileFragment"
android:label="Profile"
tools:layout="#layout/fragment_profile" />
</navigation>
tell this dunkey what need to do..
In your menu file for bottomnavigationview, each item id has to same as fragment id which is added in navigation graph xml file. Also in menu file, Each item has to give OrderInCategory for fragment position no in bottomnavigationview.
Related
I am trying to implement show/hide behavior of bottom navigation and collapsible toolbar from a specific fragment of bottom navigation.
Here is my activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="#+id/nav_host_fragment_activity_main"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
app:layout_constraintBottom_toTopOf="#id/nav_view"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="#navigation/mobile_navigation" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/nav_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="?android:attr/windowBackground"
app:layout_behavior="#string/hide_bottom_view_on_scroll_behavior"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="#menu/bottom_nav_menu" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
And here is the fragment_home.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.home.HomeFragment">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/purple_200">
<com.google.android.material.appbar.CollapsingToolbarLayout
android:id="#+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways|snap">
<TextView
android:id="#+id/text_header"
android:layout_width="match_parent"
android:layout_height="150dp"
android:gravity="center"
android:text="Parallax Area"
android:textSize="20sp" />
</com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recycler"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="4dp"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Currently when I scroll, only 'Parallax Area (pink)' is showing/hiding but not bottom navigation in sync. When I remove the '#+id/text_header' then my bottom navigation also shows/hides. I want to do it for only this fragment, not for the others. I came across with nested coordinator layout implementations, none of them worked.
How can I solve this? Thanks in advance.
I just found the solution to show/hide the BottomNavigation in an Activity when scrolling a RecyclerView in a Fragment.
The core idea is to set HideBottomViewOnScrollBehavior
programmatically when you're navigating Fragments
First of all, if you're using Navigation component from Jetpack, use addOnDestinationChangedListener in the Activity witch holds the nav host to get the current showing Fragment.
navController.addOnDestinationChangedListener { _, destination, _ ->
when (destination.id) {
R.id.scrollToHideFragment -> {
toggleBottomNavBehavior(true)
}
else -> {
toggleBottomNavBehavior(false)
}
}
}
In the above code, toggleBottomNavBehavior will be called when the destination has changed.
Add toggleBottomNavBehavior method in the Activity
private fun toggleBottomNavBehavior(scrollToHide: Boolean) {
// Get the layout params of your BottomNavigation
val layoutParams = binding.bottomNav.layoutParams as CoordinatorLayout.LayoutParams
// Apply LayoutBehavior to the layout params
layoutParams.behavior =
if (scrollToHide) HideBottomViewOnScrollBehavior<CoordinatorLayout>() else null
// Add margin to Nav Host Fragment if the layout behavior is NOT added
// bottom_nav_height is 56dp when using Material BottomNavigation
binding.navHostMain.updateLayoutParams<ViewGroup.MarginLayoutParams> {
setMargins(
0, 0, 0, if (scrollToHide) 0 else resources.getDimensionPixelSize(R.dimen.bottom_nav_height)
)
}
}
Now you can scroll in a Fragment and hide the BottomNav in the Activity!
I have nav host fragment and bottom navigation view inside home fragment as below
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="home.HomeFragment2">
<androidx.fragment.app.FragmentContainerView
android:id="#+id/homeNavHost"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="#+id/bottomMenu"
app:layout_constraintEnd_toEndOf="parent"
app:navGraph="#navigation/staging_menu_navigation1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottomMenu"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:labelVisibilityMode="labeled"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:menu="#menu/bottom_menu" />
</androidx.constraintlayout.widget.ConstraintLayout>
The menu as
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/staging_dashboard_nav_graph"
android:icon="#drawable/ic_home"
android:title="#string/menu_dashboard" />
<item
android:id="#+id/staging_offer_nav_graph"
android:icon="#drawable/ic_offer"
android:title="#string/menu_offers" />
<item
android:id="#+id/staging_profile_nav_graph"
android:icon="#drawable/ic_profile"
android:title="#string/menu_profile" />
</menu>
The navigation graph as
<navigation 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/staging_menu_navigation"
app:startDestination="#id/dashboardFragment3">
<include app:graph="#navigation/staging_dashboard_nav_graph" />
<include app:graph="#navigation/staging_offer_nav_graph" />
<include app:graph="#navigation/staging_profile_nav_graph" />
<fragment
android:id="#+id/dashboardFragment3"
android:name="com.octave.dashboard.DashboardFragment"
android:label="DashboardFragment" />
</navigation>
The other graphs are as
<navigation 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/staging_dashboard_nav_graph"
app:startDestination="#id/dashboardFragment">
<fragment
android:id="#+id/dashboardFragment"
android:name="com.octave.dashboard.DashboardFragment"
android:label="dashboard_fragment"
tools:layout="#layout/dashboard_fragment" />
</navigation>
Offer graph
<navigation 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/staging_offer_nav_graph"
app:startDestination="#id/offersFragment">
<fragment
android:id="#+id/offersFragment"
android:name="com.octave.offers.OffersFragment"
android:label="offers_fragment"
tools:layout="#layout/offers_fragment" />
</navigation>
Profile graph
<navigation 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/staging_profile_nav_graph"
app:startDestination="#id/profileFragment">
<fragment
android:id="#+id/profileFragment"
android:name="com.octave.profile.ProfileFragment"
android:label="profile_fragment"
tools:layout="#layout/profile_fragment" />
In my home fragment I am setting up as
binding.bottomMenu.setupWithNavController(Navigation.findNavController(requireActivity(), R.id.homeNavHost))
When I tap on bottom menu items the fragment doesn't change.
What is wrong over here?
If you are trying to create another nav graph with the bottom navigation bar that is already a part of the default nav graph, you first need to change the defaultNavHost value to false in XML of wherever your nav host fragment is in. then find NavHost to get NavController:
val navHost = childFragmentManager.findFragmentById(R.id.${yourNavHostFragment} as NavHostFragment
val navController = navHost.navController
view.findViewById<BottomNavigationView>(R.id.bottomNavigationView).setupWithNavController(navController!!)
No drawer view found with gravity LEFT occurs whenever I click on the menu items in the navigation view. I am using a custom App bar. I am using openDrawer and closeDrawer functions. Below I pasting the code please help I am stuck on this for a week.
property.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".views.enforcement.PropertyActivity">
<include
android:id="#+id/header"
layout="#layout/property_header" />
<androidx.drawerlayout.widget.DrawerLayout
android:id="#+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:openDrawer="end">
<fragment
android:id="#+id/fragmentContainerView"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:navGraph="#navigation/property_nav" />
<com.google.android.material.navigation.NavigationView
android:id="#+id/navigationView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="end"
android:background="#color/white"
app:itemBackground="#color/white"
app:itemIconTint="#color/color_light_blue"
app:itemTextColor="#color/color_light_blue"
app:menu="#menu/drawer_menu" />
</androidx.drawerlayout.widget.DrawerLayout>
</LinearLayout>
property.class
private fun initView() {
navController = findNavController(R.id.fragmentContainerView)
mDrawerLayout = findViewById<DrawerLayout>(R.id.drawerLayout)
navigationView.setupWithNavController(navController)
binding.header.menuIV.setOnClickListener {
if (mDrawerLayout.isDrawerOpen(GravityCompat.END)) {
mDrawerLayout.closeDrawers()
} else {
mDrawerLayout.openDrawer(GravityCompat.END)
}
}
You need to be explicit with closeDrawers() because it is inherently Gravity.START but you are calling Gravity.END.
Instead call:
if (mDrawerLayout.isDrawerOpen(GravityCompat.END)) {
mDrawerLayout.closeDrawer(GravityCompat.END)
}
This question already has answers here:
FragmentContainerView using findNavController
(9 answers)
Closed 2 years ago.
I try to write an Android app which uses the Navigation Component from androidx, a toolbar and a drawer layout to slide in a setting menu from the left. I followed the guides Get started with the Navigation component and tried to add a top app bar and a setting using this guide Update UI components with NavigationUI.
When my apps starts, the following exception is thrown:
java.lang.IllegalStateException: Activity ....MainActivity#e686cd8 does not have a NavController set on 2131230993
Side remark: If I open nav_graph.xml in design mode, the hosts pane reports "No NavHostFragments found. This nav graph must be referenced from a NavHostFragment in a layout in order to be accessible." Maybe this error and the exception are related and share the same root cause. However, my navigation graph is referenced by a NavHostFragment though, see below.
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout 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/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.appcompat.widget.Toolbar
android:id="#+id/main_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:theme="?attr/actionBarTheme" />
<androidx.fragment.app.FragmentContainerView
android:id="#+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="#navigation/nav_graph" />
</LinearLayout>
<com.google.android.material.navigation.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:menu="#menu/drawer_view"
app:headerLayout="#layout/nav_header" />
</androidx.drawerlayout.widget.DrawerLayout>
This layout
contains a FragmentContainerView
references #navigation/nav_graph (pasted below)
contains a NavigationView
references #layout/nav_header (pasted below)
references #menu/drawer_view (pasted below)
nav_header.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.widget.LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="192dp"
android:background="?attr/colorPrimaryDark"
android:gravity="bottom"
android:padding="16dp"
android:theme="#style/ThemeOverlay.AppCompat.Dark">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/app_name"
android:textAppearance="#style/TextAppearance.AppCompat.Body1" />
</android.widget.LinearLayout>
drawer_view.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="#+id/nav_home"
android:icon="#drawable/ic_home"
android:title="#string/home"/>
<item
android:id="#+id/nav_settings"
android:icon="#drawable/ic_settings"
android:title="#string/settings" />
</group>
</menu>
fragment_home.xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".HomeFragment"/>
This fragment is referenced by nav_graph (pasted below) as the home fragment (start destination).
nav_graph.xml:
<?xml version="1.0" encoding="utf-8"?>
<navigation 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/nav_graph"
app:startDestination="#id/homeFragment">
<fragment
android:id="#+id/homeFragment"
android:name="de.mhnnet.lychee4android.HomeFragment"
android:label="fragment_home"
tools:layout="#layout/fragment_home" />
</navigation>
MainActivity.java:
protected void onCreate( Bundle savedInstanceState ) {
super.onCreate( savedInstanceState );
setContentView( R.layout.activity_main );
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment );
AppBarConfiguration appBarConfiguration =
new AppBarConfiguration.Builder(navController.getGraph()).build();
Toolbar toolbar = findViewById( R.id.main_toolbar );
NavigationUI.setupWithNavController( toolbar, navController, appBarConfiguration );
}
Further Remarks:
I already found some tips which says that androidx.fragment.app.FragmentContainerView should be replaced by fragment. However, this feels wrong as the linter recommends to use FragmentContainerView instead of fragment and the official Android docs use FragmentContainerView, too.
Okay to get Nav Controller in your activity either replace your
<androidx.fragment.app.FragmentContainerView
android:id="#+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="#navigation/nav_graph" />
with
<fragment
android:id="#+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="#navigation/nav_graph" />
But as the lint suggest to change fragment with FragmentContainerView Use the below code to get Nav Controller in your activity.
NavHostFragment navHostFragment = (NavHostFragment) getSupportFragmentManager().findFragmentById(R.id.nav_host_fragment);
if (navHostFragment != null) {
NavController navController = navHostFragment.getNavController();
// Setup NavigationUI here
}
When I clicked on the button this thing is happening
navigation destination com.android.example.cameraxbasic:id/action_camera_to_gallery is unknown to this NavController
This is my code in fragment
container.findViewById<ImageButton>(R.id.photo_view_button).setOnClickListener {
Navigation.findNavController(requireActivity(), R.id.fragment_container).navigate(
CameraFragmentDirections.actionCameraToGallery(outputDirectory.absolutePath))
}
This is a full error description
In my case , I am using HomeFragment Having FragmentOne and FragmentTwo in HomeFragment
<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".HomeFragment">
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="729dp"
android:fillViewport="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<androidx.fragment.app.FragmentContainerView
android:id="#+id/fragmentOne"
android:name="com.example.FragmentOne"
android:layout_width="match_parent"
android:layout_height="100dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.fragment.app.FragmentContainerView
android:id="#+id/fragmentTwo"
android:name="com.example.FragmentTwo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/fragmentCarosuel" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
my nav_main.xml file is like below:
<?xml version="1.0" encoding="utf-8"?>
<navigation 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/mobile_navigation"
app:startDestination="#+id/nav_home">
<fragment
android:id="#+id/nav_home"
android:name="com.example.HomeFragment"
android:label="#string/menu_home"
tools:layout="#layout/fragment_home">
<action
android:id="#+id/action_main_to_fragment_three"
app:destination="#id/fragmentThree" />
</fragment>
<fragment
android:id="#+id/fragmentThree"
android:name="com.example.FragmentThree"
android:label="fragment_three"
tools:layout="#layout/fragment_three" >
</fragment>
</navigation>
Now, I am Navigating from FragmentTwo any click event etc.:
NOTE: Please make sure while selection of navigation destingation in navGraph. Here i am using two sub fragment (FragmentOne and FragmentTwo) but my base fragment is HomeFragment. so i have to make action from HomeFragment. If anyone create destination from FragmentOne it is not accesible from HomeFragment and NavController did not identify.
HomeFragmentDirections.ActionMainToFragmentThree nextAction = HomeFragmentDirections.actionMainToFragmentThree();
NavController navController = NavHostFragment.findNavController(this);
navController.navigate(nextAction);
Sorry for poor English. Thanks