Click here to Skip to main content
15,116,354 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

My Layout Activity have Tabs and Bottom Navigation, The Bottom Navigation works fine with Fragments but if I click on any tab it doesn't work.

Activity Layout is below:

XML
<?xml version="1.0" encoding="utf-8"?>
<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"
    xmlns:ads="http://schemas.android.com/tools"
    android:orientation="vertical">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="8"
        android:orientation="vertical">
        <com.google.android.material.appbar.AppBarLayout
            android:id="@+id/appbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <androidx.appcompat.widget.Toolbar
                android:layout_width="match_parent"
                android:layout_height="100dip"
                android:background="@drawable/top_header"
                app:layout_scrollFlags="scroll|enterAlways">
				
			</androidx.appcompat.widget.Toolbar>

            <com.google.android.material.tabs.TabLayout
                android:id="@+id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:tabMode="scrollable">

                <com.google.android.material.tabs.TabItem
                    android:id="@+id/tab1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/tab1" />

                <com.google.android.material.tabs.TabItem
                    android:id="@+id/tab2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/tab2" />

                <com.google.android.material.tabs.TabItem
                    android:id="@+id/tab3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/tab3" />

                <com.google.android.material.tabs.TabItem
                    android:id="@+id/tab4"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/tab4" />

                <com.google.android.material.tabs.TabItem
                    android:id="@+id/tab5"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/tab5" />
            </com.google.android.material.tabs.TabLayout>

        </com.google.android.material.appbar.AppBarLayout>

        <FrameLayout
            android:id="@+id/fragment_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@id/appbar"
            android:layout_above="@id/bottom_navigation">
        </FrameLayout>

        <com.google.android.material.bottomnavigation.BottomNavigationView
            android:id="@+id/bottom_navigation"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            app:menu="@menu/bottom_menu"
            />
    </RelativeLayout>

    <com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        ads:adSize="BANNER"
        android:layout_weight="1"
        android:layout_alignParentBottom="true" />

</LinearLayout>


Please help.

What I have tried:

Java
public class MainActivity extends AppCompatActivity{
    public static BottomNavigationView bottomNavigationView;
    TabLayout tabs;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // Bottom Navigation
        bottomNavigationView = findViewById(R.id.bottom_navigation);
        bottomNavigationView.setOnNavigationItemSelectedListener(navlistner);
        getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new Fragment1()).commit();

        // Tab Layout
        tabs = findViewById(R.id.tabs);
  tabs.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
      @Override
      public void onTabSelected(TabLayout.Tab tab) {
          Fragment selectedfragment = new Fragment1();
          switch (tab.getId()) {
              case R.id.tab1:
                  selectedfragment = new Fragment3();
                  break;
              case R.id.tab2:
                  selectedfragment = new Fragment4();
                  break;
              case R.id.tab3:
                  selectedfragment = new Fragment5();
                  break;
              case R.id.tab4:
                  selectedfragment = new Fragment5();
                  break;
              case R.id.tab5:
                  selectedfragment = new Fragment6();
                  break;
              default:
                  ///
          }
		  getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, selectedfragment).commit();
      }

      @Override
      public void onTabUnselected(TabLayout.Tab tab) {

      }

      @Override
      public void onTabReselected(TabLayout.Tab tab) {

      }
  });

}

private BottomNavigationView.OnNavigationItemSelectedListener navlistner = new BottomNavigationView.OnNavigationItemSelectedListener() {
    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        Fragment selectedfragment = new Fragment1();
        switch (item.getItemId()) {
            case R.id.Bottom_Bar_Home:
                selectedfragment = new Fragment1();
                break;
            case R.id.Bottom_Bar_Categories:
                selectedfragment = new Fragment2();
                break;
            default:
                ///
        }
        getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, selectedfragment).commit();
        return true;
    }
};




}
Posted
Updated 7-Jul-21 18:03pm
v3
Comments
David Crow 7-Jul-21 13:27pm
   
"The Bottom Navigation works fine...but if I click on any tab it doesn't work."

What exactly does that mean? The tabs are what make bottom navigation what it is. If bottom navigation is working, then the tabs are working. Otherwise, if the tabs are not working, then bottom navigation is not working. If you meant something else, please elaborate.
nyt1972 7-Jul-21 13:42pm
   
if I click on bottom navigation Fragment show up, but if I click on a tab the Fragment is not showing up.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900