Search code examples
androidandroid-5.0-lollipopandroid-tabs

Creating Swipe Views with tabs in Android 5.0


In making the swipe tabbed layout as below prior to Android 5.0:

enter image description here

Simply required the code as:

@Override
public void onCreate(Bundle savedInstanceState) {
    final ActionBar actionBar = getActionBar();
    ...

    // Specify that tabs should be displayed in the action bar.
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    // Create a tab listener that is called when the user changes tabs.
    ActionBar.TabListener tabListener = new ActionBar.TabListener() {
        public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
            // show the given tab
        }

        public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
            // hide the given tab
        }

        public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
            // probably ignore this event
        }
    };

    // Add 3 tabs, specifying the tab's text and TabListener
    for (int i = 0; i < 3; i++) {
        actionBar.addTab(
                actionBar.newTab()
                        .setText("Tab " + (i + 1))
                        .setTabListener(tabListener));
    }
}

But given the methods for navigation on the ActionBar are deprecated, How can I implement this type of view to be compatible withe Android 5.0 and previous versions?


Solution

  • You need to update to Toolbar here is the documentation.

    http://android-developers.blogspot.mx/2014/10/appcompat-v21-material-design-for-pre.html