Create Lollipop Tabs in Android Example.

To create Lollipop style of tab for all the version of Android from 2.1 onward just follow steps given in this example, here we shall see setting up of tabs with simple Listview.

Tabs in Lollipop Android
Portrait mode tabs in Lollipop Android

In our previous example of Appbar in Android, there we had used recycleview but here we are using the simple listview.

Update Gradle with below dependency’s

compile 'com.android.support:design:22.2.0'
compile 'com.android.support:appcompat-v7:22.2.0'
compile 'com.android.support:recyclerview-v7:22.2.0'

Now create activity comprise of toolbar, tablayout and viewpager. Here viewpager helps in swiping the tabs. Here is the acitivity_main.xml

<android.support.design.widget.CoordinatorLayout
    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">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
        
    <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/tabspager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
    </android.support.design.widget.CoordinatorLayout>

Now create tabs adapter class, this will manage all your tabs

public class SimpleTapbsadapter extends FragmentPagerAdapter {

    private final List<Fragment> FragmentList = new ArrayList();
    private final List<String> FragmentTitles = new ArrayList();

    public SimpleTapbsadapter(FragmentManager fm) {
        super(fm);
    }

    public void addFragment(Fragment fragment, String title) {
        FragmentList.add(fragment);
        FragmentTitles.add(title);
    }

    @Override
    public Fragment getItem(int position) {
        return FragmentList.get(position);
    }

    @Override
    public int getCount() {
        return FragmentList.size();
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return FragmentTitles.get(position);
    }

}

friendsview.xml input view to fragment.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    android:orientation="vertical">

    <ListView android:layout_width="fill_parent"
        android:layout_height="wrap_content"

        android:id="@+id/id_test_listview"/>

</RelativeLayout>

Now create ObjectsFragment.java adapter for each tabs fragment. here we are using predefined simple list adapter by android. you can create your own custom adapter also.

public class ObjectsFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.friendsview, container, false);


        // Defined Array values to show in ListView
        String[] Objects = new String[] { "Apple",
                "Orange","Grapes","Dog","Cat","Rat","Ants","Books","Lion","Tiger",           
                "Monkey","Water","Air","Sun","Rain","Orange","Grapes","Dog","Cat","Rat",
                "Ants","Books","Lion","Tiger","Monkey","Water","Air","Sun","Rain","Orange",
                "Grapes","Dog","Cat","Rat","Ants","Books","Lion","Tiger","Monkey","Water","Air",
                "Sun","Rain"
        };

        ListView simpleList = (ListView)view.findViewById(R.id.id_test_listview);

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
                android.R.layout.simple_list_item_1, android.R.id.text1, Objects);

        // Assign adapter to ListView
        simpleList.setAdapter(adapter);

        return view;
    }
}

Now create instance of tabsadapter & viewpager and link them in mainactivity.java.

public class MainActivity extends AppCompatActivity {

    TabLayout mTabs;
    private ViewPager tabsviewPager;
    private SimpleTapbsadapter mTabsAdapter;

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

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        tabsviewPager = (ViewPager) findViewById(R.id.tabspager);

        mTabsAdapter = new SimpleTapbsadapter(getSupportFragmentManager());

         //creating the tabs and adding them to adapter class
        mTabsAdapter.addFragment(new ObjectsFragment(), "Fruits");
        mTabsAdapter.addFragment(new ObjectsFragment(), "Flower");
        mTabsAdapter.addFragment(new ObjectsFragment(), "leaves");

        //setup viewpager to give swipe effect
        tabsviewPager.setAdapter(mTabsAdapter);

        mTabs = (TabLayout) findViewById(R.id.tabs);
        mTabs.setupWithViewPager(tabsviewPager);

    }

}

Update the style.xml to add toolbar.

    <style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
        <!-- Customize your theme here. -->
    </style>

In Landscape mode the tabs looks like below.

Lollipop mode tabs in portrait mode
Landscape mode tabs