Get rid of extra padding above in PreferenceFragment

943 views Asked by At

I am trying to build a preference screen for my app. I have created a class called SettingsFragment which extends from PreferenceFragment. I am not loading preference items from xml because I need to load some data dynamically. That's why I am building everything programmatically. Everything is showing as expected. But there is some extra padding at the top. See the image below:

Some white space above the item "Choose Language"

I am not sure why it's happening. Here is the code below I wrote in the SettingsFragment class:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    PreferenceScreen root = getPreferenceManager().createPreferenceScreen(getActivity());
    root.setTitle("Hello World");

    PreferenceCategory cat1 = new PreferenceCategory(root.getContext());
    root.addPreference(cat1);

    ListPreference lang = new ListPreference(root.getContext());
    lang.setEntries(new CharSequence[]{"English", "Bangla"});
    lang.setEntryValues(new CharSequence[]{"en", "bn"});
    lang.setTitle("Choose Language");
    lang.setSummary("English");
    cat1.addPreference(lang);

    CheckBoxPreference check = new CheckBoxPreference(root.getContext());
    check.setTitle("Title Check");
    cat1.addPreference(check);

    setPreferenceScreen(root);
}

FYI, I am using NavigationDrawer in my app. This SettingsFragment loads when a navigation item is clicked from the drawer.

3

There are 3 answers

0
saurabh1489 On BEST ANSWER

You are trying to add a PreferenceCategory with no title, which is causing the empty space.

The following line is the problem:

PreferenceCategory cat1 = new PreferenceCategory(root.getContext());
root.addPreference(cat1);

So you can add title to your preference category like this :

PreferenceCategory cat1 = new PreferenceCategory(root.getContext());
cat1.setTitle("Category");
root.addPreference(cat1);

If you don't want a preference category, you can modify your code like below:

    @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            PreferenceScreen root = getPreferenceManager().createPreferenceScreen(getActivity());
            root.setTitle("Hello World");

            ListPreference lang = new ListPreference(root.getContext());
            lang.setEntries(new CharSequence[] { "English", "Bangla" });
            lang.setEntryValues(new CharSequence[] { "en", "bn" });
            lang.setTitle("Choose Language");
            lang.setSummary("English");
            root.addPreference(lang);

            CheckBoxPreference check = new CheckBoxPreference(root.getContext());
            check.setTitle("Title Check");
            root.addPreference(check);
            setPreferenceScreen(root);
        }
2
reidzeibel On

Check your layout file for the Fragment and its parent Activity, usually when you create a new project or layout/screen, there will be an automatic padding provided on your android layout file.

1
AAnkit On

Go into your layout of Fragment. You will see below tags in Main/Parent Layout.

like this android:paddingBottom or android:paddingTop remove these.

Alternatively if you want to remove this padding from every fragments/activitys/layouts. go to the dimen.xml and make activity_vertical_margin as 0. It gets default value as 16dp.