how to hide the divider between preferences in fragment

soildong picture soildong · Dec 17, 2014 · Viewed 7.7k times · Source

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

Answer

rana picture rana · Mar 16, 2016

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