204

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.

matiash
  • 52,725
  • 16
  • 117
  • 149

10 Answers10

179

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.

ianhanniballake
  • 155,909
  • 19
  • 351
  • 362
  • 8
    This is a direct replacement in portrait mode, with the bonus of making it easy to use an extended Material-type header. However, in landscape mode the ActionBar tabs automatically move up into the action bar itself, freeing important screen area. Anybody know a clean way to manage this with the support library? – ehartwell Jul 08 '14 at 03:23
  • 1
    [This Google+ post](https://plus.google.com/+LucasRocha/posts/eGR9P5mSRXm) about Toolbars has a comment from Chris Banes, one of the Google developers who built this new functionality, stating that the support library work to support this change is in work. I'd expect more information closer to Android L's release. – ianhanniballake Jul 08 '14 at 03:42
  • 9
    The developer document still says using Action Bar Tab. Simlpy not updated yet?http://developer.android.com/training/implementing-navigation/lateral.html#swipe-tabs – Romulus Urakagi Ts'ai Oct 23 '14 at 02:00
  • 3
    This is a replacement for NAVIGATION_MODE_TABS but not for NAVIGATION_MODE_LIST :/ – Geob-o-matic Oct 24 '14 at 12:41
  • 2
    @Geobert - `NAVIGATION_MODE_LIST` is much easier, given that all that did was add a [Spinner](http://developer.android.com/reference/android/widget/Spinner.html) and some callback helpers. – ianhanniballake Oct 24 '14 at 15:47
  • Thank you so much, it helped me a lot. – Kamran Ahmed Oct 29 '14 at 07:46
  • 2
    What is the replacement for `NAVIGATION_MODE_LIST`? Mine lists it as deprecated... – Sauron Dec 13 '14 at 17:06
  • The video linked to with instructions for implementing tabs is good, but the tutorial (https://developer.android.com/training/implementing-navigation/lateral.html#tabs) it links to at the end is the same one which uses the deprecated NAVIGATION_MODE_TABS, the very reference which led me to this SO question in the first place. – Philip Stratford Dec 20 '16 at 11:32
  • 1
    It's amazing that they still use a feature in their offical "training" guides that has been deprecated for years now. – algorithms Nov 20 '17 at 12:14
  • 4 years and their official document hasn't been updated. – Duan Nguyen Sep 02 '18 at 07:40
20

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.

matiash
  • 52,725
  • 16
  • 117
  • 149
  • 53
    "much more control and flexibility" is only nice if there are sensible and easy to use defaults. If something is flexible but you need 20 lines here and there to make it work, I don't consider it a win... – TWiStErRob Oct 21 '14 at 16:08
  • 1
    @TWiStErRob I agree. Emphasis his, not mine :) – matiash Oct 22 '14 at 13:50
  • 6
    @TWiStErRob I feel the same way...Google better watch out, this kind of activity is reminding me of the old Microsoft – Sauron Dec 13 '14 at 17:26
13

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.

Kuno
  • 3,327
  • 2
  • 24
  • 41
  • 1
    Can someone share if they tried anything with the Toolbar widget or knows how it is intended to work. Also is it included in the compatibility package? – Sandra Jul 10 '14 at 10:35
  • Have a look at https://plus.google.com/+IanLake/posts/aanbpbAcUKW?utm_source=Android%20Weekly&utm_campaign=aa354b61eb-Android_Weekly_110&utm_medium=email&utm_term=0_4eb677ad19-aa354b61eb-337825901 – Kuno Jul 14 '14 at 09:18
  • 1
    Thank u very much for the link..so now if I want to develop an app that would be according to the new material guidelines, I should probably wait for the toolbar to become available, because if I use the actionbar I would be using everything that is deprecated.. ? – Sandra Jul 14 '14 at 11:01
  • 1
    The ActionBar will still be a standard design pattern. Only the navigation modes will be deprecated (Tabs or Spinner). See http://developer.android.com/guide/topics/ui/actionbar.html#Tabs and http://developer.android.com/guide/topics/ui/actionbar.html#Dropdown – Kuno Jul 14 '14 at 11:06
  • Well you'll still have to implement for Android <= 4.4.x and Android L because you know how long it takes for new Android versions to distribute :(... Unless the Toolbar will be part of the support lib, what I don't see coming right now. – Kuno Jul 14 '14 at 11:18
  • From the above link that you posted, it seems like they are working on compatibility for the toolbar, but you're right about the time it will take for us to see it in action and actually use it. So thx for the links, you at least helped me make up my mind. – Sandra Jul 14 '14 at 11:25
  • Oops, I didn't have the chance to read chris banes comment. I just remembered that there was a post in the current android weekly about your problem. Sorry for that : ) – Kuno Jul 14 '14 at 11:33
  • 1
    I think that it is related to the question.. the new i/o 2014 application's apk is available for download, and as you can see from https://plus.google.com/+RomanNurik/posts/PD6MS3feXTT, there are plenty of toolbar styles used. We just have to wait for the source code, and explore it then :( – Sandra Jul 16 '14 at 09:26
4

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.

NeoKree
  • 362
  • 3
  • 13
3

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.

1

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:

<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;
}
AR Ross
  • 19
  • 1
0

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);
    }
}
Anuj
  • 1,050
  • 2
  • 16
  • 34
  • 1
    Here is a tutorial which includes this answer, so it might help someone http://www.skholingua.com/android-basic/user-interface/compositeitems/tabhost – Dejan Apr 11 '15 at 16:08
0

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.

kip2
  • 4,801
  • 3
  • 39
  • 59
0

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
        }
    });
0

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/

Andrew S
  • 2,390
  • 2
  • 27
  • 44