Im trying to make a bottom navigation view to control which fragment is displayed but the navigation view didn't detect any item selected. Here is my Activity Code :
val fragmentMore = MoreMenuFragment()
val fragmentTransactionHistory = TransactionHistoryFragment()
val fragmentLicenseList = DaftarLisensiFragment()
replaceFragment(fragmentTransactionHistory)
binding.mainNavigationView.setOnItemSelectedListener {
when (it.itemId) {
R.id.menuTransactionHistoryFragment -> replaceFragment(fragmentTransactionHistory)
R.id.menuLicenseListFragment -> replaceFragment(fragmentLicenseList)
R.id.menuMoreFragment -> replaceFragment(fragmentMore)
}
true
}
Here is my 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=".presentation.view.MainActivity">
<com.google.android.material.appbar.MaterialToolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/second_color"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:subtitle="Management System"
app:subtitleTextColor="@color/white"
app:title="LeleStacia Store"
app:titleMarginTop="5dp"
app:titleTextColor="@color/white" />
<androidx.fragment.app.FragmentContainerView
android:id="@+id/fragmentContainer"
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="@color/fourth_color"
app:layout_constraintBottom_toTopOf="@id/mainNavigationView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/toolbar" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/mainNavigationView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/second_color"
style="@style/Widget.MaterialComponents.BottomNavigationView.Colored"
android:paddingTop="5dp"
android:paddingBottom="5dp"
app:itemIconTint="@color/white"
app:itemTextColor="@color/white"
app:labelVisibilityMode="labeled"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:menu="@menu/main_menu" />
</androidx.constraintlayout.widget.ConstraintLayout>
And here is my menu :
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<item
android:id="@+id/menuTransactionHistoryFragment"
android:icon="@drawable/ic_history"
android:title="Riwayat Transaksi"
tools:ignore="HardcodedText" />
<item
android:id="@+id/menuLicenseListFragment"
android:icon="@drawable/ic_storage"
android:title="Daftar Lisensi"
tools:ignore="HardcodedText" />
<item
android:id="@+id/menuMoreFragment"
android:icon="@drawable/ic_more"
android:title="More"
tools:ignore="HardcodedText" />
</menu>
I also put the log before to check if the navigation item got selected or not, but my function did not get called.
We don't have your "replaceFragment" function so here's how it should look like :
private fun replaceFragment(Check: Int, context: Context) {
//val frg: Fragment? = null
val fragment: Fragment
if(Check == 0)
fragment = FirstFragment(this)
else if(Check == 1) {
fragment = SecondFragment(this)
}else if(Check == 2) {
fragment = ThirdFragment(this)
}else{
fragment = FirstFragment(this)
}
val repo = Repository()
repo.YourRepoFunc{
val transaction = supportFragmentManager.beginTransaction()
transaction.replace(R.id.fragment_layout, fragment)
transaction.addToBackStack(null)
transaction.attach(fragment)
transaction.commit()
}
}