0

I followed this guide https://developer.android.com/guide/topics/ui/settings to create my preferences page, but when I navigate to my settings page, I get an empty row in my settings fragment just before the preferencescreen items, like in the picture https://i.stack.imgur.com/sTcJ0.jpg

This is my code;

class SettingsActivity  : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        supportFragmentManager
            .beginTransaction()
            .replace(R.id.settings_container, SettingsFragment())
            .commit()
    }
}
class SettingsFragment : PreferenceFragmentCompat() {
    override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
        addPreferencesFromResource(R.xml.preferences)
    }

}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/settings_container"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ui.actionbar.settings.SettingsActivity">

</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<androidx.preference.PreferenceScreen
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <PreferenceCategory
        app:iconSpaceReserved="false"
        app:title="Theme">

        <SwitchPreferenceCompat
            app:iconSpaceReserved="false"
            app:key="switch"
            app:summary="Enable dark theme."
            app:title="Dark Theme" />
    </PreferenceCategory>

</androidx.preference.PreferenceScreen>
AlanJ
  • 11
  • 5
  • Is everything displaying as you'd expect without `setContentView(...)` in `SettingsActivity`? `setContentView(R.layout.settings_activity)` – MFazio23 Nov 14 '19 at 04:53
  • @MFazio23 Everything is displaying, except for the gap on top, that's the only thing that is wrong – AlanJ Nov 14 '19 at 16:21

0 Answers0