Android bottom navigation not changing destination - android

I was trying to implement a simple bottom navigation view using android's navigation, but I think I'm missing a step.
Here's what I did:
At first I created four fragments. These fragments contain only a text view.
Then I created a navigation graph. The code is as follows:
<?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/main_nav_graph"
app:startDestination="#id/homeFragment">
<fragment
android:id="#+id/homeFragment"
android:name="com.baba_abdullah.Project.fragment.HomeFragment"
android:label="fragment_home"
tools:layout="#layout/fragment_home" />
<fragment
android:id="#+id/addBooksFragment"
android:name="com.example.Project.fragment.AddBooksFragment"
android:label="fragment_add_books"
tools:layout="#layout/fragment_add_books" />
<fragment
android:id="#+id/chatFragment"
android:name="com.example.Project.fragment.ChatFragment"
android:label="fragment_chat"
tools:layout="#layout/fragment_chat" />
<fragment
android:id="#+id/profileFragment"
android:name="com.example.Project.fragment.ProfileFragment"
android:label="fragment_profile"
tools:layout="#layout/fragment_profile" />
</navigation>
Menu file:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/home_fragment"
android:icon="#drawable/ic_baseline_home_24"
android:title="#string/home_menu_title"/>
<item
android:id="#+id/add_book_fragment"
android:icon="#drawable/ic_baseline_add_circle_outline_24"
android:title="#string/add_book_menu_title"/>
<item
android:id="#+id/chat_fragment"
android:icon="#drawable/ic_baseline_chat_bubble_outline_24"
android:title="#string/chat_menu_title"/>
<item
android:id="#+id/profile_fragment"
android:icon="#drawable/ic_baseline_account_circle_24"
android:title="#string/profile_menu_title"/>
</menu>
Inside the layout file of MainActivity, I added a FragmentContainer that would serve as NavHostFragment and a BottomNavigationView in the following way:
<?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:id="#+id/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.fragment.app.FragmentContainerView
android:id="#+id/main_fragment_container"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:layout_constraintBottom_toTopOf="#+id/bottom_nav_bar"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="#navigation/main_nav_graph" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottom_nav_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:menu="#menu/main_nav_menu" />
</androidx.constraintlayout.widget.ConstraintLayout>
And in the MainActivity, I added the following lines to tie them up together:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
NavHostFragment navHostFragment = (NavHostFragment) getSupportFragmentManager().findFragmentById(R.id.main_fragment_container);
NavController navController = navHostFragment.getNavController();
BottomNavigationView bottomNav = findViewById(R.id.bottom_nav_bar);
NavigationUI.setupWithNavController(bottomNav, navController);
}
Now, when I click on other menu item, it doesn't get selected and the destination view isn't changed either.
You can check the screen recording of the bahaviour of the output app.
What am I missing here?

to connect bottom navigation id of fragments most be equal with menu id, here is how need to be your navigation graph
<?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/main_nav_graph"
app:startDestination="#id/home_fragment">
<fragment
android:id="#+id/home_fragment"
android:name="com.baba_abdullah.Project.fragment.HomeFragment"
android:label="fragment_home"
tools:layout="#layout/fragment_home" />
<fragment
android:id="#+id/add_book_fragment"
android:name="com.example.Project.fragment.AddBooksFragment"
android:label="fragment_add_books"
tools:layout="#layout/fragment_add_books" />
<fragment
android:id="#+id/chat_fragment"
android:name="com.example.Project.fragment.ChatFragment"
android:label="fragment_chat"
tools:layout="#layout/fragment_chat" />
<fragment
android:id="#+id/profile_fragment"
android:name="com.example.Project.fragment.ProfileFragment"
android:label="fragment_profile"
tools:layout="#layout/fragment_profile" />
</navigation>

In Menu file replace menu items id with fragment id's. e.g
<item
android:id="#+id/homeFragment"
android:icon="#drawable/ic_baseline_home_24"
android:title="#string/home_menu_title"/>

