Skip to content
Related Articles

Related Articles

Improve Article
Coordinator TabLayout in Android
  • Last Updated : 05 May, 2021

Many applications use TabLayout in which we have to show an image above the TabLayout. This type of UI design is mainly seen in Restaurant apps and many more. In this article, we will take a look at implementing the same type of view using Coordinator TabLayout in our Android App using Android Studio. 

What we are going to build in this article? 

We will be building a simple application in which we will be displaying the images and below that images, we will be displaying a TabLayout. Inside that TabLayout, we will be displaying the course-related details in that list and we will be displaying different types of courses in it. A sample video is given below to get an idea about what we are going to do in this article. Note that we are going to implement this project using the Java language. 

Step by Step Implementation for Adding Coordinator TabLayout in Android  

Step 1: Create a New Project

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language.

Step 2: Adding dependency for using Coordinator Layout in build.gradle

Navigate to the Gradle Scripts > build.gradle(Module:app) and add the below dependency in the dependencies section.   

implementation 'cn.hugeterry.coordinatortablayout:coordinatortablayout:1.2.2'

After adding the dependency now sync the project and we will be ready to work on it. 

Step 3: Updating the themes.xml file. 

Navigate to the app > res > values > themes.xml and change the theme to NoActionBar. Refer to the following code.


<resources xmlns:tools="">
    <!-- Base application theme with No Action Bar. -->
    <style name="Theme.GFG" parent="Theme.MaterialComponents.DayNight.NoActionBar">
        <!-- Primary brand color. -->
        <item name="colorPrimary">@color/purple_500</item>
        <item name="colorPrimaryVariant">@color/purple_700</item>
        <item name="colorOnPrimary">@color/white</item>
        <!-- Secondary brand color. -->
        <item name="colorSecondary">@color/teal_200</item>
        <item name="colorSecondaryVariant">@color/teal_700</item>
        <item name="colorOnSecondary">@color/black</item>
        <!-- Status bar color. -->
        <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
        <!-- Customize your theme here. -->

Step 4: Working with the activity_main.xml file

Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. 


<?xml version="1.0" encoding="utf-8"?>
    <!--creating a coordinator layout on below line-->
        <!--creating a view pager on below line-->
            app:layout_behavior="@string/appbar_scrolling_view_behavior" />

Step 5: Creating a Fragment for displaying our course details 

Navigate to the app > res > layout > Right-click on it > New > Blank Fragment and name it as HomeFragment and click on Create to create a new Fragment. After creating this fragment, now navigate to the app > res > layout > fragment_home and add the below code to it. 


<?xml version="1.0" encoding="utf-8"?>
    <!--creating a recycler view for displaying the course content-->
        android:layout_height="match_parent" />

Step 6: Creating a new layout file for the item of RecyclerView

Navigate to app > res > layout > Right-click on it > New > Layout Resource file and name it as item_main and add below code to it. 


<?xml version="1.0" encoding="utf-8"?>
    <!--text view for displaying course contents-->
        android:text="1" />

Step 7: Creating an adapter class for setting data to RecyclerView items 

