Search code examples
androidxmlandroid-studioandroid-layoutandroid-appbarlayout

elements going behind status bar and bottom navigation bar


bottom navigation view is going behind bottom nav bar and the toolbar is going behind the status bar

I am new to android and I have been stuck with this issue from day one. I have tried to set specific margins for the toolbar and bottomnavigationview but I don't think it's a good solution. Can someone please help me with this. I have also tried android:fitsystemwindows = "true" but it did not work. below is the fragment's xml code

<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:orientation="vertical"
    tools:context="fragments.homeUi">

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="65dp"
            android:background="?attr/colorPrimary"
            android:minHeight="?attr/actionBarSize"
            android:theme="?attr/actionBarTheme"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    </com.google.android.material.appbar.AppBarLayout>

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="167dp"
        android:layout_marginBottom="78dp"
        android:text="@string/home"
        app:layout_constraintBottom_toTopOf="@+id/signOutButton"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.029"
        app:layout_constraintStart_toStartOf="parent" />

    <Button
        android:id="@+id/signOutButton"
        android:layout_width="100dp"
        android:layout_height="50dp"
        android:layout_marginStart="143dp"
        android:layout_marginTop="44dp"
        android:layout_marginEnd="168dp"
        android:text="@string/signout"
        android:textAllCaps="false"
        android:textColor="#736B3B"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView4" />

    <androidx.cardview.widget.CardView
        android:id="@+id/cardView"
        android:layout_width="409dp"
        android:layout_height="230dp"
        android:layout_marginStart="1dp"
        android:layout_marginTop="140dp"
        android:layout_marginEnd="1dp"
        android:layout_marginBottom="116dp"
        app:cardCornerRadius="10dp"
        app:layout_constraintBottom_toTopOf="@+id/textView4"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <androidx.viewpager2.widget.ViewPager2
            android:id="@+id/viewPagerHome"
            android:layout_width="409dp"
            android:layout_height="230dp"
            app:layout_constraintCircleRadius="100dp">

        </androidx.viewpager2.widget.ViewPager2>
    </androidx.cardview.widget.CardView>

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottomNavbar"
        android:layout_width="409dp"
        android:layout_height="73dp"
        android:layout_marginStart="1dp"
        android:layout_marginEnd="1dp"
        android:background="#fff"
        app:itemIconTint="@drawable/bottom_nav_icon_color"
        app:itemTextColor="#00B8B4"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:menu="@menu/bottom_nav" />


</androidx.constraintlayout.widget.ConstraintLayout>

here is the activity's xml file

<?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=".HomeActivity">

    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/fragmentContainerView"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="409dp"
        android:layout_height="867dp"
        android:layout_marginStart="1dp"
        android:layout_marginTop="1dp"
        android:layout_marginEnd="1dp"
        android:layout_marginBottom="1dp"
        app:defaultNavHost="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navGraph="@navigation/nav_home_acitvity" />

</androidx.constraintlayout.widget.ConstraintLayout>

Solution

  • I think the issue lies here:

    android:layout_width="409dp"
    android:layout_height="867dp"
    

    set them as below:

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