-1

I had use a material datepicker, but it is not showing the current year in calendar view Datepicker

 from_date.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            flage_from=true;
            flage_to=false;
            datePickerDialog = DatePickerDialog.newInstance(MainActivity.this, Year, Month, Day);
            datePickerDialog.setThemeDark(false);


            datePickerDialog.showYearPickerFirst(false);

            datePickerDialog.setAccentColor(Color.parseColor("#215f8b"));

           // datePickerDialog.setTitle("Select Date");

            datePickerDialog.show(getFragmentManager(), "DatePickerDialog");

        }
    });

This is my code

4 Answers4

1

I have also added the code to set the chosen date into a textview. You can remove that code if you don't want and also you can change the format too if you want

Declare the calendar Instance

Calendar c;
int year, month, day;

Then Initialise

c = Calendar.getInstance();
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);

Button onClick Listener

bookdate.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        // Get Current Date

                        DatePickerDialog dd = new DatePickerDialog(Wishlist_Detail_Activity.this,
                                new DatePickerDialog.OnDateSetListener() {

                                    @Override
                                    public void onDateSet(DatePicker view, int year,
                                                          int monthOfYear, int dayOfMonth) {

                                        try {
                                            SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
                                            String dateInString = dayOfMonth + "/" + (monthOfYear + 1) + "/" + year;
                                            Date date = formatter.parse(dateInString);

                                            //bookdate.setText(formatter.format(date).toString());

                                            formatter = new SimpleDateFormat("dd MMMM yyyy");

                                            bookdate.setText(formatter.format(date).toString());


                                        } catch (Exception ex) {

                                        }


                                    }
                                }, year, month, day);
                        dd.show();
                    }
                });
Vishnu M Menon
  • 1,459
  • 2
  • 19
  • 34
0

Try this

Calendar c = Calendar.getInstance();
int mYear = c.get(Calendar.YEAR);
int mMonth = c.get(Calendar.MONTH);
int mDay = c.get(Calendar.DAY_OF_MONTH);

DatePickerDialog dialog =
    new DatePickerDialog(this, mDateSetListener, mYear, mMonth, mDay);
dialog.show();
Vishal Patoliya ツ
  • 3,170
  • 4
  • 24
  • 45
0

You can try this solution:

 final Calendar calendar = Calendar.getInstance();
        final DatePicker datePicker = new DatePicker(this);
        datePicker.setMinDate(calendar.getTimeInMillis());
        datePicker.setCalendarViewShown(false);

        new AlertDialog.Builder(this)
                .setView(datePicker)
                .setNegativeButton(android.R.string.cancel, null)
                .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        mActionsListener.filterEventsByDate(datePicker.getYear(), datePicker.getMonth(), datePicker.getDayOfMonth());
                    }
                })
                .show();
    }
Luiz Fernando Salvaterra
  • 4,192
  • 2
  • 24
  • 42
0

Try This one

DatePickerDialog dpd = DatePickerDialog.newInstance(MainActivity.this,
now.get(Calendar.YEAR),
now.get(Calendar.MONTH),
now.get(Calendar.DAY_OF_MONTH);
dpd.show(getFragmentManager(), "Datepickerdialog");
Ratilal Chopda
  • 4,162
  • 4
  • 18
  • 31