Skip to content Skip to sidebar Skip to footer

How To Hide The Softketboard When Datepickerdialog Is Open On The Screen

I am customizing DatePickerDialog to add some of my functionality for native DatePicker. that is working as expected. But day, month and year fields of DatepickerDialog are editabl

Solution 1:

This is the best option which stops manual editing in datepickerdialog. You can disable the softkeyboard using this.

         final DatePickerDialog dp = new DatePickerDialog(MainActivity.this, new    DatePickerDialog.OnDateSetListener() {

                @Override
                publicvoidonDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {

                    Calendar newDate = Calendar.getInstance();
                    newDate.set(year, monthOfYear, dayOfMonth);
                    txtdate.setText(dateFormatter.format(newDate.getTime()));

                }
            }, newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));

            dp.show();
            dp.getDatePicker().setDescendantFocusability(DatePicker.FOCUS_BLOCK_DESCENDANTS);

Solution 2:

Here's the way I've handled force closing the soft keyboard

@Override
public void dismiss() {
    super.dismiss();
    //hide the soft keyboardgetActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
}

Solution 3:

Best option for stopping manual editing in DatePickerDialog items is using set the descendantFocusability . You can add it in code or in your dialog style file. Personally I recommended to use it in style.

Method 1 : Using Code

datePickerDialog.datePicker.setDescendantFocusability(DatePicker.FOCUS_BLOCK_DESCENDANTS)

Method 2 : Using Style

<style name="MyDatePickerDialogTheme" parent="android:Theme.Material.Light.Dialog"> <item name="android:descendantFocusability">blocksDescendants</item </style>

Now set the style in dialog

datePickerDialog = DatePickerDialog(
                    this.context,
                    R.style.MyDatePickerDialogTheme
                )

I think you get the idea. Happy codig.

Post a Comment for "How To Hide The Softketboard When Datepickerdialog Is Open On The Screen"