DZone
Mobile Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Mobile Zone > Scrolling Tabs in Android

Scrolling Tabs in Android

Dan Dyer user avatar by
Dan Dyer
·
Apr. 18, 11 · Mobile Zone · Tutorial
Like (0)
Save
Tweet
71.33K Views

Join the DZone community and get the full member experience.

Join For Free

perhaps this is obvious but it wasn’t immediately clear to me so maybe it’s worth documenting for the benefit of future google searchers. if you use a tabactivity in an android application you can probably fit four or maybe five tabs on a standard screen in portrait orientation before things become too cramped. to display more tabs than this and still be able to read the text on each it would be ideal to have a scrolling tab widget like many ui toolkits do. but how can you do this in an android app?

reading the patchy android api documentation is rarely an illuminating experience at the best of times and it doesn’t help in this instance either because the possibility of scrolling tabs is not even mentioned in the entries for tabhost and tabwidget . in the absence of any better ideas you might speculatively attempt to wrap the tabwidget in a scrollview. don’t bother, it won’t do anything.

android scrollable tabs if, like me, you thought that scrollview with its horizontal and vertical scrolling was the last word in scrolling android widgets, you may have failed to notice the more modest capabilities of android.widget.horizontalscrollview . the name fails to conceal what this class is about. it’s the solution to your non-scrolling tab woes.

wrap your tabwidget with a horizontalscrollview and the tab set will be able to expand beyond the width of the screen and the user can drag the tabs left and right as required:

<?xml version="1.0" encoding="utf-8"?><tabhost xmlns:android="http://schemas.android.com/apk/res/android"         android:id="@android:id/tabhost"         android:layout_width="fill_parent"         android:layout_height="fill_parent">  <linearlayout android:orientation="vertical"                android:layout_width="fill_parent"                android:layout_height="fill_parent">    <horizontalscrollview android:layout_width="fill_parent"                          android:layout_height="wrap_content"                          android:fillviewport="true"                          android:scrollbars="none">      <tabwidget android:id="@android:id/tabs"                 android:layout_width="fill_parent"                 android:layout_height="wrap_content"/>    </horizontalscrollview>    <framelayout android:id="@android:id/tabcontent"                 android:layout_width="fill_parent"                 android:layout_height="fill_parent" />  </linearlayout></tabhost>

set android:fillviewport to true to ensure that the tabs stretch the full width like they would without a scroller and set android:scrollbars to none to avoid having a scrollbar displayed between the tabs and the content. the scrollable tabs will be rendered with faded edges on the right and/or left depending on which direction you can scroll (see image above).

from http://blog.uncommons.org/2011/04/18/scrolling-tabs-in-android/

Android (robot)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • CSS Position: Relative vs Position Absolute
  • 30 Common CI/CD Interview Questions (With Answers)
  • Cypress: The Future of Test Automation! Advantages and Disadvantages
  • Functional vs. Non-Functional Requirements: The Full Guide, Definitions, and Technical Examples

Comments

Mobile Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo