Skip to content Skip to sidebar Skip to footer

Extra Padding/margin Added To DatePicker On Android 7.1.1

I'm trying to make a custom dialog displaying the DatePicker, but the element adds an extra margin/padding to the right of the view. This happens only on Android 7.1.1, Nexus 6P, b

Solution 1:

Add Style with AlertDialog.Builder like as follow.

AlertDialog.Builder builder = new AlertDialog.Builder(UserAttributesActivity.this, android.R.style.Theme_DeviceDefault_Light_Dialog_NoActionBar);

Solution 2:

@Ankita is right, but if you are using AppCompat things are a bit more involved:

Define a new style and extend AppCompat's Light.Dialog style

<style name="DatePickerDialogStyle" parent="@style/Theme.AppCompat.Light.Dialog">
    ...
</style>

Then add attributes to mimic the NoActionBar part:

<style name="DatePickerDialogStyle" parent="@style/Theme.AppCompat.Light.Dialog">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>

Lastly, now that you are no longer getting app compat colors from your AppTheme, copy them over to your new theme:

<style name="AppTheme" parent="Theme.AppCompat.Light">
    <item name="colorPrimary">@color/primary</item>
    <item name="colorPrimaryDark">@color/primary_dark</item>
    <item name="colorAccent">@color/accent</item>
</style>

<style name="DatePickerDialogStyle" parent="@style/Theme.AppCompat.Light.Dialog">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
    <item name="colorPrimary">@color/primary<item>
    <item name="colorPrimaryDark">@color/primary_darkitem>
    <item name="colorAccent">@color/accent</item>
</style>

Then apply this new theme:

AlertDialog.Builder builder 
    = new AlertDialog.Builder(UserAttributesActivity.this,
    DatePickerDialogStyle);

Solution 3:

Use this

DatePickerDialog.Builder builder = null;
// 5.0+ 需要更换主题以去除右侧白边
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    builder = new DatePickerDialog.Builder(this.getActivity(),
            android.R.style.Theme_DeviceDefault_Light_Dialog_NoActionBar);
}
// 4.4 不需要,相反加上会有白边
else {
    builder = new DatePickerDialog.Builder(this.getActivity());
}

Solution 4:

If you are using a class like androidx.fragment.app.DialogFragment(). Do this:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    // Set the dialog style.
    setStyle(STYLE_NO_TITLE, android.R.style.Theme_DeviceDefault_Light_Dialog_NoActionBar)
}

Post a Comment for "Extra Padding/margin Added To DatePicker On Android 7.1.1"