8

If you set the selectability of a preference item to false, you will notice that the dividers around that item will disappear.

Do you know if there is a way to keep those dividers?

I have looked at the ListView API and could not find a solution that could be applied here, since there is no selectable attribute for ListView items (except for the headers and footers).

Thanks!

Community
  • 1
  • 1
Amokrane Chentir
  • 29,907
  • 37
  • 114
  • 158

5 Answers5

1

I did it with a hack.

<Preference android:layout="@layout/preference_divider" />
<Preference android:title="@string/whatever" android:selectable="false" />

res/layout/preference_divider.xml

<?xml version="1.0" encoding="utf-8"?>
<View xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="#cccccc"
    android:layout_width="fill_parent"
    android:layout_height="1px" />
Dave Aaron Smith
  • 4,517
  • 32
  • 37
  • Why did you change it to 1px? 0px works fine for me, just wondering if there may be problems with 0px on some devices or Android versions? – tomrozb Dec 11 '14 at 21:26
0

some example code would help! did you set your dividers in the ListView element? if yes check this post as well.

<ListView
    //...
    android:divider="@color/black"
    />

Otherwise you could create a custom ListElementView and add your dividers in the layout xml.

Community
  • 1
  • 1
longi
  • 11,104
  • 10
  • 55
  • 89
  • 1
    I am not saying that my list of preferences item doesn't have dividers at all. I am saying, that the items which are made *non selectable* end up with no dividers around them. There is nothing special about the code, since it works as the framework intended it. I am just looking for a way to customize this default experience. Thanks for the help! – Amokrane Chentir Aug 26 '13 at 15:01
0

Here is a "trick" that worked for me. Simply set android:focusable="true" to any of the items inside the preference. This will make the whole preference line not clickable.

StefanMK
  • 1,313
  • 1
  • 12
  • 22
0

If you want to use the preference to show some text you can also use an uneditable EditTextPreference. The preference will still be selectable but only shows its content when selected.

<EditTextPreference
    android:editable="false"
    ... />
0

The line you want to hide is called 'divider'. PreferenceActivity extends ListActivity and as you may expect it uses ListView. You can change a divider in a ListView. In your PreferenceActivity:

        ListView lv = getListView();
        lv.setDivider(new ColorDrawable(Color.TRANSPARENT));
        lv.setDividerHeight(0);
Ahmer Afzal
  • 501
  • 2
  • 11
  • 24