Related

How does Navigation with BottomNavigation work? [duplicate]

I want to use the navigation and destination system in android so when a menu item in the bottom nav bar is clicked it goes to a fragment.
I created 3 empty fragments and followed this guide to tie the items to the fragments but when I click the menu items, nothing happens.
https://developer.android.com/guide/navigation/navigation-ui
I made sure that the item id from the menu has the same id as the fragment too.
how can I make this work?
Here is my Activity:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val navController = findNavController(R.id.nav_host_fragment)
bottom_navigation
.setupWithNavController(navController)
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
val navController = findNavController(R.id.nav_host_fragment)
return item.onNavDestinationSelected(navController) || super.onOptionsItemSelected(item)
}
}
This is the activity 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"
android:id="#+id/coordinator"
tools:context=".MainActivity">
<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/navigation" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="#color/colorPrimary"
app:itemIconTint="#color/colorPrimaryDark"
app:itemTextColor="#color/colorPrimaryDark"
app:menu="#menu/bottom_navigation_menu" />
And this is the bottom nav bar menu:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/fragment_home"
android:icon="#drawable/ic_home_24px"
android:title="Home" />
<item
android:id="#+id/nav_search"
android:icon="#drawable/ic_search_24px"
android:title="Search" />
<item
android:id="#+id/nav_profile"
android:icon="#drawable/ic_face_24px"
android:title="Profile" />
Navigation.xml code:
<?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/navigation"
app:startDestination="#id/homeFragment">
<fragment
android:id="#+id/homeFragment"
android:name="com.puntogris.herewego.home.HomeFragment"
android:label="fragment_home"
tools:layout="#layout/fragment_home" >
<action
android:id="#+id/action_homeFragment_to_profileFragment"
app:destination="#id/profileFragment" />
</fragment>
<fragment
android:id="#+id/profileFragment"
android:name="com.puntogris.herewego.profile.ProfileFragment"
android:label="fragment_profile"
tools:layout="#layout/fragment_profile" >
<action
android:id="#+id/action_profileFragment_to_searchFragment"
app:destination="#id/searchFragment" />
</fragment>
<fragment
android:id="#+id/searchFragment"
android:name="com.puntogris.herewego.search.SearchFragment"
android:label="fragment_search"
tools:layout="#layout/fragment_search" />
Your android:ids do not match.
Your menu XML uses android:id="#+id/fragment_home", but your navigation XML uses android:id="#+id/homeFragment".
These need to be the same name. For example, you could change your menu XML to be
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/homeFragment"
android:icon="#drawable/ic_home_24px"
android:title="Home" />
<item
android:id="#+id/searchFragment"
android:icon="#drawable/ic_search_24px"
android:title="Search" />
<item
android:id="#+id/profileFragment"
android:icon="#drawable/ic_face_24px"
android:title="Profile" />
</menu>

How to set Bottom navigation clickable in android studio

