Tuesday, March 16, 2021

Android TabLayout

 

Android TabLayout

TabLayout is used to implement horizontal tabs. TabLayout is released by Android after the deprecation ofActionBar.TabListener (API level 21).

TabLayout is introduced in design support library to implement tabs.

Tabs are created using newTab() method of TabLayout class. The title and icon of Tabs are set through setText(int) andsetIcon(int) methods of TabListener interface respectively. Tabs of layout are attached over TabLayout using the method addTab(Tab) method.

  1. TabLayout tabLayout = (TabLayout)findViewById(R.id.tabLayout);  
  2. tabLayout.addTab(tabLayout.newTab().setText("Tab 1"));  
  3. tabLayout.addTab(tabLayout.newTab().setText("Tab 2"));  
  4. tabLayout.addTab(tabLayout.newTab().setText("Tab 3"));  

We can also add tab item to TabLayout using TabItem of android design widget.

  1. <android.support.design.widget.TabItem  
  2.              android:text="@string/tab_text"/>  

Example of TabLayout using ViewPager

Let's create an example of TabLayout using ViewPager and Fragment.

File: activity.xml

Create an activity.xml file with TabLayout and ViewPager view components.

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:app="http://schemas.android.com/apk/res-auto"  
  4.     xmlns:tools="http://schemas.android.com/tools"  
  5.     android:layout_width="match_parent"  
  6.     android:layout_height="match_parent"  
  7.     tools:context="tablayout.example.com.tablayout.MainActivity">  
  8.   
  9.   
  10.     <android.support.design.widget.TabLayout  
  11.         android:id="@+id/tabLayout"  
  12.         android:layout_width="match_parent"  
  13.         android:layout_height="wrap_content"  
  14.         android:background="#1db995">  
  15.     </android.support.design.widget.TabLayout>  
  16.   
  17.     <android.support.v4.view.ViewPager  
  18.         android:id="@+id/viewPager"  
  19.         android:layout_width="355dp"  
  20.         android:layout_height="455dp"  
  21.         app:layout_constraintTop_toBottomOf="@+id/tabLayout"  
  22.         tools:layout_editor_absoluteX="8dp" />  
  23.   
  24.   
  25. </android.support.constraint.ConstraintLayout>  

File: build.gradle

Now gave the dependency library of TabLayout in build.gradle file.

  1. implementation 'com.android.support:design:26.1.0'  

File: MainActivity.java

In this file, we implement two additional listener addOnPageChangeListener(listener) of ViewPager which makes slides the different fragments of tabs and addOnTabSelectedListener(listener) of TabLayout which select the current tab on tab selection.

  1. package tablayout.example.com.tablayout;  
  2.   
  3. import android.support.design.widget.TabLayout;  
  4. import android.support.v4.view.ViewPager;  
  5. import android.support.v7.app.AppCompatActivity;  
  6. import android.os.Bundle;  
  7.   
  8. public class MainActivity extends AppCompatActivity {  
  9.     TabLayout tabLayout;  
  10.     ViewPager viewPager;  
  11.     @Override  
  12.     protected void onCreate(Bundle savedInstanceState) {  
  13.         super.onCreate(savedInstanceState);  
  14.         setContentView(R.layout.activity_main);  
  15.   
  16.         tabLayout=(TabLayout)findViewById(R.id.tabLayout);  
  17.         viewPager=(ViewPager)findViewById(R.id.viewPager);  
  18.   
  19.         tabLayout.addTab(tabLayout.newTab().setText("Home"));  
  20.         tabLayout.addTab(tabLayout.newTab().setText("Sport"));  
  21.         tabLayout.addTab(tabLayout.newTab().setText("Movie"));  
  22.         tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);  
  23.   
  24.         final MyAdapter adapter = new MyAdapter(this,getSupportFragmentManager(), tabLayout.getTabCount());  
  25.         viewPager.setAdapter(adapter);  
  26.   
  27.         viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));  
  28.   
  29.          tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {  
  30.             @Override  
  31.             public void onTabSelected(TabLayout.Tab tab) {  
  32.                 viewPager.setCurrentItem(tab.getPosition());  
  33.             }  
  34.   
  35.             @Override  
  36.             public void onTabUnselected(TabLayout.Tab tab) {  
  37.   
  38.             }  
  39.   
  40.             @Override  
  41.             public void onTabReselected(TabLayout.Tab tab) {  
  42.   
  43.             }  
  44.         });  
  45.   
  46.     }  
  47. }  

