PersianMaterialDateTimePicker

repository·master·Indexed 43 days ago

https://github.com/mohamad-amin/persianmaterialdatetimepicker

An Android library providing Material Design-compliant Hijri/Shamsi (Persian) Date Pickers and standard Time Pickers for Android 4.0.3 (API 15) and above. It includes support for custom themes, selectable and highlighted days via PersianCalendar, and standard listener interfaces like OnTimeSetListener and OnDateSetListener.

Tokens
1.2K
Snippets
4
Records
5
Agent score
6%

What's inside PersianMaterialDateTimePicker

  1. Create a DatePickerDialog or TimePickerDialog

    master

    Use the static newInstance() method to create an instance of DatePickerDialog or TimePickerDialog. You must provide the context, default values (year, month, day for DatePicker), and the listener. Once created, call .show() to display the dialog.

    Note: For the DatePickerDialog, it is recommended to use a PersianCalendar instance to provide the correct default Persian date values.

    PersianCalendar persianCalendar = new PersianCalendar();
    DatePickerDialog datePickerDialog = DatePickerDialog.newInstance(
      MainActivity.this,
      persianCalendar.getPersianYear(),
      persianCalendar.getPersianMonth(),
      persianCalendar.getPersianDay()
    );
    datePickerDialog.show(getFragmentManager(), "Datepickerdialog");
  2. Theme the pickers

    master

    You can customize the accent colors of the pickers by overriding the following color resources in your project's colors.xml:

    • mdtp_accent_color: The primary accent color.
    • mdtp_accent_color_dark: The dark variant of the accent color.
    • mdtp_date_picker_text_highlighted: The color used for highlighted days in the DatePickerDialog.
    <color name="mdtp_accent_color">#009688</color>
    <color name="mdtp_accent_color_dark">#00796b</color>
  3. Configure TimePickerDialog options

    master

    The TimePickerDialog supports several customization options:

    • setThemeDark(boolean): Enables dark theme for the picker.
    • setTitle(String title): Displays a title at the top of the dialog.
    • setOnDismissListener(DialogInterface.OnDismissListener): Callback for when the dialog is dismissed.
    • setOnCancelListener(DialogInterface.OnCancelListener): Callback for when the dialog is cancelled.
    • vibrate(boolean): Enables or disables device vibration upon selection (defaults to true).
    timePickerDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
        @Override
        public void onCancel(DialogInterface dialogInterface) {
          Log.d("TimePicker", "Dialog was cancelled");
        }
    });
  4. Configure DatePickerDialog options

    master

    The DatePickerDialog supports several customization options:

    • setThemeDark(boolean): Enables dark theme for the picker.
    • setSelectableDays(PersianCalendar[] days): Restricts the picker to only allow selection of the provided dates. This takes precedence over setMinDate and setMaxDate.
    • setHighlightedDays(PersianCalendar[] days): Renders specific dates in bold.
    • setOnDismissListener(DialogInterface.OnDismissListener): Callback for when the dialog is dismissed.
    • setOnCancelListener(DialogInterface.OnCancelListener): Callback for when the dialog is cancelled.
    • vibrate(boolean): Enables or disables device vibration upon selection (defaults to true).
  5. Implement OnTimeSetListener and OnDateSetListener

    master

    To receive the date or time selected by the user, you must implement the OnTimeSetListener or OnDateSetListener interfaces. These interfaces follow the standard Android picker API. Typically, the Activity or Fragment that triggers the picker implements these callbacks.

    • onTimeSet(RadialPickerLayout view, int hourOfDay, int minute): Triggered when a time is selected.
    • onDateSet(DatePickerDialog view, int year, int monthOfYear, int dayOfMonth): Triggered when a date is selected.
    @Override
    public void onTimeSet(RadialPickerLayout view, int hourOfDay, int minute) {
      String time = "You picked the following time: "+hourOfDay+"h"+minute;
      timeTextView.setText(time);
    }
    
    @Override
    public void onDateSet(DatePickerDialog view, int year, int monthOfYear, int dayOfMonth) {
      String date = "You picked the following date: "+dayOfMonth+"/"+(monthOfYear+1)+"/"+year;
      dateTextView.setText(date);
    }