Place Preference Screen Inside Fragment

1.2k views Asked by At

i want to know if there is any chance i could place my PreferenceScreen xml inside a ConstraintLayout fragment. I want my PreferenceScreen below the "Settings" text

enter image description here

Here is my Fragment xml code:

<?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"
    android:background="@drawable/shape_fragment_background"
    tools:context=".ui.fragments.SettingsFragment">

    <TextView
        android:id="@+id/tv_header"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:fontFamily="@font/montserrat"
        android:gravity="center"
        android:text="@string/settings"
        android:textSize="24sp"
        android:textStyle="bold"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

And here is my PreferenceScreen code:

<androidx.preference.PreferenceScreen xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <PreferenceCategory app:title="@string/settings">

        <Preference
            app:title="@string/language"
            app:summary="@string/language_summary"
            app:icon="@drawable/ic_baseline_language_24">
            <intent android:action="android.settings.LOCALE_SETTINGS" />
        </Preference>

        <SwitchPreferenceCompat
            app:title="@string/reminder"
            app:summaryOn="@string/reminder_summary_on"
            app:summaryOff="@string/reminder_summary_off"
            app:icon="@drawable/ic_baseline_notifications_24"
            app:key="@string/reminder"/>

    </PreferenceCategory>

    <PreferenceCategory android:title="@string/info">

        <Preference
            app:title="@string/about"
            app:icon="@drawable/ic_baseline_info_24" />

    </PreferenceCategory>

</androidx.preference.PreferenceScreen>

Thanks for the help.

1

There are 1 answers

0
Viatcheslav Ehrmann On BEST ANSWER

Make a container fragment that holds the preference xml.

class FragmentContainerSettings : Fragment() {
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    requireActivity().supportFragmentManager
        .beginTransaction()
        .replace(
            R.id.settings,
            SettingsFragment()
        )
        .commit()
}

override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    // Inflate the layout for this fragment
    val view = inflater.inflate(R.layout.fragment_container_settings, container, false)

    return view
    }


}

For the PrefereneceScreen settings xml

class SettingsFragment : PreferenceFragmentCompat() {

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

    }

}

Layout xml of FragmentContainerSettings. I used LinearLayout here, but you can replace it with ConstraintLayout if you want. Basically when you navigate to FragmentContainerSettings with navController.navigate(R.id.fragmentContainerSettings) it will create the text view and replace the FrameLayout with the PreferenceScreen xml

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".ui.settings.FragmentContainerSettings"
    >

    <TextView
    android:id="@+id/tv_header"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="16dp"
    android:layout_marginTop="16dp"
    android:fontFamily="@font/montserrat"
    android:gravity="center"
    android:text="@string/settings"
    android:textSize="24sp"
    android:textStyle="bold"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

    <FrameLayout
    android:id="@+id/settings"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    />
</LinearLayout>