Hossam Samir Tawfeek
Hossam Samir Tawfeek

Reputation: 31

The Titles do not appear in PagerTitleStrip at all on sdk 23 or 22

i have tried several things to display the text in the tabs on "scroll tabs" in my activity but it never shows

any ideas what happened?

below is the code:

scrollabletabs.xml:

<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="@+id/pager">
<android.support.v4.view.PagerTitleStrip
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:paddingTop="20dp"
    android:paddingBottom="20dp"
    android:layout_gravity="top"
    android:background="@color/material_deep_teal_500"
    android:id="@+id/tabtitle"
    style="@style/pagertitlestrip">

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

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

ScrolllableTabs.java:

package com.example.pc.learn_again;


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 ScrollableTabs extends FragmentActivity {

//get the ViewPager//
ViewPager viewPager=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.scrollabletabs);
    viewPager = (ViewPager) findViewById(R.id.pager);
    //the connection between the ViewPager and the adapter is below//
    FragmentManager fragmentManager = getSupportFragmentManager();
    viewPager.setAdapter(new myadapter(fragmentManager));
}
}

class myadapter extends FragmentPagerAdapter
{

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

@Override
public Fragment getItem(int position) {
    //as you see from the below this is how we tell the Viewpager which fragment (page) to be shown//
    Fragment fragment=null;
    if (position==0)
    {
        fragment = new ScrollableFragment1();
    }
    if (position==1)
    {
        fragment = new ScrollableFragment2();
    }
    if (position==2)
    {
        fragment = new ScrollableFragment3();
    }
    return fragment;
}

@Override
public int getCount() {

    return 3;//this is the number of pages
}
//the below is for the titles//

@Override
public CharSequence getPageTitle(int position) {

    if (position==0)
    {
        return "Login Screen";
    }
    if (position==1)
    {
        return " List View";
    }
    if (position==2)
    {
        return "Calculator";
    }
    return null;
}
}

let me point out that the fragments appear correctly and they work fine , but i do not know why the titles in the tabs do not show (is it possible that the method returns null?? and if yes....why??)

thx for the help guys :)

Upvotes: 3

Views: 1558

Answers (1)

Akshay Sood
Akshay Sood

Reputation: 150

Use:

<android.support.v4.view.PagerTabStrip

        android:id="@+id/strip"

        android:layout_gravity="top"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="4dp"
        android:textColor="#fff"

        android:background="@android:color/holo_blue_bright"></android.support.v4.view.PagerTabStrip>

instead of

<android.support.v4.view.PagerTitleStrip
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:paddingTop="20dp"
    android:paddingBottom="20dp"
    android:layout_gravity="top"
    android:background="@color/material_deep_teal_500"
    android:id="@+id/tabtitle"
    style="@style/pagertitlestrip">

The PageTabStrip worked for me.

Upvotes: 1

Related Questions