Search code examples
androidandroid-preferencesandroid-styles

I want to edit Preference.xml style


I used Preference.xml for setting page.

The file has PreferenceScreen, PreferenceCategory, SwitchPreference and so on. But There is a problem in Preference. When put two SwitchPreferences in PreferenceCategory, the division line which is between SwitchPreferences appears. So I want to change color of the line or remove that.

enter image description here

Many people use style.xml and change value about Preference. But I don't know how to apply style to Preference. Should I use Manifest? Then how?

please tell me detail about that.

------------------UPDATE---------------------

Here is xml code.

<?xml version="1.0" encoding="utf-8"?>

<PreferenceCategory
    android:title="Locker Mode">

    <SwitchPreference
        android:defaultValue="false"
        android:key="toggle_select_mode"
        android:switchTextOff="OFF"
        android:switchTextOn="ON"
        android:summary="Change to free mode when key activated"
        />

</PreferenceCategory>

<PreferenceCategory
    android:title="Notifications">

    <SwitchPreference
        android:defaultValue="false"
        android:key="toggle_soundkey"
        android:switchTextOff="OFF"
        android:switchTextOn="ON"
        android:summary="Play sound when key activated"
        />

    <SwitchPreference
        android:defaultValue="false"
        android:key="toggle_vibratekey"
        android:switchTextOff="OFF"
        android:switchTextOn="ON"
        android:summary="Vibrate when key activated"/>

</PreferenceCategory>
<PreferenceCategory
    android:title="Bluetooth">

    <ListPreference
        android:title="Bluetooth sensitivity"
        android:key="lp_ble_sensitivity"
        android:entries="@array/ble_sensitivity"
        android:entryValues="@array/ble_sensitivity"
        android:dialogTitle="Bluetooth sensitivity"
        />

</PreferenceCategory>

<PreferenceCategory
    android:title="User Interface">

    <SwitchPreference
        android:defaultValue="false"
        android:key="toggle_appstartscan"
        android:switchTextOff="OFF"
        android:switchTextOn="ON"
        android:summary="Start scanning when App started"
        android:autoText="true"
    />

    <SwitchPreference
        android:defaultValue="false"
        android:key="toggle_appfinish"
        android:switchTextOff="OFF"
        android:switchTextOn="ON"
        android:summary="Finish App when key activated"/>

</PreferenceCategory>


Solution

  • 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);
    
    }
    

    You can remove the divider using style

    <style name="PreferenceFragment.Material">
        <item name="android:divider">@null</item>
    </style>
    

    Set the style in your PreferencesActivity

    set style using style="@style/PreferenceFragment.Material"

    Or you can do

       List view.setDivider(null); //as shown above