How to create bottom navigation with help of navigation resource and menu in android studio.
I was trying to create bottom navigations but I click another bottom navigations option its not working.
please help me and suggest me what I done wrong
app screen
MainActivity.java:
import androidx.appcompat.app.AppCompatActivity;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.NavigationUI;
import android.os.Bundle;
import com.google.android.material.bottomnavigation.BottomNavigationView;
public class MainActivity extends AppCompatActivity {
private BottomNavigationView bottomNavigationView;
private NavController navController;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bottomNavigationView = findViewById(R.id.bottomNavigationView);
navController = Navigation.findNavController(this, R.id.frame_layout);
NavigationUI.setupWithNavController(bottomNavigationView, navController);
}
}
main_activity.xml
<?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=".MainActivity">
<fragment
android:id="#+id/frame_layout"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
app:defaultNavHost="true"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
app:layout_constraintBottom_toTopOf="#+id/bottomNavigationView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="#navigation/mobile_navigation" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottomNavigationView"
android:layout_width="match_parent"
android:layout_height="50dp"
app:menu="#menu/bottom"
android:background="?android:attr/windowBackground"
app:labelVisibilityMode="selected"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
mobile_navigation.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/navigation"
app:startDestination="#id/galleryFragment">
<fragment
android:id="#+id/aboutFragment"
android:name="com.example.mycollege.ui.about.aboutFragment"
android:label="about"
tools:layout="#layout/fragment_about" />
<fragment
android:id="#+id/galleryFragment"
android:name="com.example.mycollege.ui.gallery.galleryFragment"
android:label="gallery"
tools:layout="#layout/fragment_gallery" />
<fragment
android:id="#+id/homeFragment"
android:name="com.example.mycollege.ui.home.homeFragment"
android:label="home"
tools:layout="#layout/fragment_home" />
<fragment
android:id="#+id/noticeFragment"
android:name="com.example.mycollege.ui.notice.noticeFragment"
android:label="notice"
tools:layout="#layout/fragment_notice" />
<fragment
android:id="#+id/facultyFragment"
android:name="com.example.mycollege.ui.faculty.facultyFragment"
android:label="faculty"
tools:layout="#layout/fragment_faculty" />
</navigation>
menu.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/home_navigation"
android:icon="#drawable/home"
android:iconTint="#color/white"
android:title="Home" />
<item
android:id="#+id/notice_navigation"
android:icon="#drawable/noticeboard"
android:iconTint="#color/white"
android:title="Notice" />
<item
android:id="#+id/faculty_navigation"
android:icon="#drawable/team"
android:iconTint="#color/white"
android:title="Faculty" />
<item
android:id="#+id/gallery_navigation"
android:icon="#drawable/gallery"
android:iconTint="#color/white"
android:title="Gallery" />
<item
android:id="#+id/about_navigation"
android:icon="#drawable/writing"
android:iconTint="#color/white"
android:title="About" />
</menu>
Make sure your menu items id are the same as fragments ids.
For instance, your fragment id is aboutFragment, then your menu item's id should be aboutFragment.

Clicking on the First Item on BottomTabBar Navigation Does Not Work on Android

I am using Android Jetpack with Kotlin to create this BottomBarNavigation UI using NavigationHostFragment, Navgraph and NavController.
I have 3 tabs and everything works as expected except when I try to click on the first tab, it does not do anything.
Got stuck for a few days. Can't find the issue. Any ideas?
bottom_tabbar_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"
android:id="#+id/bottom_tabbar_nav_graph"
app:startDestination="#id/transactionFragment">
<fragment
android:id="#+id/transactionFragment"
android:name="com.example.project_budget_planner.main.transaction.TransactionFragment"
android:label="fragment_transaction" />
<fragment
android:id="#+id/statisticsFragment"
android:name="com.example.project_budget_planner.main.statistics.StatisticsFragment"
android:label="fragment_statistics" />
<fragment
android:id="#+id/settingsFragment"
android:name="com.example.project_budget_planner.main.settings.SettingsFragment"
android:label="fragment_settings" />
</navigation>
bottom_tabbar_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/trasactionFragment"
android:icon="#drawable/common_google_signin_btn_icon_dark"
android:orderInCategory="1"
app:showAsAction="always"
android:title="Transaction" />
<item
android:id="#+id/statisticsFragment"
android:icon="#drawable/common_google_signin_btn_icon_dark"
android:orderInCategory="2"
app:showAsAction="always"
android:title="Statistics" />
<item
android:id="#+id/settingsFragment"
android:icon="#drawable/common_google_signin_btn_icon_dark"
android:orderInCategory="3"
app:showAsAction="always"
android:title="Settings" />
</menu>
activity_main.xml
<?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=".main.MainActivity">
<fragment
android:id="#+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
app:defaultNavHost="true"
app:layout_constraintBottom_toTopOf="#id/bottom_navbar"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="#navigation/bottom_tabbar_nav_graph" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottom_navbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ccc"
app:itemIconTint="#472B78"
app:itemTextColor="#472C67"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:menu="#menu/bottom_tabbar_menu" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.kt
private fun setupNavigation() {
val bottomNavigationView: BottomNavigationView = findViewById(R.id.bottom_navbar)
val navHostFragment: NavHostFragment =
supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment
NavigationUI.setupWithNavController(bottomNavigationView, navHostFragment.navController)
}
You misspelled the ID on your menu item: android:id="#+id/trasactionFragment". It should be android:id="#+id/transactionFragment" (note the n).

