use tab with new toolbar appcompat v7 21

use tab with new toolbar appcompat v7 21  using -'android,tabs,android-support-library,appcompat,android-actionbar-compat'

I was using Support ActionBar with tabs and using custom ActionBar theme (created with, showing the tabs only when the user expand the search view.

public boolean onMenuItemActionExpand(MenuItem item) {
        return true;

I migrated from ActionBar to ToolBar. My app really needs to support api 9.

Is there a way to use this?:

Toolbar toolbar = (Toolbar) findViewById(;

If is possible, how to use my custom theme or style the toolbar?

Documentation say that is deprecated and suggests to use other type of navigation.
But I don't know other component in android to have the same functionality.

Some help?

asked Oct 11, 2015 by pradip.bhoge
0 votes

2 Answers

0 votes

With the API 21 the method setNavigationMode(ActionBar.NAVIGATION_MODE_TABS) is deprecated.

You can use a different pattern. For example you can use the same example that you can see in googleio14.

It uses a SlidingTabLayout which works with a ViewPager.

Here you can find the example (it is in your sdk example)

Here you can find the Google io14 example:

UPDATE 29/05/2015

With the new Design Support Library now you can use the new TabLayout.

Just add this dependency to your build.gradle

compile ''

The code is very simple:

TabLayout tabLayout = (TabLayout) findViewById(;

To implement many of the features of material designs you should use it within a CoordinatorLayout and a AppBarLayout.

Something like this:"






answered Oct 11, 2015 by patilkiran.101
0 votes

Although probably a little late to answer this question now, I realised that I wrote an answer on a similar question which covers both using the Design Support Library and prior to Google I/O.

I have included the essential parts below:

Using a TabLayout with the Toolbar has become much simpler since the announcement of the Android Design Support Library meaning we no longer need to download custom view classes.

From the Android Developers' Blogspot post on the Android Design Support Library:


Switching between different views in your app via tabs is not a new concept to material design and they are equally at home as a top level navigation pattern or for organizing different groupings of content within your app (say, different genres of music).

The Design library’s TabLayout implements both fixed tabs, where the view’s width is divided equally between all of the tabs, as well as scrollable tabs, where the tabs are not a uniform size and can scroll horizontally. Tabs can be added programmatically:

TabLayout tabLayout = ...;
tabLayout.addTab(tabLayout.newTab().setText("Tab 1"));

However, if you are using a ViewPager for horizontal paging between tabs, you can create tabs directly from your PagerAdapter’s getPageTitle() and then connect the two together using setupWithViewPager(). This ensures that tab selection events update the ViewPager and page changes update the selected tab.

If you are not using the Design Support library, you will instead need to do the following:

1. Download the and files from Google's I/O Conference app on GitHub. These would be the views that would be used in the tab layout, so I created a folder with my other Java activities called 'view' and placed them there.

2. Edit your layout to include the SlidingTabLayout:






The line which references the Toolbar (), is referencing another XML file I had used to make the Toolbar.

3. Change the package names in and corresponding to where they were placed. In my case, I used something similar to: package com.mycompany.myapp.view; for both of these files. Organise imports and add in any necessary, as noted by the IDE you are using.

4. In your Activity (which was extending AppCompatActivity), setup the Toolbar in the onCreate method:

Toolbar toolbar = (Toolbar) findViewById(;

5. Setup the ViewPager and SlidingTabLayout parts:

mViewPager = (ViewPager) findViewById(;
mViewPager.setAdapter(new ViewPagerAdapter(getSupportFragmentManager()));
mSlidingTabLayout = (SlidingTabLayout) findViewById(;

The colour 'tab_line' was a colour I had declared in color.xml which would be the colour of the tab line indicator. Also note that the variables above were global which I defined previously in this activity:

SlidingTabLayout mSlidingTabLayout;
ViewPager mViewPager;

6. Finally, setup the ViewPagerAdapter which I had called earlier. This would be responsible for changing the page depending on which tab was selected. I used the following code:

public class ViewPagerAdapter extends FragmentPagerAdapter {

    public ViewPagerAdapter(FragmentManager fm) {

    public int getCount() {
        // Returns the number of tabs
        return 3;

    public Fragment getItem(int position) {
        // Returns a new instance of the fragment
        switch (position) {
            case 0:
                return new FragmentOne();
            case 1:
                return new FragmentTwo();
            case 2:
                return new FragmentThree();
        return null;

    public CharSequence getPageTitle(int position) {
        Locale l = Locale.getDefault();
        switch (position) {
            case 0:
                return getString(R.string.title_section1).toUpperCase(l);
            case 1:
                return getString(R.string.title_section2).toUpperCase(l);
            case 2:
                return getString(R.string.title_section3).toUpperCase(l);
        return null;

As I mentioned above, further details to these solutions are available on another question I answered, about using Sliding Tabs with the Toolbar.

answered Oct 11, 2015 by sandeep bhadauria