So i just got started with android development a couple weeks ago, while trying to make navigation between two fragments with a bottom nav bar, for some reasons the click listener is just not working, no errors, no warnings, compiles no problem but it might be some logic problem? here's the code:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val fragmentHome = HomeFragment()
val fragmentProfile = ProfileFragment()
replaceCurrentFragment(fragmentHome)
NavigationBarView.OnItemSelectedListener { item ->
when(item.itemId) {
R.id.page_home -> {
Log.i("NavBar","Home pressed")
replaceCurrentFragment(fragmentHome)
true
}
R.id.page_profile -> {
Log.i("NavBar","Profile pressed")
replaceCurrentFragment(fragmentProfile)
true
}
else -> {
Log.i("NavBar","Error?")
false
}
}
}
}
private fun replaceCurrentFragment(fragment: Fragment) =
supportFragmentManager.beginTransaction().apply {
replace(R.id.flFragment, fragment)
commit()
}
}
Layout:
<?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">
<FrameLayout
android:id="@ id/flFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@ id/bottom_navigation"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.google.android.material.bottomnavigation.BottomNavigationView
app:labelVisibilityMode="selected"
android:id="@ id/bottom_navigation"
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/bottom_navigation_menu" />
</androidx.constraintlayout.widget.ConstraintLayout>
Menu:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@ id/page_home"
android:icon="@drawable/ic_home"
android:title="Home"/>
<item
android:id="@ id/page_profile"
android:icon="@drawable/ic_profile"
android:title="Profile"/>
</menu>
the main page does get initialized on the home fragment, but when i click the nav bar buttons, nothing happens so the problem is in that listener part, copy pasted from the docs yet it doesn't work.
i also have a bug where the layout editor doesn't show the nav bar contents, so my ide might be glitched? would appreciate any help.
CodePudding user response:
Replace "NavigationBarView" with your actual bottomNavigationView id.
This would work,
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val fragmentHome = HomeFragment()
val fragmentProfile = ProfileFragment()
replaceCurrentFragment(fragmentHome)
val myBottomNavigationView = findViewById<BottomNavigationView>(R.id.bottom_navigation)
myBottomNavigationView.OnItemSelectedListener { item ->
when(item.itemId) {
R.id.page_home -> {
Log.i("NavBar","Home pressed")
replaceCurrentFragment(fragmentHome)
true
}
R.id.page_profile -> {
Log.i("NavBar","Profile pressed")
replaceCurrentFragment(fragmentProfile)
true
}
else -> {
Log.i("NavBar","Error?")
false
}
}
}
}
private fun replaceCurrentFragment(fragment: Fragment) =
supportFragmentManager.beginTransaction().apply {
replace(R.id.flFragment, fragment)
commit()
}
}