0

i am building an application in which i want the Activity to have a bottomBar and each item of it to be a fragment. The first fragment should have a viewpager. Everything works fine except the fact that TabLayout and Toolbar do not have the same height.

The problem is:

enter image description here

and my activity layout is:

<android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <include
            android:id="@+id/toolbar"
            layout="@layout/toolbar_top" />
    </android.support.design.widget.AppBarLayout>

my toolbarTop layout:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:theme="@style/Base.ThemeOverlay.AppCompat.Dark"
    android:id="@+id/topBar"
    android:background="@color/colorPrimary">
</android.support.v7.widget.Toolbar>

and my tablayout fragment:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"

    android:layout_marginBottom="?android:attr/actionBarSize"
    android:layout_height="match_parent">

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        app:tabMaxWidth="0dp"
        app:tabGravity="fill"
        app:tabMode="fixed"
        android:elevation="6dp"
        android:layout_height="?android:attr/actionBarSize"
        android:background="@color/colorPrimary" />

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        app:layout_scrollFlags="scroll|enterAlways|snap"
        android:layout_height="wrap_content" />

</LinearLayout>

I have tried this answer but it was not successful. I cannot put the tablayout in the Appbarlayout(which solves the problem) because every fragment will have a tablayout.

Any other ideas to solve this problem?

Community
  • 1
  • 1

1 Answers1

0

You need to include it in the AppbarLayout so, to do that Add the TabLayout to toolbarTop.xml file below the toolbar.

Lalit Dhameliya
  • 348
  • 3
  • 8
  • if i do that every fragment will have the Tablayout –  Dec 16 '16 at 13:03
  • yes, for that you can hide the tablayout from other fragments. getActivity().findViewById(R.id.tab_layout).setVisibility(View.GONE); – Lalit Dhameliya Dec 16 '16 at 13:04
  • and how will setup the tablayout with viewpager in the fragment? –  Dec 16 '16 at 13:06
  • To setup tablayout with ViewPager you can do it with just one line of code. tabLayout.setupWithViewPager(mViewPager); – Lalit Dhameliya Dec 16 '16 at 13:08
  • the way you suggested i have to implement the tablayout in the activity and then the viewpager in the fragment..or you also suggest to have viewpager in every fragment? –  Dec 16 '16 at 13:09
  • No, You can have the ViewPager also in the same layout as tablayout. Add this both in the above file that i have told in answer. In fragment you can have your actual content not the viewpager. so it will solve your problem. – Lalit Dhameliya Dec 16 '16 at 13:14