简体   繁体   中英

Android date picker date not selecting

I have added a date picker which is show once user clicks a view and date picker appears and user can select date on some devices date picker is not showing properly and user is unable to select date. Below screenshot is from Samsung J7 Android version 7.1.1. 在此处输入图像描述

code i'm using for date picker is this

public void initDatePicker(final TextView textView) {
        Calendar calendar = Calendar.getInstance();
        DatePickerDialog dialog = new DatePickerDialog(this, (view, year, month, dayOfMonth) -> {
            Date date = new Date();

            Calendar c = Calendar.getInstance();
            c.set(Calendar.YEAR, year);
            c.set(Calendar.MONTH, month);
            c.set(Calendar.DAY_OF_MONTH, dayOfMonth);
            // and get that as a Date
            Date dateSpecified = c.getTime();
            if (dateSpecified.before(date)) {
                ((TextView)findViewById(R.id.tv_date)).setTextColor(getResources().getColor(R.color.red));
                UIHelper.showShortToastInCenter(this, "Selected Date is in Past");
            } else {
                
                DateSelected = dateSpecified;
                String predate = new SimpleDateFormat("EEE,MMM d").format(c.getTime());
                String predateNew = new SimpleDateFormat("dd/MM/yyyy").format(c.getTime());
                textView.setText(predate);
                textView.setPaintFlags(Typeface.BOLD);
                dateStr = predateNew;
                
            }
        },
                calendar.get(Calendar.YEAR),
                calendar.get(Calendar.MONTH),
                calendar.get(Calendar.DAY_OF_MONTH)
        );
        Calendar min = Calendar.getInstance();
        min.set(calendar.get(Calendar.YEAR),
                calendar.get(Calendar.MONTH),
                calendar.get(Calendar.DAY_OF_MONTH));
        dialog.getDatePicker().setMinDate(min.getTimeInMillis());
        Calendar max = Calendar.getInstance();
        max.set(calendar.get(Calendar.YEAR),
                calendar.get(Calendar.MONTH),
                calendar.get(Calendar.DAY_OF_MONTH) + 30);
        dialog.getDatePicker().setMaxDate(max.getTimeInMillis());
        dialog.getDatePicker().setEnabled(true);
        dialog.show();
    }

here is the theme of activity

 <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
        <item name="colorPrimary">@color/colorAccent</item>
        <item name="colorPrimaryDark">@color/colorAccent</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

I found a way to solve the above issue, using basic dialog picker i was unable to change accent color of piker, i used a Material Date/Time picker which allows customisation of both date and time picker. below is the snippet which helped me fixing the issue

    Calendar calendar = Calendar.getInstance();
    DatePickerDialog dialog =  DatePickerDialog.newInstance(
            this::onDateSet,//Listener when user select date
            calendar.get(Calendar.YEAR), // Initial year selection
            calendar.get(Calendar.MONTH), // Initial month selection
            calendar.get(Calendar.DAY_OF_MONTH) // Initial day selection
    );
    dialog.setMinDate(calendar);
    Calendar max = Calendar.getInstance();
    max.set(calendar.get(Calendar.YEAR),
            calendar.get(Calendar.MONTH),
            calendar.get(Calendar.DAY_OF_MONTH) + 30);
    dialog.setMaxDate(max);
    dialog.setAccentColor(getResources().getColor(R.color.colorAccent));
    dialog.show(getSupportFragmentManager(),"DatePicker");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM