Search code examples
androidandroid-viewpagermaterial-designpagertitlestrip

ViewPager and PageTitleStrip


So,I am trying to create Swipe tabs using ViewPager from suppoer.v4 library.

Everything works as meant,BUT the title-bar and tabs are not showing. I know that the tabs bar is there because like 2-3 pixels are shown of it (I set a specific color,that's how I know), but it's not fully shown, nor is the title.

I will link my MainActivity.java ,activity_main.xml and if anyone asks,I will add the two fragments.But I doubt that is the problem as they have nothing in there besides a linear layout and a textView.

MainActivity.java:

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;

public class MainActivity extends FragmentActivity {

    ViewPager viewPager=null;

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


        viewPager = (ViewPager) findViewById(R.id.viewPager);
        FragmentManager fm = getSupportFragmentManager();
        viewPager.setAdapter(new Adaptor(fm));
    }

    class Adaptor extends FragmentPagerAdapter{


        Fragment fragment = null;

        public Adaptor(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int i) {

            if(i==0){
                fragment = new OpiniaTa();
            }
            if(i==1){
                fragment = new Test();
            }

            return fragment;
        }

        @Override
        public int getCount() {
            return 2;
        }

        @Override
        public CharSequence getPageTitle(int position) {

            if(position == 0){
                return "Opinia ta";
            }
            if(position == 1){
                return "test";
            }

            return null;
        }
    }

}

activity_main.xml:

<android.support.v4.view.ViewPager
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/viewPager"
    android:layout_height="match_parent"
    android:layout_width="match_parent">

    <android.support.v4.view.PagerTitleStrip
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/titleTabs"
        android:background="#33B5E5"
        android:foregroundGravity="top"
        android:paddingTop="4dp"
        android:paddingBottom="4dp"
        >
    </android.support.v4.view.PagerTitleStrip>

</android.support.v4.view.ViewPager>

If any other information needed, let me know, I will be as prompt as possible.


Solution

  • Could you post a screenshot of the result. I am wondering if its just the content in tabs or the tabs itself are not displayed.

    There is an issue on 23.0.0 with text in tab missing https://code.google.com/p/android/issues/detail?id=183127 https://code.google.com/p/android/issues/detail?id=184431

    Maybe your issue is not related to this. Just want to make sure.

    Alternatively, use the below link as suggested the poster. Probably wrap_content to height...

    How can we work-around the blank title in PagerTitleStrip and PagerTabStrip?