0

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)
    }
Al Nafis
  • 127
  • 9

1 Answers1

2

You misspelled the ID on your menu item: android:id="@+id/trasactionFragment". It should be android:id="@+id/transactionFragment" (note the n).

ianhanniballake
  • 191,609
  • 30
  • 470
  • 443