Cover Image for TabLayout with FrameLayout in Android
153 views

TabLayout with FrameLayout in Android

To implement a TabLayout with a FrameLayout in Android, you can use the TabLayout to switch between different fragments displayed within the FrameLayout. This is a common pattern for creating tabbed interfaces in Android apps.

Here’s how you can create a TabLayout with a FrameLayout:

  1. Add Dependencies: First, ensure that you have the necessary dependencies in your build.gradle file:
Java
 implementation 'com.google.android.material:material:1.4.0' // Use the latest version available
 implementation 'androidx.viewpager2:viewpager2:1.1.0' // For ViewPager2 (optional)

The ViewPager2 library is optional but recommended for handling fragment transitions when using a TabLayout.

  1. Create Fragments: Create the fragments that you want to display within the FrameLayout. These fragments will represent the content of each tab.
Java
 public class Fragment1 extends Fragment {
     // Fragment1 code here
 }

 public class Fragment2 extends Fragment {
     // Fragment2 code here
 }

 // Create more fragments as needed
  1. Create XML Layout: Create the XML layout file for your main activity, which includes the TabLayout and a FrameLayout to host the fragments. Here’s an example:
XML
 <?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
     android:layout_height="match_parent">

     <com.google.android.material.tabs.TabLayout
         android:id="@+id/tab_layout"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:layout_alignParentTop="true"
         app:tabGravity="fill"
         app:tabMode="fixed" />

     <FrameLayout
         android:id="@+id/frame_layout"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:layout_below="@id/tab_layout" />

 </RelativeLayout>

In this layout, the TabLayout is positioned at the top, and the FrameLayout is placed below it to host the fragments.

  1. Initialize TabLayout: In your activity, you need to set up the TabLayout, configure it with tab names and fragments, and connect it to the FrameLayout. Here’s a simplified example:
Java
 import com.google.android.material.tabs.TabLayout;
 import androidx.viewpager2.widget.ViewPager2;

 public class MainActivity extends AppCompatActivity {
   private ViewPager2 viewPager;
   private TabLayout tabLayout;
   private MyPagerAdapter pagerAdapter;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);

       viewPager = findViewById(R.id.view_pager);
       tabLayout = findViewById(R.id.tab_layout);

       pagerAdapter = new MyPagerAdapter(this);
       viewPager.setAdapter(pagerAdapter);

       new TabLayoutMediator(tabLayout, viewPager,
           (tab, position) -> tab.setText("Tab " + (position + 1))
       ).attach();
   }
 }

In this code:

  • MyPagerAdapter is a custom adapter that manages the fragments for the ViewPager2.
  • The TabLayoutMediator is used to link the TabLayout with the ViewPager2 and set tab names.
  1. Create PagerAdapter: Create a custom PagerAdapter to manage the fragments for the ViewPager2. Here’s a simplified example:
Java
 public class MyPagerAdapter extends FragmentStateAdapter {
   public MyPagerAdapter(FragmentActivity fragmentActivity) {
       super(fragmentActivity);
   }

   @Override
   public Fragment createFragment(int position) {
       switch (position) {
           case 0:
               return new Fragment1();
           case 1:
               return new Fragment2();
           // Add more cases for additional fragments
       }
       return null;
   }

   @Override
   public int getItemCount() {
       return 2; // Number of tabs or fragments
   }
 }

Customize the MyPagerAdapter to return the appropriate fragment for each tab. You can add more cases for additional tabs.

  1. Run the App: Run your app, and you should see a TabLayout with tabs that switch between different fragments displayed within the FrameLayout.

This is a basic implementation of a TabLayout with a FrameLayout to switch between fragments. You can further customize the appearance and behavior of your tabs and fragments to suit your app’s requirements.

YOU MAY ALSO LIKE...

The Tech Thunder

The Tech Thunder

The Tech Thunder


COMMENTS