How to tie fragment destination to menu items in bottom nav bar?

I want to use the navigation and destination system in android so when a menu item in the bottom nav bar is clicked it goes to a fragment.
I created 3 empty fragments and followed this guide to tie the items to the fragments but when I click the menu items, nothing happens.
https://developer.android.com/guide/navigation/navigation-ui
I made sure that the item id from the menu has the same id as the fragment too.
how can I make this work?
Here is my Activity:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val navController = findNavController(R.id.nav_host_fragment)
bottom_navigation
.setupWithNavController(navController)
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
val navController = findNavController(R.id.nav_host_fragment)
return item.onNavDestinationSelected(navController) || super.onOptionsItemSelected(item)
}
}
This is the activity 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"
android:id="#+id/coordinator"
tools:context=".MainActivity">
<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/navigation" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="#color/colorPrimary"
app:itemIconTint="#color/colorPrimaryDark"
app:itemTextColor="#color/colorPrimaryDark"
app:menu="#menu/bottom_navigation_menu" />
And this is the bottom nav bar menu:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/fragment_home"
android:icon="#drawable/ic_home_24px"
android:title="Home" />
<item
android:id="#+id/nav_search"
android:icon="#drawable/ic_search_24px"
android:title="Search" />
<item
android:id="#+id/nav_profile"
android:icon="#drawable/ic_face_24px"
android:title="Profile" />
Navigation.xml code:
<?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/navigation"
app:startDestination="#id/homeFragment">
<fragment
android:id="#+id/homeFragment"
android:name="com.puntogris.herewego.home.HomeFragment"
android:label="fragment_home"
tools:layout="#layout/fragment_home" >
<action
android:id="#+id/action_homeFragment_to_profileFragment"
app:destination="#id/profileFragment" />
</fragment>
<fragment
android:id="#+id/profileFragment"
android:name="com.puntogris.herewego.profile.ProfileFragment"
android:label="fragment_profile"
tools:layout="#layout/fragment_profile" >
<action
android:id="#+id/action_profileFragment_to_searchFragment"
app:destination="#id/searchFragment" />
</fragment>
<fragment
android:id="#+id/searchFragment"
android:name="com.puntogris.herewego.search.SearchFragment"
android:label="fragment_search"
tools:layout="#layout/fragment_search" />
Your android:ids do not match.
Your menu XML uses android:id="#+id/fragment_home", but your navigation XML uses android:id="#+id/homeFragment".
These need to be the same name. For example, you could change your menu XML to be
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/homeFragment"
android:icon="#drawable/ic_home_24px"
android:title="Home" />
<item
android:id="#+id/searchFragment"
android:icon="#drawable/ic_search_24px"
android:title="Search" />
<item
android:id="#+id/profileFragment"
android:icon="#drawable/ic_face_24px"
android:title="Profile" />
</menu>

Bottom navigation doesn't show fragments in android jet pack navigation graph

