0

I have created a new bottom navigation activity using the android studio wizard. The problem seems to be the activity_main layout. The fragment view isn't extending to the top of the screen, but instead it's covered by the bottom navigation bar. enter image description here

No matter what I do, I can't move it up.

That's the XML code:

<?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"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="?attr/actionBarSize">

   <com.google.android.material.bottomnavigation.BottomNavigationView
    android:id="@+id/nav_view"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="0dp"
    android:layout_marginEnd="0dp"
    android:background="?android:attr/windowBackground"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:menu="@menu/bottom_nav_menu" />

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

</androidx.constraintlayout.widget.ConstraintLayout>
ChocolateChapta
  • 148
  • 2
  • 11

2 Answers2

0

You need to set

 android:layout_width="0dp"
 android:layout_height="0dp"

to switch from "normal sizing" to "apply constraint sizing"

muetzenflo
  • 5,653
  • 4
  • 41
  • 82
0

Remove the below padding from the root layout:

android:paddingTop="?attr/actionBarSize"
Zain
  • 37,492
  • 7
  • 60
  • 84
  • why did the android studio wizard add that? – ChocolateChapta Oct 01 '21 at 11:06
  • It's weird to me, but probably because the padding equals to the `actionBarSize` I think they consider that you could implement a custom `toolBar` as an `actionBar` in this space – Zain Oct 01 '21 at 11:08
  • It may be that the wizard looks at your theme defined in the `AndroidManifest.xml` `` tag. If you are using a `.NoActionBar` theme, it will probably not add this line. But that's just a wild guess.... – muetzenflo Oct 01 '21 at 12:30