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:
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?
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.
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 ".
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With