-1

I would like to disable the EditText behaviour on DatePickerDialog when the theme is set to theme_traditional

enter image description here

public class M3DatePickerDialog extends DialogFragment {

    private DatePicker mDatePicker;
    private OnDatePickerInteractionListener mListener;

    public static M3DatePickerDialog newInstance() {
        return new M3DatePickerDialog();
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.dialog_m3_date_picker, container, false);

        mDatePicker = (DatePicker) rootView.findViewById(R.id.datePicker);
        mDatePicker.setDescendantFocusability(android.widget.DatePicker.FOCUS_BLOCK_DESCENDANTS);
        mDatePicker.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                return true;
            }
        });

        Button setDate = (Button) rootView.findViewById(R.id.datePickerSet);
        setDate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null) {
                    mListener.onDateChanged(mDatePicker.getYear(), mDatePicker.getMonth() + 1, mDatePicker.getDayOfMonth());
                }
                getDialog().dismiss();
            }
        });

        return rootView;
    }

    @Override
    public void onResume() {
        super.onResume();
        if (this.getDialog().getWindow() != null) {
            this.getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mListener = null;
    }

    public void registerListener(OnDatePickerInteractionListener onDatePickerInteractionListener) {
        mListener = onDatePickerInteractionListener;
    }

    public interface OnDatePickerInteractionListener {
        void onDateChanged(int year, int monthOfYear, int dayOfMonth);
    }
}

1 Answers1

0

Well i have made an demo with AlertDialog and its working fine . No more focusable children.

AlertDialog.Builder builder = new AlertDialog.Builder(DemoActivity.this);
            View view=LayoutInflater.from(DemoActivity.this).inflate(R.layout.temp,null);
            builder.setView(view);
            builder.setTitle("Pick");
            DatePicker datePicker=view.findViewById(R.id.dp);
            datePicker.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
            builder.setPositiveButton("Set", (dialog, which) -> {

            });
            builder.setNegativeButton("Cancel", (dialog, which) -> {

            });
            AlertDialog dialog = builder.create();
            dialog.show();

And xml is

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<DatePicker
    android:id="@+id/dp"
    android:layout_width="match_parent"
    android:calendarViewShown="false"
    android:datePickerMode="spinner"
    android:layout_height="wrap_content">
</DatePicker>

Well i can not test on Gingerbread (Can not time travel to past) . So its tested on Android Oreo. See if this works as expected on Gingerbread.

ADM
  • 20,406
  • 11
  • 52
  • 83
  • I can still select and copy the text android:calendarViewShown="false" android:datePickerMode="spinner FYI both of these is unavalible in api 10 – László Soós May 16 '18 at 14:58
  • You do not need those in API10 i guess. I can not test it .Gingerbread is not available on AS. – ADM May 16 '18 at 15:41