androidxmlkotlinmaterial-designbottomnavigationview

Android Bottom NavigationView taking too much height


I am not using any style/theme or any complex customization on it yet Bottom Navigation view is taking unexpected height,

I am using wrap_content for height so got following output:

bottom navigation screenshot

 <com.google.android.material.bottomnavigation.BottomNavigationView
    android:id="@+id/bottom_nav"
    android:layout_width="match_parent"
    **android:layout_height="wrap_content"**
    app:labelVisibilityMode="selected"
    app:menu="@menu/bottom_nav_menu" />

and when using fixed height i.e ?attr/actionbarsize I am getting following output(all icons/labels collapsed): output

code:

<com.google.android.material.bottomnavigation.BottomNavigationView
    android:id="@+id/bottom_nav"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    app:labelVisibilityMode="selected"
    app:menu="@menu/bottom_nav_menu" />

Full code of Layout:

    <?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".AppInstalledCheckActivity"
    android:background="#FFEB3B">

    <EditText
        android:id="@+id/edittextPackageName"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:hint="Enter Package Name" />

    <Button
        android:id="@+id/buttonCheckInstalled"
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:text="Enter Package Name" />

    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="400dp"
        app:navGraph="@navigation/navgraph" />

     <com.google.android.material.bottomnavigation.BottomNavigationView
    android:id="@+id/bottom_nav"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:labelVisibilityMode="selected"
    app:menu="@menu/bottom_nav_menu" />


</LinearLayout>

I want it to have equal or similar height as tool/actionbar.


Solution

  • Two changes needs to be made:

    Change root LinearLayout to RelativeLayout:

    <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:id="@+id/main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".AppInstalledCheckActivity">
    

    Comment/remove windowInsetsListener from activity's oncreate method

    /*        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
                val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
                v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
                insets
            }*/
    

    Final 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:id="@+id/main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context=".AppInstalledCheckActivity">
    
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
    
            <EditText
                android:id="@+id/edittextPackageName"
                android:layout_width="match_parent"
                android:layout_height="60dp"
                android:hint="Enter Package Name" />
    
            <Button
                android:id="@+id/buttonCheckInstalled"
                android:layout_width="match_parent"
                android:layout_height="45dp"
                android:text="Enter Package Name" />
    
        </LinearLayout>
    
        <androidx.fragment.app.FragmentContainerView
            android:layout_marginTop="150dp"
            android:id="@+id/nav_host_fragment"
            android:name="androidx.navigation.fragment.NavHostFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:navGraph="@navigation/navgraph" />
    
        <com.google.android.material.bottomnavigation.BottomNavigationView
            android:id="@+id/bottom_nav"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            app:itemTextAppearance="@style/TextAppearance.MaterialComponents.Caption"
            app:labelVisibilityMode="selected"
            app:menu="@menu/bottom_nav_menu" />
    
    
    </RelativeLayout>
    

    Output:output