2

I have BottomNavigationView in 'activity-home.xml' as below.

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <com.google.android.material.bottomappbar.BottomAppBar
            android:id="@+id/bottom_appbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            app:fabCradleRoundedCornerRadius="50dp"
            app:fabCradleMargin="10dp">


    <com.google.android.material.bottomnavigation.BottomNavigationView
                    android:id="@+id/nav_view"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_marginEnd="16dp"
                    android:background="@android:color/transparent"
                    app:menu="@menu/bottom_nav_menu"
                    app:itemIconTint="@color/bottom_nav_color"
                    app:itemTextColor="@color/bottom_nav_color"/>
        
            </com.google.android.material.bottomappbar.BottomAppBar>

<com.google.android.material.floatingactionbutton.FloatingActionButton
            android:id="@+id/fab_new"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:srcCompat="@drawable/fab_image"
            android:scaleType="center"
            app:maxImageSize="56dp"
            app:tint="@null"
            app:layout_anchor="@id/bottom_appbar"
            android:contentDescription="Show Categories" />
        
        <fragment
            android:id="@+id/nav_host_fragment"
          android:name="androidx.navigation.fragment.NavHostFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginBottom="50dp"
            app:defaultNavHost="true"
            app:layout_anchor="@id/bottom_appbar"
            app:navGraph="@navigation/mobile_navigation" />
        
    </androidx.coordinatorlayout.widget.CoordinatorLayout>

Following is the 'mobile_navigation.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"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/mobile_navigation"
    app:startDestination="@+id/nav_home">

    <fragment
        android:id="@+id/nav_home"
        android:name="com.abc.xyz.ui.fragments.HomeFragment"
        android:label="@string/mobile_navigation_label"
        tools:layout="@layout/fragment_home" />

    <fragment
        android:id="@+id/nav_orders"
        android:name="com.abc.xyz.ui.fragments.OrdersFragment"
        android:label="@string/mobile_navigation_label"
        tools:layout="@layout/fragment_orders"/>

    <fragment
        android:id="@+id/nav_cart"
        android:name="com.abc.xyz.ui.fragments.CartFragment"
        android:label="@string/mobile_navigation_label"
        tools:layout="@layout/fragment_cart"/>

</navigation>

When I click each menu item from the bottom navigation it shows respective fragments and everything works perfectly as expected. Now I wanted to show/open 'CartFragment.kt', ('R.id.nav_cart') when I click a button that is in the recyclerView of the 'fragment_home'. I tried the following way but it didn't work as I expected.

Following is what I have in the onBindViewHolder of the adapter class of the 'fragment_home'

        holder.binding.btnGoToCart.setOnClickListener {
    val activity=context as HomeActivity
activity.supportFragmentManager.beginTransaction().replace(R.id.container,CartFragment()).addToBackStack(null).commit()
    }

Even though I tried as above I think, that's not how I am supposed to do it. All I wanted is to navigate to the bottom menu 'R.id.nav_cart' which should show 'CartFragment'

EDIT:

I also tried the following code in the adapter class of 'fragment_home'. When I hit the button, The bottom menu is highlighted as if it's selected but I don't know what's really happening since the fragment of which is not being shown properly. What I meant is, there is a recyclerView and a few textViews and EditTexts in the fragment but the recyclerView is not being shown, only other views are shown. Also, when I hit other bottom menu items, nothing happens at all except those menu items are highlighted.

            holder.binding.btnGoToCart.setOnClickListener {

                val activity=context as MainActivity

                var fragment:Fragment=CartFragment()
                val bottomNav: BottomNavigationView = context.findViewById(R.id.nav_view)
                bottomNav.setOnNavigationItemSelectedListener { item ->
                    when (item.itemId) {
                        R.id.nav_cart -> {
                            fragment = CartFragment()
                        }
                    }
                    context.supportFragmentManager
                        .beginTransaction()
                        .replace(R.id.container, fragment)
                        .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
                        .commit()
                    true
                }
                bottomNav.selectedItemId = R.id.nav_cart
            }
Codist
  • 737
  • 8
  • 23
  • Can you make ask make it a bit detailed please ? is CardFragment is shown in one of the bottom navigation options or you want to navigate a totaly new CardFragment that bottom navigation does not shown ? – Oğuzhan Aslan Nov 21 '21 at 12:23
  • My question is updated now, please check. – Codist Nov 21 '21 at 12:47
  • Can someone help me with this? – Codist Nov 21 '21 at 19:15
  • Your question is a little unclear (to me). Show some screenshot or videos to help illustrate the issue. – dominicoder Nov 21 '21 at 20:16
  • @dominicoder My question is updated, I hope it's clear now. Thanks. – Codist Nov 22 '21 at 11:55
  • You added some layouts but the issue is still not clear. Again, pictures / videos would be worth 1000 words. However, I do notice you have `` element but then you are manually setting fragment transactions when you should be using a nav controller. Read and follow the documentation carefully: https://developer.android.com/guide/navigation – dominicoder Nov 22 '21 at 17:53
  • Thanks, I will go through the link. Did you mean this line of code (`activity.supportFragmentManager.beginTransaction().replace(R.id.container,CartFragment()).addToBackStack(null).commit()`) by I am manually setting the fragment transaction, then, I was just trying how to accomplish my requirement. Now, about the issue... What I am trying to do is go to the fragment 'CartFragment', that is, select the bottom navigation menu ('R.id.nav_cart') when I click a button in the `recyclerView` of 'fragment_home'. I hope it's now clear. – Codist Nov 22 '21 at 18:08
  • @dominicoder maybe I should change my question to "How to select a bottom navigation menu programmatically from the `recyclerView` adapter?" – Codist Nov 22 '21 at 18:26
  • 1
    https://developer.android.com/guide/navigation/navigation-global-action – dominicoder Nov 22 '21 at 23:41
  • @dominicoder That was awesome. If you add the content in the link as the answer then I can accept it as the Answer. – Codist Nov 23 '21 at 14:49

1 Answers1

1

I should change my question to "How to select a bottom navigation menu programmatically from the recyclerView adapter?"

Since you're using the navigation library, you would programmatically navigate to the desired destination. See the example in the documentation.

In your case, something like:

holder.binding.btnGoToCart.setOnClickListener { view ->
    view.findNavController().navigate(R.id.nav_cart)
}
dominicoder
  • 9,338
  • 1
  • 26
  • 32