Scrolling tabs in Android

Source: Internet
Author: User

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 wocould be ID Eal to have a scrolling tab widget like your 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 entriesTabhostAndTabwidget. In the absence of any better ideas you might speculatively attempt to wrapTabwidgetInScrollview. Don't bother, it won't do anything,ScrollviewOnly provides vertical scrolling.

If, like me, you thought thatScrollviewWas the last word in scrolling Android widgets, you may have failed to noticeAndroid. widget. horizontalscrollview. The name fails to conceal what this class is about. It's the solution to your non-scrolling tab woes.

Wrap yourTabwidgetWithHorizontalscrollviewAnd 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 wowould 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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.