Action bar navigation modes are deprecated in Android L

AndroidAndroid ActionbarAndroid NavigationAndroid 5.0-Lollipop

Android Problem Overview


Taking a look at the API diff report for the Android "L" preview, I see that all methods related to navigation modes in the ActionBar class (such as setNavigationMode(), addTab(), selectTab(), &c). are now deprecated.

The documentation explains:

> Action bar navigation modes are deprecated and not supported by inline > toolbar action bars. Consider using other common navigation patterns > instead.

What is the supposed replacement?

Also, is "inline toolbar action bars" a new concept? I don't think I've heard of it before.

Android Solutions


Solution 1 - Android

The new Android Design Support Library adds TabLayout, providing a tab implementation that matches the material design guidelines for tabs. A complete walkthrough of how to implement Tabs and ViewPager can be found in this video

Now deprecated: The PagerTabStrip is part of the support library (and has been for some time) and serves as a direct replacement. If you prefer the newer Google Play style tabs, you can use the PagerSlidingTabStrip library or modify either of the Google provided examples SlidingTabsBasic or SlidingTabsColors as explained in this Dev Bytes video.

Solution 2 - Android

Now that the Android 5.0 docs are available, we have the official documentation for the Toolbar widget:

> A standard toolbar for use within application content. > > A Toolbar is a generalization of action bars for use within > application layouts. While an action bar is traditionally part of an > Activity's opaque window decor controlled by the framework, a Toolbar > may be placed at any arbitrary level of nesting within a view > hierarchy.

A Toolbar widget can also be used to replace the action bar:

> An application may choose to designate a Toolbar as the action bar for > an Activity using the setActionBar() method.

The deprecation of tabs in the action bar is most probably due to this, since toolbars cannot contain tab themselves.

Also, it's available for previous Android verions via the appcompat library. See this post by Chris Banes for more information. An excerpt:

> Android 5.0 introduces a new Toolbar widget. This is a generalization > of the ActionBar pattern but gives you much more control and > flexibility in using it. Toolbar is a view in your hierarchy just like > any other, making it easier to interleave with the rest of your views, > animate, react to scroll events.

Solution 3 - Android

It seems like they added a new Class named android.widget.Toolbar that extends ViewGroup. Also they added a new method setActionBar(Toolbar) in Activity. I haven't tested it yet, but it looks like you can wrap all kinds of TabWidgets, Spinners or custom views into a Toolbar and use it as your Actionbar.

Solution 4 - Android

The new Toolbar cannot be used for inflating multiple line objects, so it is impossible to add Tabs to it.
If you want to use a Toolbar like a TabWidget you can insert some Tab Objects to it, but only with the old Holo style.

Here there is a custom Library that uses v7 Toolbar like TabWidget with the new Material Design animations, but it uses the same methods from the old ActionBar Tabs, so you can attach your ViewPager to it.

Solution 5 - Android

For 'replacement' of deprecated ActionBar, I changed the type of my ActionBar-type variables to PagerTabStrip, as per (old code in comment):

// ActionBar bigActionBar;
PagerTabStrip bigActionBar;

A 'replacement' for ~actionBar's .selectTab(tabindex) was to use my associated ViewPager's .setCurrentItem(int) method, like this (old code in comment):

/*
ActionBar.Tab eventTab = bigActionBar.getTabAt(2);
bigActionBar.selectTab(eventTab);
*/
mViewPager.setCurrentItem(2);

Hope this is helpful.

Solution 6 - Android

I had the same problem and this solution suited me quite nicely:

In the layout xml file that contains the viewpager, add the a PagerTabStrip as shown:

http://schemas.android.com/apk/res/android" android:id="@+id/pager2" android:layout_width="match_parent" android:layout_height="match_parent">

<android.support.v4.view.PagerTabStrip
    android:id="@+id/pager_tab_strip"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="top"
    android:background="#996633"
    android:textColor="#CCCCCC"
    android:paddingTop="5dp"
    android:paddingBottom="5dp" />

To control page titles, add a switch statement to your ViewPager file:

@Override
public CharSequence getPageTitle(int position)
{
    switch (position)
    {
	case 0:
	    return "Page 1";
	    
	case 1:
	    return "Page 2";
	    
	case 2:
	    return "Page 3";

    }

    return null;
}

Solution 7 - Android

FragmentTabHost is also an option.

This code is from Android developer's site:

/**
 * This demonstrates how you can implement switching between the tabs of a
 * TabHost through fragments, using FragmentTabHost.
 */
public class FragmentTabs extends FragmentActivity {
    private FragmentTabHost mTabHost;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.fragment_tabs);
    mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
    mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);

    mTabHost.addTab(mTabHost.newTabSpec("simple").setIndicator("Simple"),
            FragmentStackSupport.CountingFragment.class, null);
    mTabHost.addTab(mTabHost.newTabSpec("contacts").setIndicator("Contacts"),
            LoaderCursorSupport.CursorLoaderListFragment.class, null);
    mTabHost.addTab(mTabHost.newTabSpec("custom").setIndicator("Custom"),
            LoaderCustomSupport.AppListFragment.class, null);
    mTabHost.addTab(mTabHost.newTabSpec("throttle").setIndicator("Throttle"),
            LoaderThrottleSupport.ThrottledLoaderListFragment.class, null);
    }
}

Solution 8 - Android

I found these tutorials helpful while putting together an action bar (now the 'tool bar' - argh) that supports sliding tabs with Material Design:

https://www.youtube.com/watch?v=Fl0xMuo10yA

http://www.exoguru.com/android/material-design/navigation/android-sliding-tabs-with-material-design.html

You sort of have to synthesize these resources to match your particular situation. For example, you may not want to manually create the tabs in the same style that the exoguru.com tutorial did.

Solution 9 - Android

Well for me to handle the deprecated navigation toolbar by using toolbar v7 widget appcompat.

    setSupportActionBar(toolbar);
    getSupportActionBar().setSubtitle("Feed Detail");
    toolbar.setNavigationOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //goToWhere
        }
    });

Solution 10 - Android

I think a suitable replacement for when you have three to five screens of equal importance is the BottomNavigationActivity,this can be used to switch fragments.

You will notice a wizard exists for this in Android Studio, take care however as Android Studio has a tendency to produce overly complex boiler plate code.

A tutorial can be found here: https://android.jlelse.eu/ultimate-guide-to-bottom-navigation-on-android-75e4efb8105f

Another quality tutorial can be found at Android Hive here: https://www.androidhive.info/2017/12/android-working-with-bottom-navigation/

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmatiashView Question on Stackoverflow
Solution 1 - AndroidianhanniballakeView Answer on Stackoverflow
Solution 2 - AndroidmatiashView Answer on Stackoverflow
Solution 3 - AndroidKunoView Answer on Stackoverflow
Solution 4 - AndroidNeoKreeView Answer on Stackoverflow
Solution 5 - AndroidapplesaucedView Answer on Stackoverflow
Solution 6 - AndroidAR RossView Answer on Stackoverflow
Solution 7 - AndroidAnujView Answer on Stackoverflow
Solution 8 - Androidkip2View Answer on Stackoverflow
Solution 9 - AndroidNewbie DeveloperView Answer on Stackoverflow
Solution 10 - AndroidAndrew SView Answer on Stackoverflow