14

Before I asked to remove the padding of the PreferenceActivity/PreferenceFragment:

Android: How to maximize PreferenceFragment width (or get rid of margin)?

This time I having trouble adjusting margin/padding before Title Text. (See image below).

enter image description here

Does anybody have any idea?

Community
  • 1
  • 1
jclova
  • 5,466
  • 16
  • 52
  • 78
  • hi @jclova, I am facing with same issue,and I don't want to go through custom layout for check boxes. How did you got rid of this problem. – Dory Aug 29 '13 at 11:01
  • I suspect, what you see as the left-side padding is an "empty" `android:icon`. You should possibly provide your own drawable with as little dimensions as you wish and see if it helps. – Stan Nov 02 '13 at 20:38
  • Similar question answered here: https://stackoverflow.com/questions/18509369/android-how-to-get-remove-margin-padding-in-preference-screen – Vivek Nov 06 '18 at 09:19

5 Answers5

13

You could customize you checkbox like this: MyPreference.xml

<CheckBoxPreference android:key="testcheckbox" 
    android:title="Checkbox Title" 
    android:summary="Checkbox Summary..." 
    android:layout="@layout/custom_checkbox_preference_layout">
</CheckBoxPreference>

and custom_checkbox_preference_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?android:attr/listPreferredItemHeight"
    android:gravity="center_vertical"
    android:paddingLeft="16dip"
    android:paddingRight="?android:attr/scrollbarSize">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:minWidth="0dp"
        android:gravity="center"
        android:orientation="horizontal">
        <ImageView
            android:id="@+android:id/icon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            />
    </LinearLayout>

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="6dip"
        android:layout_marginTop="6dip"
        android:layout_marginBottom="6dip"
        android:layout_weight="1">

        <TextView android:id="@+android:id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:singleLine="true"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:ellipsize="marquee"
            android:fadingEdge="horizontal" />

        <TextView android:id="@+android:id/summary"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@android:id/title"
            android:layout_alignLeft="@android:id/title"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="?android:attr/textColorSecondary"
            android:maxLines="4" />

    </RelativeLayout>

    <!-- Preference should place its actual preference widget here. -->
    <LinearLayout android:id="@+android:id/widget_frame"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="vertical" />

</LinearLayout>

the point is android:minWidth="0dp".

user1482130
  • 176
  • 2
  • 4
  • hello sir i am using custom layout for CheckBoxPreference. but my problem is linear layout with id "@+android:id/widget_frame" is having some padding in its left and right. how to remove it.Thanks in Advance. – Hradesh Kumar Apr 09 '14 at 12:53
11

For anyone facing this issue and don't want to go through custom layout. I'd like to note that adding:

android:icon="@null"

solved my problem with the left-side empty space.

<PreferenceScreen android:icon="@null" android:title="Your title" android:summary="Your summary" />
5

As of 16 Feb 2020, This space is meant for icon. To get rid of this space use:

JAVA

    preference.setIconSpaceReserved(false);

XML

<androidx.preference.PreferenceScreen
xmlns:app="http://schemas.android.com/apk/res-auto">

<Preference
    ...
    app:iconSpaceReserved="true/false"
    .../>

</androidx.preference.PreferenceScreen>

True adds the padding. False removes it.

PS: Dependency used

implementation 'androidx.preference:preference:1.1.0'
Ishaan Kumar
  • 968
  • 1
  • 11
  • 24
0

The custom_checkbox_preference_layout.xml described by user1482130 work also without modifications for other types of preferences such as listpreferences ! Very usefull

android:layout="@layout/custom_checkbox_preference_layout">
André DLC
  • 261
  • 4
  • 11
0

If you are creating preferences dynamically pass ContextThemeWrapper as a parameter to the new Preference

TypedValue themeTypedValue = new TypedValue();
getActivity().getTheme().resolveAttribute(R.attr.preferenceTheme, themeTypedValue, true);
ContextThemeWrapper contextThemeWrapper = new ContextThemeWrapper(getActivity(), themeTypedValue.resourceId);
Preference preference = new Preference(contextThemeWrapper);

I have found it in this article: Dynamically creating Preference Screens on Android

KLM
  • 69
  • 1
  • 4