0

There are certain apps that have Action Bar + Tabs, and when you scroll down, the tabs shrink until there is just the Action Bar visible.

I have this code of my own:

XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/primary_color"
android:orientation="vertical"
tools:context="com.example.android.miwok.MainActivity">

<android.support.design.widget.TabLayout
    android:id="@+id/tabs"
    style="@style/CategoryTab"
    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"/>

What can I do to achieve such behaviour?

1 Answers1

0

I Tried this and it works...

In the adapter class, create a Context object, add it in constructor of Adapter class.

private Context context;

public MyAdapter(FragmentManager fm,Context context) {
    super(fm);
    this.context = context;
}

then create a function in the Adapter class getPageTitle and use context to return String.

public CharSequence getPageTitle(int position) {

    switch (position) {

        case 0 :
            return context.getString(R.string.chat);
        case 1:
            return context.getString(R.string.status);
        case 2:
            return context.getString(R.string.call);

    }

    return super.getPageTitle(position);
}

now, go to the MainActivity. while setting adapter to view pager use this,

viewPager.setAdapter(new MyAdapter(getSupportFragmentManager(),this));

and the last step is to use the funtion setupWithViewPager.

tabLayout.setupWithViewPager(viewPager);

Hope it works for you. :)

For further queries you can also connect with me on Facebook and Instagram

www.facebook.com/himanshu.dubey.967806

www.instagram.com/_xplosion__/?hl=en