I have this app with two activities,one is the host for login,signup and ... fragments and if the user signs up it goes to another activity which holds a bottom navigation and 5 fragments to switch.The problem is that the first activity works fine but when I lunch the second activity,The bottom navigation shows up and works but the contents of fragments doesn't show up!
here is my nav graph:
<?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/main_nav_graph"
app:startDestination="#id/homeFragment">
<fragment
android:id="#+id/homeFragment"
android:name="com.prototype.nyx.HomeFragment"
android:label="fragment_home"
tools:layout="#layout/fragment_home" >
<action
android:id="#+id/action_homeFragment_to_walletFragment"
app:destination="#id/walletFragment" />
</fragment>
<fragment
android:id="#+id/walletFragment"
android:name="com.prototype.nyx.WalletFragment"
android:label="fragment_wallet"
tools:layout="#layout/fragment_wallet" >
<action
android:id="#+id/action_walletFragment_to_addProductFragment"
app:destination="#id/addProductFragment" />
</fragment>
<fragment
android:id="#+id/addProductFragment"
android:name="com.prototype.nyx.AddProductFragment"
android:label="fragment_add_product"
tools:layout="#layout/fragment_add_product" >
<action
android:id="#+id/action_addProductFragment_to_settingsFragment"
app:destination="#id/settingsFragment" />
</fragment>
<fragment
android:id="#+id/settingsFragment"
android:name="com.prototype.nyx.SettingsFragment"
android:label="fragment_settings"
tools:layout="#layout/fragment_settings" >
<action
android:id="#+id/action_settingsFragment_to_profileFragment"
app:destination="#id/profileFragment" />
</fragment>
<fragment
android:id="#+id/profileFragment"
android:name="com.prototype.nyx.ProfileFragment"
android:label="fragment_profile"
tools:layout="#layout/fragment_profile" />
</navigation>
and here is my main activity and its layout:
package com.prototype.nyx;
import androidx.appcompat.app.AppCompatActivity;
import androidx.navigation.fragment.NavHostFragment;
import androidx.navigation.ui.NavigationUI;
import android.os.Bundle;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.ittianyu.bottomnavigationviewex.BottomNavigationViewEx;
public class MainActivity extends AppCompatActivity {
private BottomNavigationView bottomNavigationView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setUpNavigation();
}
public void setUpNavigation() {
bottomNavigationView = findViewById(R.id.bottom_nav);
NavHostFragment navHostFragment = (NavHostFragment) getSupportFragmentManager()
.findFragmentById(R.id.main_nav_host_fragment);
System.out.println("number of fragments is:"+navHostFragment.getChildFragmentManager()
.getFragments().size());
NavigationUI.setupWithNavController(bottomNavigationView,
navHostFragment.getNavController());
}
}
<?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"
android:background="#color/white"
tools:context=".MainActivity">
<fragment
android:id="#+id/main_nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:navGraph="#navigation/main_nav_graph" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottom_nav"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:menu="#menu/bottom_nav_menu" />
</androidx.constraintlayout.widget.ConstraintLayout>
and the menu:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#id/homeFragment"
android:icon="#drawable/ic_home"
app:showAsAction="always"
android:title="">
</item>
<item
android:id="#id/walletFragment"
android:icon="#drawable/ic_wallet"
app:showAsAction="always"
android:title="">
</item>
<item
android:id="#id/addProductFragment"
android:icon="#drawable/ic_add"
app:showAsAction="always"
android:title="">
</item>
<item
android:id="#+id/settingsFragment"
android:icon="#drawable/ic_settings"
app:showAsAction="always"
android:title="">
</item>
<item
android:id="#id/profileFragment"
android:icon="#drawable/ic_person_black_24dp"
app:showAsAction="always"
android:title="">
</item>
</menu>
At first I thought it was the main activity layout so I tried frame layout and coordinator layout too.
but nothing worked.
one of my fragments:
<?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">
<TextView
android:id="#+id/textView3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="#string/hello_blank_fragment" />
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="256dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.063"
tools:src="#tools:sample/avatars" />
</androidx.constraintlayout.widget.ConstraintLayout>
what is the problem?
try to check the package name of the fragment and fragment name attr in navgraph

Categories

Resources