Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extra padding/margin added to DatePicker on Android 7.1.1

Tags:

android

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, but works perfectly fine on other lower resolution phones. How do I remove the extra padding? My XML layout code:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <DatePicker
        android:id="@+id/date_picker"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:endYear="2012"
        android:startYear="1930" />

</LinearLayout>

It looks like this on Android 7.1.1: Problem Screenshot

Edit: Added my Java code:

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_user_attributes);

    assert getSupportActionBar() != null;
    getSupportActionBar().setTitle("Register");

    final TextView birthDateSelectedTextView = (TextView) findViewById(R.id.text_view_birth_date);
    Button dialogOpenButton = (Button) findViewById(R.id.button_dialog_date_picker);
    dialogOpenButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final Dialog dialog = new Dialog(UserAttributesActivity.this);
            final AlertDialog.Builder builder = new AlertDialog.Builder(UserAttributesActivity.this);
            LayoutInflater inflater = getLayoutInflater();
            View dialogView = inflater.inflate(R.layout.dialog_date_picker, null);
            builder.setView(dialogView);
            final DatePicker datePicker = (DatePicker) dialogView.findViewById(R.id.date_picker);

            builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    day = datePicker.getDayOfMonth();
                    month = datePicker.getMonth() + 1;
                    year = datePicker.getYear();
                    String date = "" + day + "/" + month + "/" + year;
                    birthDateSelectedTextView.setText(date);
                }
            });
            builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                }
            });
            builder.show();
        }
    });
}
like image 592
Dhruvil Mehta Avatar asked Jan 04 '17 04:01

Dhruvil Mehta


1 Answers

Add Style with AlertDialog.Builder like as follow.

AlertDialog.Builder builder = new AlertDialog.Builder(UserAttributesActivity.this, android.R.style.Theme_DeviceDefault_Light_Dialog_NoActionBar);
like image 163
Ankita Shah Avatar answered Oct 25 '22 20:10

Ankita Shah