How to make ViewPager fit under tabs?

Viewed 33

I'm programming an Android application and I have encountered an issue with the height of the ViewPager element. I have search for an answer but I haven't found one yet. My problem is that I cannot make my ViewPager fit under the tabs, it occupies the entire screen:

My ViewPager

This is my xml code:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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=".frontend.Profile">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/background"
        tools:context=".frontend.Profile">

        <de.hdodenhof.circleimageview.CircleImageView
            android:id="@+id/profile_image"
            android:layout_width="85dp"
            android:layout_height="85dp"
            android:src="@drawable/ic_user"
            app:civ_border_color="#FF000000"
            app:civ_border_width="2dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.029999971" />

        <TextView
            android:id="@+id/usernameText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:fontFamily="@font/sourcesanspro_regular"
            android:gravity="center_horizontal"
            android:letterSpacing="0.1"
            android:text="John Smith"
            android:textAlignment="center"
            android:textColor="@color/black"
            android:textSize="22sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.17000002" />

        <com.google.android.material.tabs.TabLayout
            android:id="@+id/tabLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/white"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.24000001">

            <com.google.android.material.tabs.TabItem
                android:id="@+id/tabRoutines"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Routines" />

            <com.google.android.material.tabs.TabItem
                android:id="@+id/tabStats"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Stats" />
        </com.google.android.material.tabs.TabLayout>

        <androidx.viewpager.widget.ViewPager
            android:id="@+id/viewPager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/tabLayout"
            app:layout_constraintVertical_bias="1.0">
        </androidx.viewpager.widget.ViewPager>

    </androidx.constraintlayout.widget.ConstraintLayout>
</FrameLayout>

I would really appreciate if someone could tell me what I'm doing wrong.

Thank you!

1 Answers

You need to constraints the TabLayout to:

  • Be at the bottom of the TextView with
    app:layout_constraintTop_toBottomOf="@+id/usernameText"
  • And remove the app:layout_constraintTop_toTopOf="parent"

And use a 0dp for the ViewPager height to match constraints.

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/background"
        tools:context=".frontend.Profile">

        <de.hdodenhof.circleimageview.CircleImageView
            android:id="@+id/profile_image"
            android:layout_width="85dp"
            android:layout_height="85dp"
            android:src="@drawable/ic_user"
            app:civ_border_color="#FF000000"
            app:civ_border_width="2dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.029999971" />

        <TextView
            android:id="@+id/usernameText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:fontFamily="@font/sourcesanspro_regular"
            android:gravity="center_horizontal"
            android:letterSpacing="0.1"
            android:text="John Smith"
            android:textAlignment="center"
            android:textColor="@color/black"
            android:textSize="22sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.17000002" />

        <com.google.android.material.tabs.TabLayout
            android:id="@+id/tabLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/white"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/usernameText">

            <com.google.android.material.tabs.TabItem
                android:id="@+id/tabRoutines"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Routines" />

            <com.google.android.material.tabs.TabItem
                android:id="@+id/tabStats"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Stats" />
        </com.google.android.material.tabs.TabLayout>

        <androidx.viewpager.widget.ViewPager
            android:id="@+id/viewPager"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/tabLayout"
            app:layout_constraintVertical_bias="1.0" />

    </androidx.constraintlayout.widget.ConstraintLayout>
</FrameLayout>
Related