0

I have this fragment:

public class SettingsFragment extends PreferenceFragmentCompat {
    @Override
    public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
        setPreferencesFromResource(R.xml.settings, rootKey);
    }
}

And I want to add this Fragment to a .XML file. This is what I have tried:

<layout xmlns:android="http://schemas.android.com/apk/res/android">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <androidx.appcompat.widget.Toolbar
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:id="@+id/toolbar"/>

        <com.example.myapp.SettingsFragment
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/toolbar"/>
    </RelativeLayout>
</layout>

I can confirm that this is the correct location of the file:

com.example.myapp.SettingsFragment

However, this is not inflated, due this error:

android.view.InflateException: Binary XML file line #14 in com.example.myapp:layout/fragment_settings: Class is not a View com.example.myapp.SettingsFragment

That is pointing to:

<com.example.myapp.SettingsFragment

How to inflate this custom Fragment into my XML file?

The XML file is the layout of a Fragment and not of an activity.

Pathis Patel
  • 179
  • 1
  • 11
  • Sorry, it seems can't be inflated. https://stackoverflow.com/a/23693251/3501958 *Nested fragments are only supported when added to a fragment dynamically.* So you can't do that statically by layout.xml – hata Apr 28 '20 at 13:46
  • @hata Thanks hata. – Pathis Patel Apr 28 '20 at 14:08

0 Answers0