Leandro Hoffmann Leandro Hoffmann - 1 year ago 82
Android Question

Use Tab with new ToolBar (AppCompat v7-21)

I was using Support ActionBar with tabs and using custom ActionBar theme (created with http://jgilfelt.github.io/android-actionbarstylegenerator/), 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(R.id.new_actionbar);

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?

Answer Source

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 'com.android.support:design:22.2.0'

The code is very simple:

TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);

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

Something like this:






        app:layout_behavior="@string/appbar_scrolling_view_behavior" />


You can find more info in the Stackoverflow Documentation.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download