<?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>
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; } }; }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)