1

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);
    }
}

3 Answers3

2

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;

Community
  • 1
  • 1
Parag Goel
  • 31
  • 3
2

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.

Iaouei
  • 21
  • 3
0

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.

Adil Hussain
  • 30,049
  • 21
  • 112
  • 147
  • 1
    Dude, I can't see why your app is crashing from the code you have pasted in your question. Please create a separate Stack Overflow question that shows the code and the stack trace of the crash and we'l get to the bottom of it. The answer by @L.Liu seems to suggest that the code in your question is fine and it's probably something else that you're doing in your app that's causing it to crash. Like I said, create a separate question, show me the stack trace of the crash, send me the link and we'll investigate. – Adil Hussain Jul 18 '16 at 11:47