Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inflated tab items in Tab Layout does not match parent in android

I have gone through these but does not seems to work

working with

android.support.design.widget.TabLayout

needs to set custom views to each child tab as a match parent to child size

  • pragmatically give a size

        View v = LayoutInflater.from(this).inflate(R.layout.layout_two, null);
        v.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        tabLayout.getTabAt(0).setCustomView(v);
    

this works as a match parent because there is no background but once add a inflate view issue comes

 tabLayout.getTabAt(1).setText("About").setIcon(R.drawable.drop_complete);
  • though about the tabIndicator as well setting its height to app:tabIndicatorHeight="0dp" or adding android:tabStripEnabled="false" does not help.

here is a picture with boundaries which clearly shows the small gap like padding(blue bar is my TabLayout with two tabs )

left one is inflated right one is normal .setIcon

any help to make inflated one match parent with no gaps ?

Here is my code

XML

<android.support.design.widget.CoordinatorLayout
    android:id="@+id/main_layout"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appBarLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:elevation="30dp">


        //------- INSIDE THIS I HAVE A TAB
        <android.support.v7.widget.Toolbar
            android:background="@drawable/capture"
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:minHeight="?attr/actionBarSize"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            app:elevation="0dp"
            app:layout_scrollFlags="scroll|enterAlways"
            />


        //------ HERE comes THE TAB  That I DEAL 
        <android.support.design.widget.TabLayout
            android:tabStripEnabled="false"
            app:tabIndicatorHeight="0dp"
            android:id="@+id/tab_layout"
            android:scrollbars="none"
            android:layout_below="@+id/toolbar"
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            app:tabTextColor="#d3d3d3"
            android:minHeight="?attr/actionBarSize"
            android:orientation="vertical" >

        </android.support.design.widget.TabLayout>


    </android.support.design.widget.AppBarLayout>

    <android.support.v4.view.ViewPager
        android:background="#467"
        android:id="@+id/viewpager"
        android:layout_below="@+id/tab_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        />

</android.support.design.widget.CoordinatorLayout>

myClass

public class LayoutExampleActivity extends AppCompatActivity {
    private TabLayout tabLayout;


    private ViewPager viewPager;

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

        tabLayout = (TabLayout) findViewById(R.id.tab_layout);


        viewPager = (ViewPager) findViewById(R.id.viewpager);
        setupViewPagerAdaptor(viewPager);

        tabLayout.setupWithViewPager(viewPager);
        setTabs();


    }

    private void setupViewPagerAdaptor(ViewPager viewPager) {
        ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
        adapter.addFragment(new FragmentOne(), "First");
        adapter.addFragment(new GalleryFragment(), "Second");
//        adapter.addFragment(new FragmentOne(), "Third");
        viewPager.setAdapter(adapter);
    }

    class ViewPagerAdapter extends FragmentPagerAdapter {
        private final List<Fragment> mFragmentList = new ArrayList<>();
        private final List<String> mFragmentTitleList = new ArrayList<>();

        public ViewPagerAdapter(FragmentManager manager) {
            super(manager);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragmentList.get(position);
        }

        @Override
        public int getCount() {
            return mFragmentList.size();
        }

        public void addFragment(Fragment fragment, String title) {
            mFragmentList.add(fragment);
            mFragmentTitleList.add(title);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mFragmentTitleList.get(position);
        }
    }

    public void setTabs() {

        tabLayout.getTabAt(0).setCustomView(R.layout.layout_one);

        tabLayout.getTabAt(1).setText("About").setIcon(R.drawable.drop_complete);

    }

}

enter image description here

like image 951
Charuක Avatar asked Oct 19 '22 04:10

Charuක


1 Answers

Set tabPaddingStart and tabPaddingEnd attributes. like this:

    <android.support.design.widget.TabLayout
            ...
            app:tabPaddingStart="0dp"
            app:tabPaddingEnd="0dp" >

This question might be duplicated.
see: Cannot remove Padding from Tabs when using Custom views with Tab Layout

like image 142
nshmura Avatar answered Nov 01 '22 17:11

nshmura