27

Google has just implemented a new look to their tabs in Google Play.

I know this can be done with ViewPagerIndicator, yet I wouldn't like to use another library in my application and bump the app's size by another MB or so.

I am currently using the android.support.v4.view.PagerTabStrip (like in the old Google Play), and I'm wondering if the new look can also be implemented using the android support library.

Thanks in advance.

azizbekian
  • 60,783
  • 13
  • 169
  • 249
MrByte
  • 1,083
  • 2
  • 11
  • 21
  • 1
    Take a look at this: https://github.com/astuetz/PagerSlidingTabStrip – rciovati May 14 '13 at 13:14
  • Thank you very much, I've been looking for something that wouldn't bump my app's size and it seems like this is the one. Thank you :-) – MrByte May 14 '13 at 13:20
  • 1
    do we have to use external library, by default Android did not have this new sliding tab? – Eric Jun 13 '13 at 08:27

3 Answers3

53

Design Support Library (current method).

The Design Support Library includes the TabLayout widget which allows you to implement a Google Play-lie tabs:

<android.support.design.widget.TabLayout
    android:id="@+id/tabs"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

and then initializing it:

TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);

For a full example see the Cheesesquare app

PagerSlidingTabStrip Library

This is a ready-to-use library that you can find on Github.

Screenshot Screenshot

rciovati
  • 27,603
  • 6
  • 82
  • 101
34

Google finally released their sliding tabs api.

To use SlidingTabsBasic, you first need to download the zip file from: http://developer.android.com/downloads/samples/SlidingTabsBasic.zip

  1. Include the 2 java source files in the com.example.android.common.view package into your project. You may choose to move them to a suitable package in your project.
  2. To use the component, simply add it to your view hierarchy. Then in your Activity or Fragment, provide the SlidingTabsBasic your viewPager by calling mSlidingTabLayout.setViewPager(mViewPager);

Example layout from Google:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical">

    <com.example.android.common.view.SlidingTabLayout
          android:id="@+id/sliding_tabs"
          android:layout_width="match_parent"
          android:layout_height="wrap_content" />

    <android.support.v4.view.ViewPager
          android:id="@+id/viewpager"
          android:layout_width="match_parent"
          android:layout_height="0px"
          android:layout_weight="1"
          android:background="@android:color/white"/>

</LinearLayout>

Example code in the onCreate() method of your Activity:

mSlidingTabLayout = (SlidingTabLayout) view.findViewById(R.id.sliding_tabs);
mSlidingTabLayout.setViewPager(mViewPager);

For more details, follow the example in the zip file SlidingTabsBasicFragment.java and fragment_sample.xml

Tianhai
  • 663
  • 7
  • 14
1

try this library

Sample project

It uses ViewPager with fragment to display tabs as on google play.

Shruti
  • 1
  • 13
  • 55
  • 95
  • if you have implemented this project then can i ask 1 question? – TheFlash May 14 '13 at 13:19
  • Thank you for your reply, but the ViewPagerIndicator size is too big and I would like to my app as small as possible, that's why I have chosen not to use the ViewPagerIndicator. – MrByte May 14 '13 at 13:21
  • @EladCohen: When you tried ViewPagerIndicator, and you ran a production build to see what the net size increase was after ProGuard got rid of unused classes, what did you learn? – CommonsWare May 14 '13 at 13:24
  • I actually didn't run a production build yet, I am still developing my app (which I about to finish soon hopefully) and only then I will start testing it out on real devices insted of emulators. Is it possible to compile my application with ProGuard and use it on an emulator? – MrByte May 14 '13 at 13:30
  • The Eclipse plugin does not make it easy to run a "proguarded" application directly. That said, nothing prevents you to generate an APK and push it to the emulator using adb. Also, as a word of advice, don't wait until the end to test on real devices. You could get some nasty surprises – nicopico May 14 '13 at 14:33
  • Thank you very much nicopico & CommonsWare for the tips, I will test it out. – MrByte May 14 '13 at 15:10