You can. In my example I specified custom layout:
preferences_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:text="Custom Layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"/>
<FrameLayout
android:id="@+id/settings_container_view"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
<TextView
android:text="End of Layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"/>
</LinearLayout>
I created class it extends PreferenceFragmentCompat. You must override methods:
onCreatePreferences
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
setPreferencesFromResource(R.xml.app_main_preferences, rootKey);
}
This method sets your preferences. In my Example I use app_main_preferences.xml
app_main_preferences.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:app="http://schemas.android.com/apk/res-auto">
<Preference
app:key="logout"
app:title="Logout"
app:summary="Logout From The Application Account"/>
</PreferenceScreen>
onCreateView
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View layout = inflater.inflate(R.layout.preferences_fragment, container, false);
ViewGroup settingsContainerView = layout.findViewById(R.id.settings_container_view);
Toolbar toolbar = layout.findViewById(R.id.toolbar);
MainActivity mainActivity = (MainActivity) requireActivity();
mainActivity.setSupportActionBar(toolbar);
View settingsView = super.onCreateView(inflater, settingsContainerView, savedInstanceState);
settingsContainerView.addView(settingsView);
return layout;
}
Congratulations!
enter image description here
How it works:
In our custom layout we create FrameLayout it's our container for settings, Toolbar and TextView widgets.
Then we create class it extends PreferenceFragmentCompat class. We override methods. In onCreatePreferences method we set our setting's layout using setPreferencesFromResource.
And our "Main" method is onCreateView method.
View layout = inflater.inflate(R.layout.preferences_fragment, container, false);
We inflate our custom layout using our inflater and it inflate method, we pass our custom layout and other parameters such container and savedInstanceBundle.
ViewGroup settingsContainerView = layout.findViewById(R.id.settings_container_view);
Toolbar toolbar = layout.findViewById(R.id.toolbar);
MainActivity mainActivity = (MainActivity) requireActivity();
We get our FrameLayout (it's container for our settings), toolbar and MainActivity of our application.
mainActivity.setSupportActionBar(toolbar);
We set our toolbar using setSupportActionBar method.
View settingsView = super.onCreateView(inflater, settingsContainerView, savedInstanceState);
settingsContainerView.addView(settingsView);
return layout;
Finally we call super.onCreateView method which populate settings and returns populated view. We must add our populated view as child to our settingsContainerView using addView method and return layout variable.
onCreateRecyclerView(...)
which should return the RecyclerView from your layout. – CesariaPreferenceFragmentCompat
is for the standard layout, it does nothing special behind the scenes so it shouldn't be a problem implementing your custom settings UI with custom business logic using the classicSharedPreferences
. – CesariaPreferenceFragment
with aListView
andPreferenceFragmentCompat
with aRecyclerView
. Both fill the given layouts with the preference layouts loaded according to the XML supplied as your preference list. If you just need custom list items, you can customize the individual layouts by overriding the style elements inside the PreferenceThemeOverlay (use CTRL + click in your IDE or check out the sources on the Internet to explore possible values). – Cesaria