Navigate to the app > java > your app’s package name > Right-click on it > New > Java class and name it as RecyclerAdapter and add the below code to it. 


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.MyViewHolder> {
    // creating variables for context and list.
    private Context mContext;
    private List<String> mDatas;
    // creating constructor.
    public RecyclerAdapter(Context context, List<String> datas) {
        mContext = context;
        mDatas = datas;
    public RecyclerAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        // inflating layout on below line.
        return new MyViewHolder(LayoutInflater.from(
                mContext).inflate(R.layout.item_main, parent, false));
    public void onBindViewHolder(@NonNull RecyclerAdapter.MyViewHolder holder, int position) {
        // setting data to text view on below line.;
    public int getItemCount() {
        // returning the size of array list.
        return mDatas.size();
    public class MyViewHolder extends RecyclerView.ViewHolder {
        // creating variables for text view.
        TextView tv;
        public MyViewHolder(@NonNull View itemView) {
            // initializing the text view.
            tv = (TextView) itemView.findViewById(;

Step 8: Working with the file 

Navigate to the app > java > your app’s package name > file and add the below code to it. 


import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import java.util.List;
public class HomeFragment extends Fragment {
    // creating a variables on below line.
    private static final String ARG_TITLE = "title";
    private RecyclerView mRecyclerView;
    private List<String> courseList;
    private String mTitle;
    public HomeFragment() {
        // Required empty public constructor
    // creating a get instance method to get the data which we passed.
    public static HomeFragment getInstance(String title) {
        HomeFragment fra = new HomeFragment();
        Bundle bundle = new Bundle();
        bundle.putString(ARG_TITLE, title);
        return fra;
    public void onCreate(Bundle savedInstanceState) {
        // on below line we are getting the data from bundles.
        Bundle bundle = getArguments();
        mTitle = bundle.getString(ARG_TITLE);
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_home, container, false);
        // below method is use to load data in our array list.
        // initialing our variables for views.
        mRecyclerView = (RecyclerView) view.findViewById(;
        mRecyclerView.setLayoutManager(new LinearLayoutManager(mRecyclerView.getContext()));
        mRecyclerView.setAdapter(new RecyclerAdapter(mRecyclerView.getContext(), courseList));
        return view;
    private void initData(String heading) {
        // running a switch case to add data to our array list.
        switch (heading) {
            case "DSA":
                courseList = new ArrayList<>();
                courseList.add("Binary Search Trees");
            case "C++":
                courseList = new ArrayList<>();
                courseList.add("Data Types");
                courseList.add("Type Conversion");
                courseList.add("C++ Syntax");
            case "Java":
                courseList = new ArrayList<>();
                courseList.add("Intro to Java");
                courseList.add("Object Oriented Concepts");
                courseList.add("Conditional and Control Flow");
                courseList.add("Array List");
                courseList = new ArrayList<>();
                courseList.add("Basic Concepts of Python");
                courseList.add("Program Flow Control in Python");
                courseList.add("Lists and Tuples");
                courseList.add("Python Dictonaries and Sets");

Step 9: Creating an Adapter class for handling TabLayout 

Navigate to the app > java > your app’s package name > Right-click on it > New > Java class and name it as ViewPagerAdapter and add the below code to it.


import java.util.ArrayList;
public class ViewPagerAdapter extends FragmentPagerAdapter {
    // creating a string array for tab title.
    private final String[] mTitles;
    // creating an array list on below line for fragments.
    private ArrayList<Fragment> mFragments = new ArrayList<>();
    // constructor for view pager
    public ViewPagerAdapter(FragmentManager fm, ArrayList<Fragment> mFragments, String[] mTitles) {
        this.mFragments = mFragments;
        this.mTitles = mTitles;
    // returning the size of our list.
    public int getCount() {
        return mFragments.size();
    // below method is to set the title for tab layout item.
    public CharSequence getPageTitle(int position) {
        return mTitles[position];
    // below method is use to get the item.
    public Fragment getItem(int position) {
        return mFragments.get(position);

Step 10: Working with the file

Go to the file and refer to the following code. Below is the code for the file. Comments are added inside the code to understand the code in more detail.


import android.os.Bundle;
import androidx.viewpager.widget.ViewPager;
import java.util.ArrayList;
import cn.hugeterry.coordinatortablayout.CoordinatorTabLayout;
public class MainActivity extends AppCompatActivity {
    // creating variables for coordinator layout
    private CoordinatorTabLayout mCoordinatorTabLayout;
    // creating an integer array for images and colors.
    private int[] mImageArray, mColorArray;
    // creating array list for our fragments.
    private ArrayList<Fragment> mFragments;
    // creating a string array for our tab title in tab layout.
    private final String[] mTitles = {"DSA", "Java", "C++", "Python"};
    // creating a variable for view pager.
    private ViewPager mViewPager;
    protected void onCreate(Bundle savedInstanceState) {
        // calling method to initialize 
        // our fragments and view pager.
        // adding images to our int array on below line.
        mImageArray = new int[]{
        // adding color to our color array on below line.
        mColorArray = new int[]{
        // initializing our coordinator layout
        mCoordinatorTabLayout = findViewById(;
        // on below line we are setting title to our tool bar
        // and image array, color array and view pager
        // to our co coordinator layout
                .setTitle("Geeks For Geeks")
                .setImageArray(mImageArray, mColorArray)
    private void initFragments() {
        // initializing our array list.
        mFragments = new ArrayList<>();
        // adding fragment to our array list on below line.
        for (String title : mTitles) {
    private void initViewPager() {
        // initializing our view pager and setting adapter to it.
        mViewPager = findViewById(;
        mViewPager.setAdapter(new ViewPagerAdapter(getSupportFragmentManager(), mFragments, mTitles));

Note: Images use inside the project are present in the drawable folder. 

Now run your app and see the output of the app. 


Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :