Search code examples
androidkotlinbottomnavigationviewandroid-bottomnav

Android Kotlin - Material Bottom Navigation Disable Shift Mode and Selete Menu


  1. I want to Disable Shift Mode with kotlin, I used material BottomNavigationView

     <com.google.android.material.bottomnavigation.BottomNavigationView
     android:id="@+id/bottom_navigation"
     android:layout_width="0dp"
     android:layout_height="wrap_content"
     android:layout_gravity="bottom"
     app:itemIconSize="@dimen/iconSize"
     android:background="?android:attr/windowBackground"
     app:layout_constraintBottom_toBottomOf="parent"
     app:layout_constraintEnd_toEndOf="parent"
     app:layout_constraintStart_toStartOf="parent"
     app:menu="@menu/btm_nav_menu"
     app:labelVisibilityMode="labeled"
     />
    
  2. I want click account icon and start activity login when click back button homeFragment will be selected not account icon selected

    bottom_navigation.setOnNavigationItemSelectedListener {
         when(it.itemId) {
             R.id.homeFragment -> showNav(HomeFragment())
             R.id.accountFragment -> { bottom_navigation.menu.findItem(R.id.homeFragment).setCheckable(true)
                 startActivity(Intent(this, MainActivity2::class.java)) }
         }
         true
    }
    
    
    fun showNav(fragment: Fragment){ 
         supportFragmentManager.beginTransaction().apply {
               replace(R.id.fragment_container, fragment)
               commit()
         }
    }
    

Solution

  • Answer 2. Use false, Bottom Navigation will not select the icon

    bottom_navigation.setOnNavigationItemSelectedListener {
            when(it.itemId) {
                R.id.homeFragment -> {
                    showNav(HomeFragment())
                    true
                }
                R.id.accountFragment -> {
                    startActivity(Intent(this, MainActivity2::class.java))
                    false
                } else -> true
            }
        }