How to solved issue TabLayouts when items on navigation drawer is clicked?

Viewed 147

I am very new to android development and hence, I am not able to solve my problems via help from other internet resources. Using Youtube videos, I am designing an app that uses both NavigationDrawer and TabLayout, and all the TabLayout items and navigation drawer items use fragments. My problem is that when I clicked on items in navigation drawers, the app still displays TabLayout's fragments instead of displaying the fragment corresponding to the clicked item from NavigationDrawer.
Please help me with this.
I have included code and even an image below
.

MAIN_ACTIVITY

In main activity I'm use bottom navigation bar with some fragment to individuals fragment to do something in the fragment like expandable listview I'm using in the home fragment tab .

            public class MainActivity extends AppCompatActivity {
            
                private DrawerLayout dl;
                private ActionBarDrawerToggle t;
                private NavigationView nv;
            
                BottomNavigationView bottomNavigationView;
            
                @Override
                protected void onCreate(Bundle savedInstanceState) {
                    super.onCreate(savedInstanceState);
                    setContentView(R.layout.activity_main);
            
                    ////////////////////////////////////////////////////
            
                    bottomNavigationView = findViewById(R.id.bottom_navigation);
                    BottomNavigationViewHelper.disableShiftMode(bottomNavigationView);
            
                    bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
                        @Override
                        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            
                            Fragment selectedFragment = null;
            
                            switch (item.getItemId()) {
                                case R.id.action_home:
                                    selectedFragment = HomeFragment.getInstance();
                                    break;
            
                                case R.id.action_hospital:
                                    selectedFragment = HospitalFragment.getInstance();
                                    break;
            
                                case R.id.action_shipping:
                                    selectedFragment = ShippingFragment.getInstance();
                                    break;
            
                                case R.id.action_notify:
                                    selectedFragment = NotifyFragment.getInstance();
                                    break;
                            }
            
                            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                            transaction.replace(R.id.main_frame, selectedFragment);
                            transaction.commit();
                            return false;
                        }
                    });
                    setDefaultFragment();
            
            
                    dl = (DrawerLayout) findViewById(R.id.activity_main);
                    t = new ActionBarDrawerToggle(this, dl, R.string.Open, R.string.Close);
                    t = new ActionBarDrawerToggle(this, dl, R.string.Open, R.string.Close);
                    dl.addDrawerListener(t);
                    t.setDrawerIndicatorEnabled(true);
                    t.syncState();
            
                    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            
                    nv = (NavigationView) findViewById(R.id.nv);
                    nv.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
                        @Override
                        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                            int id = item.getItemId();
                            switch (id) {
                                case R.id.account:
            //                        Intent intent = new Intent(MainActivity.this,SecondActivity.class);
            //                        startActivity(intent);
                                    Toast.makeText(MainActivity.this, "My Account", Toast.LENGTH_SHORT).show();
                                    break;
                                case R.id.settings:
                                    Toast.makeText(MainActivity.this, "Settings", Toast.LENGTH_SHORT).show();
                                    break;
                                case R.id.mycart:
                                    Toast.makeText(MainActivity.this, "My Cart", Toast.LENGTH_SHORT).show();
                                    break;
                                default:
                                    return true;
                            }
                            return true;
                        }
                    });
                }
            
                @Override
                public boolean onOptionsItemSelected(MenuItem item) {
            
                    if (t.onOptionsItemSelected(item))
                        return true;
            
                    return super.onOptionsItemSelected(item);
                }
            
            
                private void setDefaultFragment() {
                    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                    transaction.replace(R.id.main_frame, HomeFragment.getInstance());
                    transaction.commit();
                }
            }
        
        
          [1]: https://i.stack.imgur.com/p8DKL.png
        
        

Main xml file i'm using in this In main activity I'm use bottom navigation bar with some fragment to individuals fragment to do something in the fragment like expandable listview I'm using in the home fragment tab .

