Search code examples
androiddatepickerdialog

How to properly display Date picker dialogues in android studio?


I have been coding for android apps for a while now and have never come up with this issue before. I have coded for the **datePickerDialogue** in my android application. The problem is the date picker does not show OK and Cancel buttons and the current selected date on top. I tried the same code with a fresh project the date picker is running perfectly fine in the freshly created project. What might be the error? Can anyone please help me out with this? My Targeted sdk version is 29 and minSdkversion is 15.

The code for datePickerDialogue is as follows:

TextView date_view = (TextView) findViewById(R.id.date_view);
date_view.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Calendar calendar = Calendar.getInstance();
            int year = calendar.get(Calendar.YEAR);
            int month = calendar.get(Calendar.MONTH);
            int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
            DatePickerDialog datePickerDialog = new DatePickerDialog(MoraUserReserve.this,R.style.MyTimePickerDialogTheme,
                    new DatePickerDialog.OnDateSetListener() {
                        @Override
                        public void onDateSet(DatePicker datePicker, int year, int month, int day) {
                            date_view.setText(day + "/" + month + "/" + year);
                        }
                    }, year, month, dayOfMonth);
            datePickerDialog.show();

        }
    });

This is what I get when I run my application


Solution

  • Try this

    Declare this globally.

    private Calendar Calendar1 = Calendar.getInstance();
    DatePickerDialog dpdialog;
    

    Intialize your DatePickerDialog onCreate

    dpdialog = new DatePickerDialog(context, date, Calendar1
                        .get(Calendar.YEAR), Calendar1.get(Calendar.MONTH),
                        Calendar1.get(Calendar.DAY_OF_MONTH));
                dpdialog.getDatePicker();
    

    Put this whenever your want to show the DatePickerDialog.

                dpdialog.show(); 
    

    This pick-up the date

    final DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() {
    
            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear,
                                  int dayOfMonth) {
                // TODO Auto-generated method stub
                Calendar1.set(Calendar.YEAR, year);
                Calendar1.set(Calendar.MONTH, monthOfYear);
                Calendar1.set(Calendar.DAY_OF_MONTH, dayOfMonth);
                updateCalendar1();
            }
    
        };
    

    Put your operation here(change to your desire date format, display it edittext/textview, ... )

    private void updateCalendar1() {
            String myFormat = "MM/dd/yyyy";
            SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
    
            YOUR_TEXTVIEW.setText(sdf.format(Calendar1.getTime()));
        }