I started dealing with preferences in a PreferenceFragment
. Here's what I have:
I'm trying to:
get rid of the dividers between items. I suppose this can be defined from styles, but I can't figure out how. I tried getting the
preference ListView
at runtime calling
findViewById(android.R.id.list)
, as I read somewhere, but it
returns null.
set new, full width dividers right on top of the headers, as seen here. For example in this case I want a full width divider right above "Statistiche", but not above "Generali" which is on top of the list.
The only way that comes to my mind is setting dividers as fake preferences, like with:
<Preference
android:layout="@layout/divider" //here I set width and a divider resource
/>
<PreferenceCategory ... />
The main issue here is that my PreferenceFragment
(or the ActionBarActivity
it's in) has some left/right padding, that make any divider I add into preferences.xml not cover the entire width.
So my question are:
How can I get rid of default, item-item dividers that you can see in the image?
How can I set full width dividers right above headers, or how can I get rid of internal fragment/activity padding? Of course my activity layout has no (explicit) padding whatsoever.
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"/>