Code Monkey home page Code Monkey logo

androidtabsexample's Introduction

Android Tabs Example

Example app showing how to implement tabs in Android.

App Screenshot

Table of Contents

Usage

  1. Import project into Android Studio
  2. Switch to the desired Git branch
  3. Hit Run

Branches

Each branch contains a fully working configuration:

Basics

Typically, an implementation of tabs in Android consists of:

  1. Swipe views
  2. Tabs UI element

These are two independent navigation patterns, but they can be combined with each other.

In general: swipe views can (but don't need to) be combined with tabs, and tabs can (but don't need to) be combined with swipe views. However, tabs benefit tremendously from being combined with swipe views, as explained below.

Swipe Views

Swipe views allow to flip through a set of "pages" by swiping horizontally on the screen.

Combined with tabs, this allows the user to switch to the next or previous tab by just swiping anywhere on the screen, rather than having to click on the tab itself.

The swipe views architecture is illustrated below:

Swipe Views Architecture

See branches 01_swipe_views_and_actionbar and 04_swipe_views_and_toolbar for code examples.

References:

Tabs

There are currently two ways to implement a tabs UI element in Android:

  1. TabLayout tabs using TabLayout from the Design Support Library
  2. ActionBar tabs using the ActionBar's NAVIGATION_MODE_TABS feature (DEPRECATED)

TabLayout Tabs

This is the preferred approach as it's easier to implement than ActionBar tabs and does not rely on an ActionBar.

TabLayout tabs are especially easy to implement if they are used in combination with a formerly implemented ViewPager (see Swipe Views), because then the tabs can be automatically populated by the ViewPager (i.e. no need to create and add the individual tabs manually). This is shown in the following:

  1. In the activity's layout XML, add a TabLayout element above the ViewPager element (and below the Toolbar element, if a Toolbar is used)
  2. In the activity's onCreate method, call setupWithViewPager(ViewPager) on the TabLayout to populate and integrate the TabLayout with the ViewPager (requires that getPageTitle of the PagerAdapter is overriden)

Note: to use TabLayout, the Design Support Library must be added to the project. In the module build.gradle file, do:

dependencies {
    compile 'com.android.support:support-v13:24.2.1'
}

See branches 06_tablayout_tabs_fixed_and_toolbar and 07_tablayout_tabs_scrollable_and_toolbar for code examples.

References:

ActionBar Tabs

Note that this approach was DEPRECATED in API level 21.

This requires the usage of the old-fashioned ActionBar as the app bar (rather than the "modern" Toolbar).

To implement ActionBar tabs, the following steps are needed in the activity's onCreate method:

  1. Set ActionBar to tab mode by setNavigationMode(ActionBar.NAVIGATION_MODE_TABS)
  2. Implement an ActionBar.TabListener
  3. Create tabs as ActionBar.Tabs and add them to the ActionBar

If the tabs are used in combination with swipe views (see above), the tabs must be bidirectionally integrated with the ViewPager:

  1. In [onTabSelected](https://developer.android.com/reference/android/app/ActionBar.TabListener.html#onTabSelected(android.app.ActionBar.Tab, android.app.FragmentTransaction)) of the ActionBar.TabListener, update the current page of the ViewPager (setCurrentItem)
  2. Implement a ViewPager.SimpleOnPageChangeListener, and in its onPageSelected method update the current tab of the ActionBar (setSelectedNavigationItem)

This ensures that when the user clicks on a tab, the ViewPager switches to the corresponding page (1), and vice versa, when the user swipes to another page, the corresponding tab is selected in the ActionBar (2).

See branch 03_actionbar_tabs_DEPRECATED for code examples.

References:

androidtabsexample's People

Contributors

weibeld avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.