Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add divider lines in between preferences (settings menu)?

In my settingsImage of my settings menu I have multiple preferences that don't have lines in between them, creating an ugly look. How do I fix this?

like image 975
Pike D. Avatar asked Nov 28 '22 00:11

Pike D.


2 Answers

AndroidX

If using AndroidX, to show dividers you can simply add the following attributes in your Preference XML:

<Preference
    ...
    app:allowDividerAbove="true"
    app:allowDividerBelow="true"
    ... />

A more detailed answer here: https://stackoverflow.com/a/55981453/2836371

like image 74
Maksim Ivanov Avatar answered Dec 15 '22 21:12

Maksim Ivanov


The following is for AndroidX:

In AndroidX, getListView() returns a RecyclerView.

Dividing lines can be added to RecyclerViews using .addItemDecoration()

This should be done after the RecyclerView has been inflated in onActivityCreated().

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    RecyclerView recyclerView = getListView();
    DividerItemDecoration itemDecoration = new DividerItemDecoration(context, RecyclerView.VERTICAL);
    recyclerView.addItemDecoration(itemDecoration);
}
like image 41
Matthew Smith Avatar answered Dec 15 '22 21:12

Matthew Smith