I started dealing with preferences in a PreferenceFragment
. Here\'s what I have:
You can re-style your divider using this theme.
<style name="PreferenceFragment.Material">
<item name="android:divider">@drawable/preference_list_divider_material</item>
</style>
Although a bit late I`m having same troubles with dividers in preff screen and found this solution: Set custom style to the hosting activity and add to the style:
<item name="android:listDivider">@null</item>
It actually does the same as setting it trough code but you save one findById and i think it looks clearer set trough styles
For PreferenceFragmentCompat, ListView doesn't work. But this works like a charm:
<style name="PrefsTheme" parent="PreferenceThemeOverlay.v14.Material">
<item name="android:divider">@null</item>
<item name="android:dividerHeight">0dp</item>
</style>
Add this to your app theme:
<item name="preferenceTheme">@style/PrefsTheme</item>
Had totally forgot about this question, will post an answer now to help others. I solved by moving my code in the onResume()
method of the activity that hosts my PreferenceFragment
. I think there are several other points at which you can recall a non-null ListView
using findViewById(android.R.id.list)
.
public boolean mListStyled;
@Override
public void onResume() {
super.onResume();
if (!mListStyled) {
View rootView = getView();
if (rootView != null) {
ListView list = (ListView) rootView.findViewById(android.R.id.list);
list.setPadding(0, 0, 0, 0);
list.setDivider(null);
//any other styling call
mListStyled = true;
}
}
}
You can probably get rid of the rootView
check, but at the same time you might even want to check for list != null
. I didn't face any NPE this way anyway.
So, setDivider(null)
takes off the item-item dividers. I managed to add section dividers covering the full width of the screen by:
list
;n
<Preference
android:title="divider"
android:selectable="false"
android:layout="@layout/preference_divider"/>
AndroidX makes it simple, but I wish it was better documented.
In XML
To add/remove dividers between preferences in XML, use the following attributes:
<androidx.preference.PreferenceScreen
xmlns:app="http://schemas.android.com/apk/res-auto">
<Preference
...
app:allowDividerAbove="true/false"
app:allowDividerBelow="true/false"
... />
</androidx.preference.PreferenceScreen>
Note, a divider will only be shown between two preferences if the top divider has allowDividerBelow
set to true
and the bottom divider has allowDividerAbove
set to true
.
In Code
You can also change/remove dividers programmatically using the following methods in onActivityCreated
of your PreferenceFragmentCompat
:
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// To remove:
setDivider(null);
// To change:
setDivider(ContextCompat.getDrawable(getActivity(), R.drawable.your_drawable));
setDividerHeight(your_height);
}
Add this code under the PreferenceFragment
:
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// remove dividers
View rootView = getView();
ListView list = (ListView) rootView.findViewById(android.R.id.list);
list.setDivider(null);
}