PagerTabStrip is not showing

I am making swipe tab view. I want to show PagerTabStrip at bottom of SupportActionBar. All the tabs contents showing well but the problem is the title of these tabs not showing also PagerTabStrip is not showing on the bottom of SupportActionBar.

main_tab.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="horizontal">


    <android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/pager"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    tools:context=".MainActivity" >

        <android.support.v4.view.PagerTabStrip
            android:layout_width="wrap_content"
            android:layout_height="100dp"
            android:background="#e62117"
            android:id="@+id/tab_strip"
            android:focusableInTouchMode="false">
        </android.support.v4.view.PagerTabStrip>

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

tab1, tab2, tab3: similar

  public class tab1 extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.tab1,container,false);
return view;
    }
}

ma_page_adapter:

public class ma_pager_adapter extends FragmentPagerAdapter {
    public ma_pager_adapter(FragmentManager fm) {
        super(fm);
    }


    @Override
    public Fragment getItem(int i) {

        switch (i) {
            case 0:
                tab1 t1 = new tab1();
                return t1;
            case 1:
                tab2 t2 = new tab2();
                return t2;
            case 2:
                tab3 t3 = new tab3();
                return t3;

        }
        return null;
    }

    @Override
    public int getCount() {
        return 3;
    }//set the number of tabs

    @Override
    public CharSequence getPageTitle(int position) {
        Locale l = Locale.getDefault();
        switch (position) {
            case 0:
                return "Top stories";
            case 1:

                return "Members";
            case 2:

                return "Setting";
        }
        return null;
    }
}

MainActivity:

   ViewPager pager;
    PagerTabStrip tab_strp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_tab);
        ma_pager_adapter mapager = new ma_pager_adapter(getSupportFragmentManager());


        pager = (ViewPager)findViewById(R.id.pager);
        pager.setAdapter(mapager);
//        tab_strp = (PagerTabStrip) findViewById(R.id.tab_strip);
        ((ViewPager.LayoutParams) findViewById(R.id.tab_strip).getLayoutParams()).isDecor = true;
//        tab_strp.setTextColor(Color.WHITE);
    }
}

Upvotes: 1

Views: 683

Answers (3)

Iaouei
Iaouei

Reputation: 21

I have run your code,its running normal.If you want to show it on the bottom of yours ViewPager,you may use"android:layout_gravity "and set it bottom.

Upvotes: 2

Adil Hussain
Adil Hussain

Reputation: 32103

Remove this line from your tab1.onCreateView(...) method...

((ViewPager.LayoutParams) (view.findViewById(R.id.tab_strip)).getLayoutParams()).isDecor = true;

... and add it to your MainActivity.onCreate(...) method. Your app will stop crashing.

Your tab1 Fragment class has no knowledge of the R.id.tab_strip view. That is a view of the MainActivity class.

Upvotes: 0

Parag Goel
Parag Goel

Reputation: 31

Your question duplicates the issue below:

PagerTabStrip not showing in ViewPager in app targeting Android 6.X (N)

For apps targeting Android N, you need to use the following workaround:

In onCreateView(), add the following:

((ViewPager.LayoutParams) (view.findViewById(R.id.tab_strip)).getLayoutParams()).isDecor = true;

Upvotes: 2

Related Questions