Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding titles to ViewPager

I have set up a ViewPager for swipeable tabs and now I want to add titles at the top of the page to indicate what fragment is being shown.

This is the code so far:

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <android.support.v4.view.ViewPager
        android:id="@+android:id/viewpager"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

</LinearLayout>

MainActivity.java

public class MainActivity extends FragmentActivity {

    private PagerAdapter mPagerAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.activity_main);

        this.initialisePaging();
    }

    private void initialisePaging() {

        List<Fragment> fragments = new Vector<Fragment>();

        // Add fragments here
        fragments.add(Fragment.instantiate(this, PickerFragment.class.getName()));
        fragments.add(Fragment.instantiate(this, DisplayFragment.class.getName()));

        this.mPagerAdapter  = new PageAdapter(super.getSupportFragmentManager(), fragments);
        ViewPager pager = (ViewPager)super.findViewById(R.id.viewpager);
        pager.setAdapter(this.mPagerAdapter);
    }

}

PageAdapter.java

public class PageAdapter extends FragmentPagerAdapter {

    private List<Fragment> fragments;

    public PageAdapter(FragmentManager fm, List<Fragment> fragments) {
        super(fm);
        this.fragments = fragments;
    }

    @Override
    public Fragment getItem(int position) {
        return this.fragments.get(position);
    }

    @Override
    public int getCount() {
        return this.fragments.size();
    }
}

This works and I am able to swipe between the two tabs but there are no titles.

I want to end up with something that looks like this:

Google play

I would appreciate any help on how I could set this up.

like image 586
Dan Avatar asked Dec 29 '12 14:12

Dan


People also ask

What the difference between ViewPager and ViewPager2?

ViewPager2 is an improved version of the ViewPager library that offers enhanced functionality and addresses common difficulties with using ViewPager . If your app already uses ViewPager , read this page to learn more about migrating to ViewPager2 .

Is ViewPager deprecated?

This method may be called by the ViewPager to obtain a title string to describe the specified page. This method is deprecated. This method should be called by the application if the data backing this adapter has changed and associated views should update.

How to use ViewPager?

You can create swipe views using AndroidX's ViewPager widget. To use ViewPager and tabs, you need to add a dependency on ViewPager and on Material Components to your project. To insert child views that represent each page, you need to hook this layout to a PagerAdapter .


2 Answers

After looking PagerTitleStrip, as suggested in the comments, I did the following:

I added a PagerTitleStrip in activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <android.support.v4.view.ViewPager
        android:id="@+android:id/viewpager"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <android.support.v4.view.PagerTitleStrip
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="top" />
    </android.support.v4.view.ViewPager>

</LinearLayout>

Then in PageAdapter.java I added the following method:

@Override
public CharSequence getPageTitle(int position) {
    return "Title Here";
}
like image 189
Dan Avatar answered Sep 20 '22 04:09

Dan


Since Android Support Design library 22.2.0, you can use TabLayout class. It's very similar to PagerTitleStrip, you just need to specify title on ViewPagerAdapter.

@Override
public CharSequence getPageTitle(int position) {
    return titleList.get(position);
}

and add it in anywhere in your xml.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.design.widget.TabLayout
        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="match_parent" />
</LinearLayout>

and lastly, tie your ViewPager and TabLayout together.

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

In my opinion, it looks nicer than PagerTitleStrip. TabLayout also suitable for Google Play-like tabs. If you want to do customization like @cjayem13 said (change tab color, text color, selected color etc.), it's very easy to achieve.. see official docs or CodePath's guide for further detail.

like image 43
aldok Avatar answered Sep 20 '22 04:09

aldok