Android TabLayout + Fragment Tutorial
Android TabLayout allow you navigate to another screen without open a new activity. It is always visible to the user unlike NavigationDrawer are always hidden. The benefit of tab in android is providing the ease of navigation to another section by just clicking the tab. You also can implement add viewPager to the tablayout so it just need swipe right or left for navigate to different tab. In this tutorial, I will teach u how to create the tablayout in android. The viewPager and Fragment are include to the tablayout. So, you just need to follow my steps so you can get it done.
Android TabLayout Demo
Creating a New Project
1. Open Android Studio IDE in your computer.
2. Create a new project and Edit the Application name to “TabExample”.
(Optional) You can edit the company domain or select the suitable location for current project tutorial. Then click next button to proceed.
3. Select Minimum SDK (API 15:Android 4.0.3 (IceCreamSandwich). I choose the API 15 because many android devices currently are support more than API 15. Click Next button.
4. Choose “Tabbed Acitivity” and Click Next button
5. Lastly, press finish button.
Edit activity_main.xml layout
After create the Tabbed Activity, go to activity_main.xml and insert the following code.
<?xml version="1.0" encoding="utf-8"?> <android.support.design.widget.CoordinatorLayout 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:id="@+id/main_content" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" tools:context="com.example.questdot.tabexample.MainActivity"> <android.support.design.widget.AppBarLayout android:id="@+id/appbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingTop="@dimen/appbar_padding_top" android:theme="@style/AppTheme.AppBarOverlay"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" app:layout_scrollFlags="scroll|enterAlways" app:popupTheme="@style/AppTheme.PopupOverlay"> </android.support.v7.widget.Toolbar> <android.support.design.widget.TabLayout android:id="@+id/tabs" android:layout_width="match_parent" android:layout_height="wrap_content" /> </android.support.design.widget.AppBarLayout> <android.support.v4.view.ViewPager android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior" /> </android.support.design.widget.CoordinatorLayout>
Edit fragment_main.xml layout
After that, go to your fragment_main and add the textview in your layout.
<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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".PlaceholderFragment"> <TextView android:id="@+id/section_label" android:textSize="25dp" android:text="This is Fragment 1" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_centerVertical="true" android:layout_centerHorizontal="true" /> </RelativeLayout>
Move class to dedicated file
Go to your MainActivity.java in your package folder. Inside your code may have PlaceholderFragment and SectionsPagerAdapter class. Move those class to dedicated file which are following.
PlaceHolderFragment.java
package com.example.questdot.tabexample; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; public class PlaceholderFragment extends Fragment { private static final String ARG_SECTION_NUMBER = "section_number"; public PlaceholderFragment() { } /** * Returns a new instance of this fragment for the given section * number. */ public static PlaceholderFragment newInstance(int sectionNumber) { PlaceholderFragment fragment = new PlaceholderFragment(); Bundle args = new Bundle(); args.putInt(ARG_SECTION_NUMBER, sectionNumber); fragment.setArguments(args); return fragment; } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_main, container, false); return rootView; } }
SectionsPagerAdapter.java
package com.example.questdot.tabexample; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; public class SectionsPagerAdapter extends FragmentPagerAdapter { public SectionsPagerAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int position) { switch(position){ case 0: return new PlaceholderFragment(); case 1: return new AnotherFragment(); } return null; } @Override public int getCount() { return 2; } @Override public CharSequence getPageTitle(int position) { switch (position) { case 0: return "SECTION 1"; case 1: return "SECTION 2"; } return null; } }
Create a new Fragment
Add a second fragment in your activity. Right click package name > New > Fragment > Fragment. I name my fragment as AnotherFragment.
Edit fragment_another.xml
In your fragment_another.xml add the textview to specific which fragment is currently open.
<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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".AnotherFragment"> <!-- TODO: Update blank fragment layout --> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:textSize="25dp" android:text="This is Fragment 2" android:layout_centerVertical="true" android:layout_centerHorizontal="true" /> </RelativeLayout>
Edit AnotherFragment.java class
Besides that, go to your AnotherFragment.java and insert the following code.
package com.example.questdot.tabexample; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class AnotherFragment extends Fragment { // TODO: Rename parameter arguments, choose names that match // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER private static final String ARG_SECTION_NUMBER = "section_number"; public AnotherFragment() { } public static AnotherFragment newInstance(int sectionNumber) { AnotherFragment fragment = new AnotherFragment(); Bundle args = new Bundle(); args.putInt(ARG_SECTION_NUMBER, sectionNumber); fragment.setArguments(args); return fragment; } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_another, container, false); return rootView; } }
Edit MainActivity.java class
Last, go to your MainActivity.java and paste the following code. This will add 2 fragment in the activity.
package com.example.questdot.tabexample; import android.support.design.widget.TabLayout; import android.support.design.widget.FloatingActionButton; import android.support.design.widget.Snackbar; import android.support.v4.app.FragmentManager; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.support.v4.app.FragmentPagerAdapter; import android.support.v4.view.ViewPager; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; public class MainActivity extends AppCompatActivity { private SectionsPagerAdapter mSectionsPagerAdapter; private ViewPager mViewPager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); // Create the adapter that will return a fragment for each of the three // primary sections of the activity. FragmentManager fragmentManager=getSupportFragmentManager(); fragmentManager.beginTransaction() .add(R.id.container, new PlaceholderFragment()) .commit(); mSectionsPagerAdapter = new SectionsPagerAdapter(fragmentManager); // Set up the ViewPager with the sections adapter. mViewPager = (ViewPager) findViewById(R.id.container); mViewPager.setAdapter(mSectionsPagerAdapter); TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs); tabLayout.setupWithViewPager(mViewPager); } }
Run your Project
Now, you can start your project in your emulator. The tablayout is just easy to use and implement by yourself.