androidtabsandroid-sliding

Tried of making swipeable tab using PagerSlidingTabStrip but tab title is not coming only the content of layout is coming


Hi I tried to make the swipleable tab layout. Layout is coming and is being swipleable but the title of tab is not coming. In my MainActivity code is :

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

    // Initialization
    viewPager = (ViewPager) findViewById(R.id.pager);
    viewPager.setAdapter(new TabsPagerAdapter(getSupportFragmentManager()));

    PagerSlidingTabStrip tabsStrip = (PagerSlidingTabStrip) findViewById(R.id.tabs);
    tabsStrip.setViewPager(viewPager);

    tabsStrip.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            Toast.makeText(EditActivity.this, "Selected page position: " + position, Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });
}

And my TabsPagerAdapter code is like:

public class TabsPagerAdapter extends FragmentPagerAdapter {

    // Tab titles
    private String[] tabs = new String[] {"Profile", "Child"};

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

    @Override
    public Fragment getItem(int position) {

        switch (position) {
            case 0:
                // User fragment activity
                return new UserFragment();
            case 1:
                // Child fragment activity
                return new ChildFragment();
        }
        return null;
    }

    @Override
    public int getCount() {
        return tabs.length;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        // Generate title based on item position
        return tabs[position];
    }
}

And my mainactivity_layout.xml is:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout 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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:paddingBottom="@dimen/activity_no_padding"
android:paddingLeft="@dimen/activity_no_padding"
android:paddingRight="@dimen/activity_no_padding"
android:paddingTop="@dimen/activity_no_padding"
tools:context="com.samvardhan.org.vaccinationinfo.EditActivity">

<com.astuetz.PagerSlidingTabStrip
    android:id="@+id/tabs"
    app:pstsShouldExpand="true"
    android:layout_width="match_parent"
    android:layout_height="48dp"
    android:textSize="14sp"
    android:textColor="#000000"
    app:pstsDividerColor="@color/green"
    app:pstsIndicatorColor="@color/red"
    app:pstsUnderlineColor="@color/blue"
    app:pstsTabPaddingLeftRight="14dp"/>

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

</RelativeLayout>

And I have two layout xml file for my two tabs. The content of two tabs is coming but not the title of the tabs.

TAB 1 content

TAB 2 content

Only content is coming of tabs but the no title. What I am missing?


Solution

  • Add a

    android:layout_below="@+id/tabs"
    

    to your ViewPager in xml.