Why are the preferences in my Android PreferenceFragment indented?

1.1k views Asked by At

When I create my PreferenceFragment, all of the preferences in the preference category are indented.

When I do this in a new project with default settings (just an Activity and PreferenceFragment), the preferences appear just fine (no indentation).

public class SettingsFragment extends PreferenceFragment {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.fragment_settings);
    }

}

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

    <PreferenceCategory android:title="Game Setup">

        <CheckBoxPreference
            android:key="checkbox_randomize_opponents"
            android:title="Randomize Opponents"
            android:summary="Randomize opponents after round 1" />

    </PreferenceCategory>
</PreferenceScreen>
1

There are 1 answers

2
ataulm On BEST ANSWER

By default, preferences have no icons. Icons are drawables, and when you specify one using the android:icon attribute, the preference fragment will display what ever drawable you have set.

android:color/transparent is a drawable. Setting this as the android:icon, in addition with the fact that preference fragment has a fixed size for icons, will tell the fragment to show the icon. In this case, it's a transparent square of fixed dimensions.

Try changing it to @android:color/black and instead of the "indent", you should see a black square with fixed dimensions.

If you want no icon, you can either set the icon attribute to android:icon="@null" or just remove the attribute entirely (default is no icon).