I'm trying to figure out how to implement a Data and Time picker for and app I'm building. I can't seem to find a built in way to allow the user to set the date they quit. Is there an elegant way to implement one? I'll need to be able to pull the day, month, year, hour, and minute values in order to calculate milliseconds. Any advice on what's available to achieve this?
For time/date picker within PreferenceScreen
, you can invoke the dialog using onPreferenceClickListener
(Preference). Use DatePickerDialog.OnDateSetListener
or TimePickerDialog.OnTimeSetListener
within PreferenceActivity
to retrieve the selected date/time.
SettingsActivity.java
public class SettingsActivity extends PreferenceActivity implements DatePickerDialog.OnDateSetListener {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
Preference btnDateFilter = (Preference) findPreference("btnDateFilter");
btnDateFilter.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@Override
public boolean onPreferenceClick(Preference preference) {
showDateDialog();
return false;
}
});
}
@Override
public void onDateSet(DatePicker datePicker, int i, int i2, int i3) {
Log.i("dasd","year "+i+" month "+i2+" day "+i3);
}
private void showDateDialog(){
// Use the current date as the default date in the picker
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
new DatePickerDialog(this,this, year, month, day).show();
}
}
preference.xml
<Preference
android:key="btnDateFilter"
android:summary="Reschedule using current setting"
android:title="Reschedule" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With