'PreferenceScreen always shows with additional space to top

Currently, I'm facing a problem regarding the display of the preference fragment. I'm using a bottom navigation menu and I want to show a toolbar (previously action bar) showing the currently selected menu item of the navigation bar.

The problem is that there seems to be a second invisible toolbar between the wanted toolbar and the top of the preference fragment as seen in the image.

Top space in preference fragment visible by swiping down

The top of the preference screen:

<PreferenceScreen
    xmlns:android="http://schemas.android.com/apk/res/android">

    <PreferenceCategory android:title="@string/preferences_category_online_area">
        <EditTextPreference
            android:key="preference_username"
            android:inputType="text"
            android:persistent="true"
            android:selectAllOnFocus="true"
            android:singleLine="true"
            android:summary="@string/edit_text_preferences_username_summary"
            android:title="@string/edit_text_preferences_username_text" />
        <EditTextPreference
            android:key="preference_password"
            android:inputType="textPassword"
            android:persistent="true"
            android:selectAllOnFocus="true"
            android:singleLine="true"
            android:summary="@string/edit_text_preferences_passwort_summary"
            android:title="@string/edit_text_preferences_passwort_text" />
...

The mobile_navigation.xml:

<?xml version="1.0" encoding="utf-8"?>
<navigation 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/mobile_navigation"
    app:startDestination="@+id/navigation_library">

    <fragment
        android:id="@+id/navigation_dummy"
        android:name="de.example.OnlineSelectionFragment"
        android:label="@string/title_dummy"
        tools:layout="@layout/fragment_dummy" />

    <fragment
        android:id="@+id/navigation_preferences"
        android:name="de.example.PrefsFragment"
        android:label="@string/title_preferences" />
</navigation>

Class PrefsFragment.kt in which the fragment is populated with the preferences

package de.example

import android.os.Bundle
import androidx.preference.PreferenceFragmentCompat


class PrefsFragment : PreferenceFragmentCompat() {

    override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
        setPreferencesFromResource(R.xml.preferences, null)
    }
}

How can I remove the empty top space? Previously, the PrefsFragment has been shown within an extra activity but without it I cannot format it in XML.

Thank you very much!

Edit 1 As requested the main activity xml file:

<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>


Solution 1:[1]

Problem has been solved.

The reason for the behaviour was the android:paddingTop="?attr/actionBarSize" in the main activity xml file.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 who9vy