Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to switch between tabs in BottomNavigationView programmatically in AndroidX?

I'm using the following activity layout with a fragment and a BottomNavigationView:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <fragment
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:id="@+id/fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        app:navGraph="@navigation/navigation_bottom"
        app:defaultNavHost="true"/>

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/bottom_navigation_view"
        app:menu="@menu/bottom_navigation_menu"/>
</LinearLayout>

And have defined three fragments in my navigation layout:

<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_bottom"
    app:startDestination="@id/firstFragment">

    <fragment
        android:id="@+id/firstFragment"
        android:name="fragments.FirstFragment"
        android:label="fragment_first"
        tools:layout="@layout/fragment_first" />

    //The other two fragments
</navigation>

Inside my activity, I'm using the following code:

navController = Navigation.findNavController(this, R.id.fragment);
BottomNavigationView bnw = findViewById(R.id.bottom_navigation_view);
NavigationUI.setupWithNavController(bnw, navController);
NavigationUI.setupActionBarWithNavController(this, navController);

And this my onSupportNavigateUp method:

@Override
public boolean onSupportNavigateUp() {
    return Navigation.findNavController(this, R.id.fragment).navigateUp();
}

But when I press on the second icon (fragment) nothing happens. How to solve this issue? Thanks!

like image 816
Johans Bormman Avatar asked Mar 01 '19 19:03

Johans Bormman


1 Answers

Solution 1:

In your menu(bottom_navigation_menu) item set same id as your fragment id generated by navigation graph like below:

<menu     xmlns:android="http://schemas.android.com.   /apk/res/android"

xmlns:app="http://schemas.android.com/apk/res-auto">

<item

android:id="@+id/firstFragment"

android:title="First fragment" />

<item

android:id="@+id/secondFragment"

android:title="Second fragmnet" />

<item

android:id="@+id/thirdFragment"

android:title="Third fragment" />

</menu>

You don't need to set by pro-grammatically because

NavigationUI.setupWithNavController(bnw, navController);

method will do the job.

Solution 2: Not recomended and Un-necessary

But if you want pro-grammatically then do like below:

  private lateinit var navController: NavController
  private val onNavigationItemSelectedListener = BottomNavigationView.OnNavigationItemSelectedListener { item ->
    when (item.itemId) {
        R.id.navigation_home -> {
            navController.navigate(R.id.firstFragment)
            return@OnNavigationItemSelectedListener true
        }
        R.id.navigation_dashboard -> {
            navController.navigate(R.id.secondFragment)
            return@OnNavigationItemSelectedListener true
        }
        R.id.navigation_notifications -> {
            Toast.makeText(this,R.string.title_notifications,Toast.LENGTH_SHORT).show()
            navController.navigate(R.id.thirdFragment)
            return@OnNavigationItemSelectedListener true
        }
    }
    false
}



override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    navController = findNavController(R.id.nav_controller_fragment)
    navView.setOnNavigationItemSelectedListener(onNavigationItemSelectedListener)
}
like image 183
Hussnain Haidar Avatar answered Sep 19 '22 14:09

Hussnain Haidar