dtpreference

A date preference, time preference and date and time preference library for Android.

License

License

GroupId

GroupId

com.pcchin.dtpreference
ArtifactId

ArtifactId

dtpreference
Last Version

Last Version

1.3.3
Release Date

Release Date

Type

Type

aar
Description

Description

dtpreference
A date preference, time preference and date and time preference library for Android.
Project URL

Project URL

https://github.com/pcchin/dtpreference
Source Code Management

Source Code Management

https://github.com/pcchin/dtpreference

Download dtpreference

How to add to project

<!-- https://jarcasting.com/artifacts/com.pcchin.dtpreference/dtpreference/ -->
<dependency>
    <groupId>com.pcchin.dtpreference</groupId>
    <artifactId>dtpreference</artifactId>
    <version>1.3.3</version>
    <type>aar</type>
</dependency>
// https://jarcasting.com/artifacts/com.pcchin.dtpreference/dtpreference/
implementation 'com.pcchin.dtpreference:dtpreference:1.3.3'
// https://jarcasting.com/artifacts/com.pcchin.dtpreference/dtpreference/
implementation ("com.pcchin.dtpreference:dtpreference:1.3.3")
'com.pcchin.dtpreference:dtpreference:aar:1.3.3'
<dependency org="com.pcchin.dtpreference" name="dtpreference" rev="1.3.3">
  <artifact name="dtpreference" type="aar" />
</dependency>
@Grapes(
@Grab(group='com.pcchin.dtpreference', module='dtpreference', version='1.3.3')
)
libraryDependencies += "com.pcchin.dtpreference" % "dtpreference" % "1.3.3"
[com.pcchin.dtpreference/dtpreference "1.3.3"]

Dependencies

compile (3)

Group / Artifact Type Version
» unspecified jar
androidx.appcompat » appcompat jar 1.2.0
androidx.preference » preference jar 1.1.1

Project Modules

There are no modules declared in this project.

dtpreference

Maven Central

Library Info

This library contains a date preference, time preference and date and time preference library for Android, allowing users to choose a specific date for DatePreference, a specific time for TimePreference, and a specific date and time for DateTimePreference.

The time value for DatePreference would be at midnight of the selected date, while the date value for the TimePreference would be the current date.

All the values for the preference are stored in the form of a long variable representing the number of milliseconds since the epoch, which can be easily converted to a Calendar object through Calendar.setTimeInMills(long millis).

Installation

This library is available in Maven Central and a backup is available on my personal repository. To install, you would need to include the following into your project/build.gradle:

implementation 'com.pcchin.dtpreference:dtpreference:1.1.3'

You may also need to include the following in your build.gradle:

buildscript {
    ...
}

allprojects {
    ...
    repositories {
        ...
        // Use this if Maven Central is not working
        // maven { url "https://nexus.pcchin.com/repository/maven-releases/" }
        mavenCentral()
    }
}

Usage

For the layout XML, simply include the preference without any additional changes. e.g.:

<com.pcchin.dtpreference.DatePreference
    android:title="Date Preference"
    android:key="pref_date"
/>

For your preference fragment, you would to need to override the onDisplayPreferenceDialog function as shown below in order to display their respective dialogs:

public class PreferenceFragment extends PreferenceFragmentCompat {
    @Override
    public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
        // ... add your preferences here
    }
    
    @Override
    public void onDisplayPreferenceDialog(Preference preference) {
        // Use instanceof to check if the preference is one of
        // DatePreference, TimePreference or DateTimePreference
        DialogFragment dialogFragment = null;
        if (preference instanceof TimePreference) {
            // If it is, then set the dialog fragment to their respective Dialog classes as shown below
            dialogFragment = TimePreferenceDialog.newInstance(preference.getKey());
        } else if (preference instanceof DatePreference) {
            dialogFragment = DatePreferenceDialog.newInstance(preference.getKey());
            // Alternatively, you can specify the minimum date and maximum date as well
            dialogFragment = DatePreferenceDialog.newInstance(preference.getKey(), minDate, maxDate);
        } else if (preference instanceof DateTimePreference) {
            dialogFragment = DateTimePreferenceDialog.newInstance(preference.getKey());
            // You can also specify the minimum and maximum date here
            dialogFragment = DateTimePreferenceDialog.newInstance(preference.getKey(), minDate, maxDate);
        }

        if (dialogFragment != null) {
            // If it is one of our preferences, show it
            dialogFragment.setTargetFragment(this, 0);
            dialogFragment.show(getFragmentManager(), "YOUR TAG HERE");
        } else {
            // Let super handle it
            super.onDisplayPreferenceDialog(preference);
        }
    }
}

The following functions are available for each Dialog:

TimePreferenceDialog:

  • setInitialTime(long time): Sets the initial time displayed by the TimePicker.

DatePreferenceDialog:

  • setInitialDate(long date): Sets the initial date displayed by the DatePicker.

DateTimePreferenceDialog:

  • setInitialDateTime(long dateTime): Sets the initial date and time displayed by both the TimePicker and the DatePicker.

Example Implementation

| TimePreference | DatePreference |

Contribution

Any contribution is welcome, feel free to add any issues or pull requests to the repository.

License

This library is licensed under the Apache 2.0 License.

Versions

Version
1.3.3
1.1.2
1.1.1
1.1.0
1.0.0