Android one tab TabLayout with full width not working at first
Asked Answered
Y

1

13

I'm trying to add only one tab in TabLayout with full-width indicator. but with google TabLayout I get this result google tab layout

after that i did used the same code with https://github.com/astuetz/PagerSlidingTabStrip and i got correct result PagerSlidingTabStrip library

did anyone faced the same problem before ? and how to solve this?

i did used simple code just activity holds

<android.support.design.widget.AppBarLayout
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:id="@+id/swipeAppBarLayout"
        >

        <android.support.design.widget.TabLayout
            android:id="@+id/swipeTabLayout"
            android:layout_width="match_parent"
            app:tabMaxWidth="0dp"
            android:layout_height="30dp"
            app:tabIndicatorHeight="4dp"
            />
    </android.support.design.widget.AppBarLayout>

    <android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/swipePager"
        android:background="@android:color/transparent"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

and the adapter

public class StreamNavigationPagerAdapter extends FragmentStatePagerAdapter {

    private Context mContext;

    public StreamNavigationPagerAdapter(FragmentManager fm, Context con) {
        super(fm);
        mContext = con;
    }


    @Override
    public Fragment getItem(int position) {
        return getNewsFragment();
    }

    private Fragment getNewsFragment() {
        Fragment fragment = new StreamNewsFragment();
        return fragment;
    }

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

    @Override
    public CharSequence getPageTitle(int position) {
        return "NEWS";
    }

}

the activity contain just

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

            swipePager.setAdapter(new StreamNavigationPagerAdapter(getSupportFragmentManager(), this));
            swipeTabLayout.setupWithViewPager(swipePager);
    //        swipeTabLayout.setViewPager(swipePager);
        }
Yoko answered 18/9, 2015 at 12:56 Comment(1)
Possible duplicate of Single tab in TabLayout is not covering the entire widthAlatea
N
35

I faced exactly the same issue and managed to solve it by setting the tabMaxWidth attribute to a high value rather (e.g. 500dp) than to 0dp as suggested in some answers.

<android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabMaxWidth="500dp" />

enter image description here

Nailbrush answered 24/4, 2016 at 19:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.