PreferenceCompatFragment - Layout messed up when edit text dialog displayed

22 hours ago 3
ARTICLE AD BOX

While testing a preferences fragment built with PreferenceFragmentCompat, I've noticed that the appearance of the preference list is messed up, seemingly at random, whenever an edit text dialog is displayed. If I rotate the screen (using default behavior for config changes with this activity) or switch from another fragment then back to this one, the correct appearance is restored. I manage a change listener in onResume/onPause, but removing that doesn't change anything, so I know that's not related.

I have tried the takisoft fix library as well, assuming that's still relevant. While it might fix other issues, it does nothing for this one.

Any ideas? I've looked for a fix, using every search term I can think of, but finding nothing.


Edit: One MRE, coming right up...

import android.os.Bundle; import androidx.preference.PreferenceFragmentCompat; public final class PreferencesFragment extends PreferenceFragmentCompat { @Override public void onCreatePreferences(Bundle icicle, String rootKey) { setPreferencesFromResource(R.xml.prefs, rootKey); } } import android.os.Bundle; import androidx.activity.EdgeToEdge; import androidx.appcompat.app.AppCompatActivity; public final class Prefs extends AppCompatActivity { @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); EdgeToEdge.enable(this); setContentView(R.layout.app_prefs); getSupportFragmentManager() .beginTransaction() .replace(R.id.fragment_container, new PreferencesFragment()) .commit(); } } <?xml version="1.0" encoding="utf-8"?> <androidx.fragment.app.FragmentContainerView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/fragment_container" /> <?xml version="1.0" encoding="utf-8"?> <androidx.preference.PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"> <androidx.preference.PreferenceCategory android:title="Category"> <androidx.preference.EditTextPreference android:key="pref1" app:useSimpleSummaryProvider="true" android:title="Preference 1" /> <androidx.preference.EditTextPreference android:key="pref2" app:useSimpleSummaryProvider="true" android:title="Preference 2" /> <androidx.preference.EditTextPreference android:key="pref3" app:useSimpleSummaryProvider="true" android:title="Preference 3" /> </androidx.preference.PreferenceCategory> </androidx.preference.PreferenceScreen> <resources xmlns:tools="http://schemas.android.com/tools"> <!-- Base application theme. --> <style name="Theme.PrefTest" parent="Theme.AppCompat.DayNight.DarkActionBar"> <!-- Primary brand color. --> <item name="colorPrimary">@color/purple_500</item> <item name="colorPrimaryVariant">@color/purple_700</item> <item name="colorOnPrimary">@color/white</item> <!-- Secondary brand color. --> <item name="colorSecondary">@color/teal_200</item> <item name="colorSecondaryVariant">@color/teal_700</item> <item name="colorOnSecondary">@color/black</item> <!-- Status bar color. --> <item name="android:statusBarColor">?attr/colorPrimaryVariant</item> <!-- Customize your theme here. --> <item name="android:fitsSystemWindows">true</item> </style> </resources>

As for libraries and versions:

androidx.appcompat - 1.7.1 com.google.android.material - 1.13.0 androidx.preference - 1.2.1

(Didn't intend for that list to be in a code block, but the editor wouldn't take it otherwise.)

I did notice something interesting when I was trying to replicate the issue, in that initially I was having trouble with the action bar covering the preference category name. At the time, appcompat was at 1.6.1 and material was at 1.10.0. When I updated either of them to what Android Studio suggested was current (see above), the action bar issue went away, but the preference layout bug started.

When I tried the Takisoft library, it was 1.1.0.

Read Entire Article