File: MyAdapter.java

  1. package tablayout.example.com.tablayout;  
  2.   
  3. import android.content.Context;  
  4. import android.support.v4.app.Fragment;  
  5. import android.support.v4.app.FragmentPagerAdapter;  
  6. import android.support.v4.app.FragmentManager;  
  7.   
  8. public class MyAdapter extends FragmentPagerAdapter {  
  9.   
  10.     private Context myContext;  
  11.     int totalTabs;  
  12.   
  13.     public MyAdapter(Context context, FragmentManager fm, int totalTabs) {  
  14.         super(fm);  
  15.         myContext = context;  
  16.         this.totalTabs = totalTabs;  
  17.     }  
  18.   
  19.     // this is for fragment tabs  
  20.     @Override  
  21.     public Fragment getItem(int position) {  
  22.         switch (position) {  
  23.             case 0:  
  24.                 HomeFragment homeFragment = new HomeFragment();  
  25.                 return homeFragment;  
  26.             case 1:  
  27.                 SportFragment sportFragment = new SportFragment();  
  28.                 return sportFragment;  
  29.             case 2:  
  30.                 MovieFragment movieFragment = new MovieFragment();  
  31.                 return movieFragment;  
  32.             default:  
  33.                 return null;  
  34.         }  
  35.     }  
  36. // this counts total number of tabs  
  37.     @Override  
  38.     public int getCount() {  
  39.         return totalTabs;  
  40.     }  
  41. }  

Now create different fragment files for all different tabs.

File: HomeFragment.java

  1. package tablayout.example.com.tablayout;  
  2.   
  3. import android.os.Bundle;  
  4. import android.support.v4.app.Fragment;  
  5. import android.view.LayoutInflater;  
  6. import android.view.View;  
  7. import android.view.ViewGroup;  
  8.   
  9. public class HomeFragment extends Fragment {  
  10.   
  11.   
  12.     public HomeFragment() {  
  13.         // Required empty public constructor  
  14.     }  
  15.   
  16.     @Override  
  17.     public View onCreateView(LayoutInflater inflater, ViewGroup container,  
  18.                              Bundle savedInstanceState) {  
  19.         // Inflate the layout for this fragment  
  20.         return inflater.inflate(R.layout.fragment_home, container, false);  
  21.     }  
  22.   
  23. }  

File: fragment_home.xml

  1. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     tools:context="tablayout.example.com.tablayout.HomeFragment">  
  6.   
  7.     <!-- TODO: Update blank fragment layout -->  
  8.   
  9.     <TextView  
  10.         android:layout_width="match_parent"  
  11.         android:layout_height="match_parent"  
  12.         android:gravity="center"  
  13.         android:text="@string/home_fragment" />  
  14.   
  15. </FrameLayout>  

File: SportFragment.java

  1. package tablayout.example.com.tablayout;  
  2.   
  3. import android.os.Bundle;  
  4. import android.support.v4.app.Fragment;  
  5. import android.view.LayoutInflater;  
  6. import android.view.View;  
  7. import android.view.ViewGroup;  
  8.   
  9. public class SportFragment extends Fragment {  
  10.   
  11.   
  12.     public SportFragment() {  
  13.         // Required empty public constructor  
  14.     }  
  15.   
  16.     @Override  
  17.     public View onCreateView(LayoutInflater inflater, ViewGroup container,  
  18.                              Bundle savedInstanceState) {  
  19.         // Inflate the layout for this fragment  
  20.         return inflater.inflate(R.layout.fragment_sport, container, false);  
  21.     }  
  22.   
  23. }  

File: fragment_sport.xml

  1. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     tools:context="tablayout.example.com.tablayout.SportFragment">  
  6.   
  7.     <!-- TODO: Update blank fragment layout -->  
  8.     <TextView  
  9.         android:layout_width="match_parent"  
  10.         android:layout_height="match_parent"  
  11.         android:gravity="center"  
  12.         android:text="@string/sport_fragment" />  
  13.   
  14. </FrameLayout>  

File: MovieFragment.java

  1. package tablayout.example.com.tablayout;  
  2.   
  3. import android.os.Bundle;  
  4. import android.support.v4.app.Fragment;  
  5. import android.view.LayoutInflater;  
  6. import android.view.View;  
  7. import android.view.ViewGroup;  
  8.   
  9. public class MovieFragment extends Fragment {  
  10.   
  11.   
  12.     public MovieFragment() {  
  13.         // Required empty public constructor  
  14.     }  
  15.   
  16.     @Override  
  17.     public View onCreateView(LayoutInflater inflater, ViewGroup container,  
  18.                              Bundle savedInstanceState) {  
  19.         // Inflate the layout for this fragment  
  20.         return inflater.inflate(R.layout.fragment_movie, container, false);  
  21.     }  
  22.   
  23. }  

File: fragment_movie.xml

  1. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     tools:context="tablayout.example.com.tablayout.MovieFragment">  
  6.   
  7.     <!-- TODO: Update blank fragment layout -->  
  8.     <TextView  
  9.         android:layout_width="match_parent"  
  10.         android:layout_height="match_parent"  
  11.         android:gravity="center"  
  12.         android:text="@string/movie_fragment" />  
  13.   
  14. </FrameLayout>  

File: strings.xml

  1. <resources>  
  2.     <string name="app_name">TabLayout</string>  
  3.   
  4.     <!-- TODO: Remove or change this placeholder text -->  
  5.     <string name="home_fragment">Home Fragment</string>  
  6.     <string name="sport_fragment">Sport Fragment</string>  
  7.     <string name="movie_fragment">Movie Fragment</string>  
  8.   
  9. </resources>  

Output

android tablayout 1
android tablayout 2 

No comments:

Post a Comment

Inapp update

  Inapp update https://desk.zoho.com/portal/vegabirdtech/en/kb/articles/how-to-use-burp-suite-with-android-mobile