Search code examples
androidandroid-layoutandroid-fragmentskotlintoolbar

Fragment / Toolbar from fragment is shown at the wrong position


I started to use a toolbar instead of an ActionBar. The toolbar is set in each layout. But as you can see in the picture, the fragment with the toolbar does not start directly below the statusbar. Moreover the toolbar crashes with the text from grid layout.

enter image description here

Here is the code of activity_main.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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingTop="?attr/actionBarSize">

        <fragment
            android:id="@+id/nav_host_fragment"
            android:name="androidx.navigation.fragment.NavHostFragment"
            android:layout_width="0dp"
            android:layout_height="0dp"
            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/mobile_navigation" />

        <com.google.android.material.bottomnavigation.BottomNavigationView
            android:id="@+id/nav_view"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:background="?android:attr/windowBackground"
            app:elevation="0.1dp"
            app:itemIconTint="@drawable/nav_item_colors"
            app:layout_constraintBottom_toBottomOf="@+id/nav_host_fragment"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:menu="@menu/bottom_nav_menu" />

    </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

Here is the code of the fragment_finder.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"
    android:layout_width="match_parent"
    xmlns:tools="http://schemas.android.com/tools"
    android:backgroundTint="#ffffff"
    android:layout_height="match_parent">

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/appbar"
        android:layout_height="wrap_content"
        tools:targetApi="21"
        app:elevation="0dp"
        android:layout_width="match_parent">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            app:elevation="0dp"
            android:layout_height="match_parent">

            <TextView
                android:id="@+id/toolbar_title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="Toolbar Title" />

        </androidx.appcompat.widget.Toolbar>

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

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:weightSum="7" >

        <GridLayout
            android:layout_gravity="center_horizontal|center_vertical"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_weight="0.5"
            android:columnCount="3"
            android:rowCount="1"
            android:alignmentMode="alignMargins" >

            <TextView
            android:id="@+id/text_country"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="0dp"
            android:layout_marginTop="0dp"
            android:layout_marginEnd="5dp"
            android:fontFamily="@font/pacifico"
            android:layout_gravity="left"
            android:textSize="20sp" />

            <TextView
                android:id="@+id/text_finder"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="0dp"
                android:layout_marginTop="0dp"
                android:layout_marginEnd="5dp"
                android:fontFamily="@font/pacifico"
                android:textAlignment="center"
                android:textSize="20sp" />

            <TextView
                android:id="@+id/text_age"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="0dp"
                android:layout_marginTop="0dp"
                android:layout_marginEnd="5dp"
                android:fontFamily="@font/pacifico"
                android:layout_gravity="right"
                android:textSize="20sp" />

        </GridLayout>

        <androidx.cardview.widget.CardView
            android:layout_width="match_parent"
            android:layout_weight="5"
            android:layout_height="0dp"
            app:cardCornerRadius="15dp"
            android:layout_margin="5dp"
            android:elevation="10dp" >

            <ImageView
                android:id="@+id/profileview"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:src="@drawable/ex1"
                android:scaleType="centerCrop" />

        </androidx.cardview.widget.CardView>

        <GridLayout
            android:layout_gravity="center_horizontal|center_vertical"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:columnCount="3"
            android:rowCount="1"
            android:alignmentMode="alignMargins" >

            <ImageView
                android:id="@+id/schlecht"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/schlecht"
                android:scaleType="centerCrop" />

            <ImageView
                android:id="@+id/hot"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/hot"
                android:scaleType="centerCrop" />

            <ImageView
                android:id="@+id/gut"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/gut"
                android:scaleType="centerCrop" />

        </GridLayout>

    </LinearLayout>

    </RelativeLayout>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

I hope anybody can help me with this problem.

With kind regards Jeremy


Solution

  • Answer to the first part: Remove below line from activity_main

    android:paddingTop="?attr/actionBarSize"
    

    Add line to Relative layout in fragment_finder

    android:paddingTop="?attr/actionBarSize"