how to hide the divider between preferences in fragment
Asked Answered
T

5

15

I want to hide the divider between preferences in fragment. The code is below:

1.SettingsActivity.java

public class SettingsActivity extends PreferenceActivity {
    super.onCreate(savedInstanceState);
    SettingsFragment settingsFragement = new SettingsFragment();
    FragmentTransaction transaction = getFragmentManager().beginTransaction();
    transaction.add(android.R.id.content, settingsFragement, "settings");
    transaction.commitAllowingStateLoss();
}

2.SettingsFragment.java

public class SettingsFragment extends PreferenceFragment{
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.settings);
    }
}

3.settings.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    <PreferenceCategory>
        <Preference 
             android:key="preference_a"
             android:title="preferenceA"/>
        <Preference 
             android:key="preference_b"
             android:title="preferenceB"/>
        <ListPreference 
             android:key="list_preference_c"
             android:title="list_preferenceC"/>
    </PreferenceCategory>
</PreferenceScreen>

There are system default dividers amoung the preferences. I want to hide the dividers.

How to hide the dividers? Thanks a lot.


Thanks for everyone`s answer. But,the question is that I cannot get the listview from the activity and fragment.

ListView list = (ListView) findViewById(android.R.id.list);
Trichloroethylene answered 17/12, 2014 at 3:59 Comment(3)
try as lv.setDivider(new ColorDrawable(Color.WHITE));Schreibman
sorry,there is no listView.Trichloroethylene
as answered by @rana, extend your fregment from PreferenceFragmentCompat and change the underlying recyclerview.Overelaborate
F
19

if you are using PreferenceFragmentCompat , it is using Recycle view so you have to use this code . This automatically hides the divider

@Nullable
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {

   setDivider(new ColorDrawable(Color.TRANSPARENT));
   setDividerHeight(0);
}
Floorboard answered 16/3, 2016 at 0:52 Comment(1)
Just put this in onViewCreated and it works just fine. This is the best solution.Consumption
P
10

These solutions didn't work for me. Here's what I did in PreferenceFragmentCompat:

@Override
public void setDivider(Drawable divider) {
    super.setDivider(new ColorDrawable(Color.TRANSPARENT));
}

@Override
public void setDividerHeight(int height) {
    super.setDividerHeight(0);
}
Portent answered 29/3, 2016 at 19:22 Comment(0)
A
1

Define custom preference theme without dividers in your style.xml:

<style name="PrefsTheme" parent="PreferenceThemeOverlay.v14.Material">
    <item name="android:divider">@null</item>
    <item name="android:dividerHeight">0dp</item>
</style>

and don't forget to use it in your main app theme which is also in style.xml:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- ... -->
    <item name="preferenceTheme">@style/PrefsTheme</item>
</style>
Amin answered 10/8, 2019 at 9:16 Comment(0)
B
0

You can remove your divider by style.

<style name="PreferenceFragment.Material">
    <item name="android:divider">@null</item>
</style>
Boone answered 12/2, 2018 at 1:52 Comment(0)
O
-1

First: if used the listview in xml

set the android:dividerHeight="0dp" in listview in xml file ,

second:

ListView list = (ListView) findViewById(android.R.id.list);
list.setDivider(new ColorDrawable(Color.TRANSPARENT)); // or some other color int
list.setDividerHeight((int) getResources().getDisplayMetrics().density); 
Ops answered 17/12, 2014 at 4:4 Comment(4)
sorry,I tried.but there is not android:dividerHeight in Preference and ListPreference.Trichloroethylene
This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post.Quadrennial
try this below link ,#17984127Ops
@Igor , initially the questions coding having the listview contents, so that i recommended,try to implement above my code in xml,Ops

© 2022 - 2024 — McMap. All rights reserved.