Search code examples
androiddatetimepicker

Custom datetime picker not working - Android


I'm using a custom datetimepicker for an android app.
I installed dependency as:

implementation 'com.github.noowenz:CustomDateTimePicker:1.01'

Then I added it in my activity as follows:

final static String TAG = BookcabActivity.class.getCanonicalName();
Calendar selectedDateAndTime = Calendar.getInstance();
mTextViewOne = findViewById(R.id.et_datetime);
        mTextViewOne.setFocusable(false);
        mTextViewOne.setOnClickListener(view -> {
            show_Datepicker();
        });
private void show_Datepicker() {
        new CustomDateTimePicker(this, new CustomDateTimePicker.ICustomDateTimeListener() {
            @Override
            public void onSet(@NotNull Dialog dialog, @NotNull Calendar calendar,
                              @NotNull Date date, int year, @NotNull String monthFullName,
                              @NotNull String monthShortName, int monthNumber, int day,
                              @NotNull String weekDayFullName, @NotNull String weekDayShortName,
                              int hour24, int hour12, int min, int sec, @NotNull String AM_PM) {
                Toast.makeText(ctx, "Date and time selected!", Toast.LENGTH_SHORT).show();
                selectedDateAndTime = calendar;
                String dateAndTime = "Date is" + date + "Year is" + year + "Day is" + day;
                Log.d(TAG, "onSet: Date is" + dateAndTime);
                mTextViewOne.setText(dateAndTime);
            }

            @Override
            public void onCancel() {
                Toast.makeText(ctx, "Date and Time selection is cancelled", Toast.LENGTH_SHORT).show();
            }
        });

    }  

But datetime dialog doesn't pop up, what am I missing?


Solution

  • You are missing the .showDialog()

    Try this:

    private void show_Datepicker() {
            new CustomDateTimePicker(this, new CustomDateTimePicker.ICustomDateTimeListener() {
                @Override
                public void onSet(@NotNull Dialog dialog, @NotNull Calendar calendar,
                                  @NotNull Date date, int year, @NotNull String monthFullName,
                                  @NotNull String monthShortName, int monthNumber, int day,
                                  @NotNull String weekDayFullName, @NotNull String weekDayShortName,
                                  int hour24, int hour12, int min, int sec, @NotNull String AM_PM) {
                    Toast.makeText(ctx, "Date and time selected!", Toast.LENGTH_SHORT).show();
                    selectedDateAndTime = calendar;
                    String dateAndTime = "Date is" + date + "Year is" + year + "Day is" + day;
                    Log.d(TAG, "onSet: Date is" + dateAndTime);
                    mTextViewOne.setText(dateAndTime);
                }
    
                @Override
                public void onCancel() {
                    Toast.makeText(ctx, "Date and Time selection is cancelled", Toast.LENGTH_SHORT).show();
                }
            }).showDialog();
    
        }