Essential Android Date and Time Pickers edit forum

DatePicker

DatePicker allows user to pick date. When we create new instance of DatePicker, we can set initial date. If we don’t set initial date, current date will be set by default.

We can show DatePicker to user by using DatePickerDialog or by creating our own layout with DatePicker widget.


Also we can limit range of date, which user can pick.

By setting minimum date in milliseconds
//In this case user can pick date only from future
datePicker.setMinDate(System.currentTimeMillis());
By setting maximum date in milliseconds
//In this case user can pick date only, before following week.
datePicker.setMaxDate(System.currentTimeMillis() + TimeUnit.DAYS.toMillis(7));

To receive information, about which date was picked by user, we have to use Listener.

If we are using DatePickerDialog, we can set OnDateSetListener in constructor when we are creating new instance of DatePickerDialog:

Sample use of DatePickerDialog

public class SampleActivity extends AppCompatActivity implements DatePickerDialog.OnDateSetListener {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ...
    }

    private void showDatePicker() {
        //We need calendar to set current date as initial date in DatePickerDialog.
        Calendar calendar = new GregorianCalendar(Locale.getDefault());
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        int day = calendar.get(Calendar.DAY_OF_MONTH);

        DatePickerDialog datePickerDialog = new DatePickerDialog(this, this, year, month, day);
        datePickerDialog.show();
    }

    @Override
    public void onDateSet(DatePicker datePicker, int year, int month, int day) {

    }
}

Otherwise, if we are creating our own layout with DatePicker widget, we also have to create our own listener as it was shown in other example


Table Of Contents
39 ACRA
64 Menu
73 Date and Time Pickers
112 Loader
119 Xposed
132 Colors
135 Fresco
140 AdMob
147 Button
156 Vk SDK
170 XMPP
176 OpenCV
200 FileIO
203 Moshi
217 Paint
231 AIDL
241 JCodec
243 Okio
255 Looper
  ↑ ↓ to navigate     ↵ to select     Esc to close