Main_XML

            <?xml version="1.0" encoding="utf-8"?>
        <RelativeLayout
            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">
        
        
            <androidx.drawerlayout.widget.DrawerLayout
                android:id="@+id/activity_main"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@android:color/holo_green_dark">
        
                <com.google.android.material.navigation.NavigationView
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    app:menu="@menu/navigation_menu"
                    android:layout_gravity="start"
                    app:headerLayout="@layout/nav_header"
                    android:id="@+id/nv">
        
                </com.google.android.material.navigation.NavigationView>
        
        
        
            </androidx.drawerlayout.widget.DrawerLayout>
        
            <FrameLayout
                android:id="@+id/main_frame"
                android:layout_above="@id/bottom_navigation"
                android:layout_width="match_parent"
                android:layout_height="match_parent"/>
        
            <com.google.android.material.bottomnavigation.BottomNavigationView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/bottom_navigation"
                android:layout_alignParentBottom="true"
                android:layout_alignParentStart="true"
                android:background="@android:color/white"
                app:menu="@menu/bottom_menu"/>
        
        </RelativeLayout>`
        
        

HomeFragment.

In this project I'm created 4 fragment and I'm describe 3 tabs >this the navigation drawer is swipe the tablayout is not visible In main activity I'm use bottom navigation bar with some fragment to individuals fragment to do something in the fragment like expandable listview I'm using in the home fragment tab .

        import android.os.Bundle;
        import android.view.LayoutInflater;
        import android.view.View;
        import android.view.ViewGroup;
        
        import androidx.annotation.Nullable;
        import androidx.fragment.app.Fragment;
        import androidx.viewpager.widget.ViewPager;
        
        import com.google.android.material.tabs.TabLayout;
        
        import kk.techbytecare.bottomnavigationview.Adapter.SectionPagerAdapter;
        import kk.techbytecare.bottomnavigationview.R;
        
        public class HomeFragment extends Fragment {
        
            View myFragment;
        
            ViewPager viewPager;
            TabLayout tabLayout;
        
        
            public HomeFragment() {
                // Required empty public constructor
            }
        
            public static HomeFragment getInstance()    {
                return new HomeFragment();
            }
        
        
            @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                     Bundle savedInstanceState) {
                // Inflate the layout for this fragment
                myFragment = inflater.inflate(R.layout.fragment_home, container, false);
        
                viewPager = myFragment.findViewById(R.id.viewPager);
                tabLayout = myFragment.findViewById(R.id.tabLayout);
        
                return myFragment;
            }
        
            >Call onActivity Create method
        
        
            @Override
            public void onActivityCreated(@Nullable Bundle savedInstanceState) {
                super.onActivityCreated(savedInstanceState);
        
                setUpViewPager(viewPager);
                tabLayout.setupWithViewPager(viewPager);
        
                tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
                    @Override
                    public void onTabSelected(TabLayout.Tab tab) {
        
                    }
        
                    @Override
                    public void onTabUnselected(TabLayout.Tab tab) {
        
                    }
        
                    @Override
                    public void onTabReselected(TabLayout.Tab tab) {
        
                    }
                });
            }
        
            private void setUpViewPager(ViewPager viewPager) {
                SectionPagerAdapter adapter = new SectionPagerAdapter(getChildFragmentManager());
        
                adapter.addFragment(new FixtureFragment(), "Fixtures");
                adapter.addFragment(new LiveFragment(), "Live");
                adapter.addFragment(new ResultFragment(),"Result");
        
                viewPager.setAdapter(adapter);
            }
        }
    
    
    **HomeFragment_XML**
    
>this is home fragment XML file so the problem is this the navigation drawer is swipe the tablayout is not visible In main activity I'm use bottom navigation bar with some fragment to individuals fragment to do something in the fragment like expandable listview I'm using in the home fragment tab .
        <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/rela"
        tools:context=".Fragment.HomeFragment">
    
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    
            <TextView
                android:layout_margin="20dp"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:text="Home Fragment"
                android:textAlignment="center"
                android:textSize="30sp" />
    
            <com.google.android.material.appbar.AppBarLayout
                android:id="@+id/appBarLayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@android:color/white">
    
                <com.google.android.material.tabs.TabLayout
                    android:id="@+id/tabLayout"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">
    
                </com.google.android.material.tabs.TabLayout>
    
            </com.google.android.material.appbar.AppBarLayout>
    
            <androidx.viewpager.widget.ViewPager
                android:id="@+id/viewPager"
                android:layout_width="match_parent"
                android:layout_height="match_parent"/>
    
        </LinearLayout>
    
    </RelativeLayout>

in this project I'm using androidx dependency In main activity I'm use bottom navigation bar with some fragment to individuals fragment to do something in the fragment like expandable listview I'm using in the home fragment tab .

1 Answers

Your issue is here:

        <androidx.drawerlayout.widget.DrawerLayout
          .....>

            <com.google.android.material.navigation.NavigationView
                android:layout_gravity="start"
                ../>
    
        </androidx.drawerlayout.widget.DrawerLayout>

        <FrameLayout
         .../>

Change to:

        <androidx.drawerlayout.widget.DrawerLayout
          .....>

            <!-- Main Content -->
            <FrameLayout
              .../>

            <com.google.android.material.navigation.NavigationView
                android:layout_gravity="start"
                ../>
    
        </androidx.drawerlayout.widget.DrawerLayout>
Related