Reputation: 333
When creating a new app in Android Studio, using the Bottom Navigation Activity
, I've found that having things in the bottom of the fragment will not be shown, because the BottomNavigationView
is hiding it. For example, I added a button to the fragment_home.xml
file, and constrained it to the bottom of the fragment.
I've tried setting the layout_width
and layout_height
of the fragment
to both match_parent
and wrap_content
but neither changed anything. I fiddled with constraints for both the BottomNavigationView
and the fragment
, but that also didn't change anything. Any help would be appreciated.
So here's the activity_main.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"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
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>
And here's the fragment_home.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"
tools:context=".ui.home.HomeFragment">
<TextView
android:id="@+id/text_home"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:textAlignment="center"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
If you launch android studio and create a new app using the bottom navigation activity example, and replace these two xml files, you'll see the issue I'm having.
Upvotes: 1
Views: 1862
Reputation: 2110
I had this issue but only once I added a pinned view to the bottom of the fragment.
Initially I had a recycler view with a height of 0dp
and it worked as expected (i.e. it would stretch to fill the screen but only to the top of the bottom navigation view).
But with the pinned view it was pinned to the bottom of the screen underneath the bottom navigation view.
Weirdly enough it was fixed when I did added this to the pinned view:
android:paddingBottom="0dp"
Upvotes: 0
Reputation: 81
I had the same problem, the solution that worked for me was changing the layout height in the activity_main.xml file:
From : android:layout_height="match_parent" ,
To : android:layout_height="0dp".
Once you set constraints for the fragment height, if you leave it on 'match parent' and not set to "0", it will ignore the constraints you set.
<fragment
android:id="@+id/nav_host_fragment_activity_main"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
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" />
Upvotes: 7
Reputation: 679
Literally don't waste your time and copy this:
android:paddingBottom="56dp"
On your root tag like this:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="56dp"
tools:context=".RouteFragment">
Upvotes: 0
Reputation: 333
One possible solution I've found is that I could add android:paddingBottom="56dp"
to the fragment_home.xml file:
<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"
android:paddingBottom="56dp"
tools:context=".ui.home.HomeFragment">
...
However that doesn't really answer what I want to know - which is why the fragment
view doesn't respect the constraints that I give it..
Upvotes: 2