android – TabLayout Height-ThrowExceptions

Exception or error:

I need help. The solution must be stupid, but I have not been able to solve it for 3 hours. I have a Toolbar and I have a TabLayout, which work perfectly. The only problem I have is that the height of the TabLayout that it takes is that of the toolbar. Then I have the size of the short TabLayout.

I leave all XML:

activity_main.xml

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

<include
layout="@layout/activity_drawer" />

<com.google.android.material.tabs.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="72dp"
android:backgroundTint="@color/white"
android:minHeight="72dp"
app:tabIconTint="@color/white"
app:tabIndicatorColor="@color/colorAccent"
app:tabIndicatorHeight="3dp"
app:tabSelectedTextColor="@color/colorAccent"
app:tabTextColor="@color/white">

</com.google.android.material.tabs.TabLayout>

</RelativeLayout>

Activity_drawer.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout 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:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start"
tools:showIn="@layout/activity_main">


<include
 layout="@layout/app_bar_drawer"
 android:layout_width="match_parent"
 android:layout_height="match_parent" />

<com.google.android.material.navigation.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_drawer"
app:menu="@menu/activity_drawer_drawer" />


</androidx.drawerlayout.widget.DrawerLayout>

app_bar_drawer.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 
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">


<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">


<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay">

</androidx.appcompat.widget.Toolbar>

</com.google.android.material.appbar.AppBarLayout>



<include layout="@layout/content_drawer" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

content_drawer.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
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"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/app_bar_drawer">

<fragment
    android:id="@+id/nav_host_fragment"
    android:name="androidx.navigation.fragment.NavHostFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:defaultNavHost="true"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:navGraph="@navigation/mobile_navigation" />

<androidx.viewpager.widget.ViewPager
    android:id="@+id/viewPager"
    android:layout_width="wrap_content"
    android:layout_height="0dp"
    android:layout_above="@id/nav_host_fragment"
    app:layout_constraintBottom_toBottomOf="@+id/nav_host_fragment"
    app:layout_constraintEnd_toEndOf="@id/nav_host_fragment"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

Image of app

How to solve:

You are using Relativelayout so you have to arrange all properties with

 android:layout_below  

attribute that will arrange all in linearly. or
you can use linearlayout with orientation vertical as root layout
this will automatically arrange all layout linear and vertical…
hope this will help you..

Leave a Reply

Your email address will not be published. Required fields are marked *