Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: How to remove margin/padding in Preference Screen

I am facing very weird problem in designing preference screen. Though I am not giving any margin in layout,it is leaving some space in left.

As you can see in image below: enter image description here

XML:

   <PreferenceScreen android:title="demo" >        <CheckBoxPreference            android:defaultValue="false"             android:key="prefSync"`             android:title="Auto Sync" />     </PreferenceScreen> 

Am I doing something wrong in adding check-box preference in screen?

like image 982
Dory Avatar asked Aug 29 '13 11:08

Dory


People also ask

What is padding and margin in Android?

Note that padding goes completely around the content: there is padding on the top, bottom, right and left sides (which can be independent). Margins are the spaces outside the border, between the border and the other elements next to this view. In the image, the margin is the grey area outside the entire object.

What is margin top Android?

android:layout_marginTopSpecifies extra space on the top side of this view. This space is outside this view's bounds. Margin values should be positive. May be a dimension value, which is a floating point number appended with a unit such as " 14.5sp ".

What is the difference between margin and padding in Android layout * 1 point?

Margin specifies the space left on four sides in the layout and padding specifies the exact position where the element going to be taking place in the layout.

What is padding in Android Studio?

Padding can be used to offset the content of the view by a specific number of pixels. For instance, a left padding of 2 will push the view's content by 2 pixels to the right of the left edge.


1 Answers

Updating this for androidx.

After a lot of experimentation, I resolved this issue by adding this to each preference that had the excess indentation:

app:iconSpaceReserved="false" 

Of course, you'll also need to add this to the PreferenceScreen declaration itself at the top of your xml:

xmlns:app="http://schemas.android.com/apk/res-auto" 

Follow Up For Custom Preferences

I noticed that in the case of custom preferences, particularly on older devices, this solution wasn't always working. For example, a preference like this could still be indented:

com.example.preference.SomeCustomPreference     android:id="@+id/some_custom_preference"     android:name="Custom Preference"     android:key="@string/custom_pref_key"     android:summary="@string/custom_pref_summary"     android:title="@string/preference_custom_title"     app:iconSpaceReserved="false" 

The problem traces to the usage of the third constructor parameter in the custom preference class. If you pass that third parameter, the list item will be indented. If you omit it, the list will be aligned correctly:

class SomeCustomPreference @JvmOverloads constructor(     context: Context,     attrs: AttributeSet? = null,     defStyle: Int = android.R.attr.someCustomPreferenceStyle ) : DialogPreference(context, attrs, defStyle) {      override fun getDialogLayoutResource(): Int {         return R.layout.my_layout     } } 

Instead of that, use this:

class SomeCustomPreference @JvmOverloads constructor(     context: Context,     attrs: AttributeSet? = null ) : DialogPreference(context, attrs) {      override fun getDialogLayoutResource(): Int {         return R.layout.my_layout     } } 

Credit for this goes to @CommonsWare in this very old post.

like image 162
AndroidDev Avatar answered Sep 20 '22 13:09

AndroidDev