private void sendResult(String callback, Date result, byte[] opaque) { this.setFieldsEnabled(false); long res = result == null ? 0 : result.getTime() / 1000; Logger.D(TAG, "Return result: " + res); DateTimePicker.callback(callback, res, opaque, result == null); finish(); }
private void makeDateTimePicker(final Button button) { // Create the dialog final Dialog mDateTimeDialog = new Dialog(this); // Inflate the root layout final RelativeLayout mDateTimeDialogView = (RelativeLayout) getLayoutInflater().inflate(R.layout.date_time_dialog, null); // Grab widget instance final DateTimePicker mDateTimePicker = (DateTimePicker) mDateTimeDialogView.findViewById(R.id.DateTimePicker); // Check is system is set to use 24h time (this doesn't seem to work as expected though) final String timeS = android.provider.Settings.System.getString( getContentResolver(), android.provider.Settings.System.TIME_12_24); final boolean is24h = !(timeS == null || timeS.equals("12")); // Update demo TextViews when the "OK" button is clicked mDateTimeDialogView .findViewById(R.id.SetDateTime) .setOnClickListener( new View.OnClickListener() { public void onClick(View v) { mDateTimePicker.clearFocus(); String date = mDateTimePicker.get(Calendar.YEAR) + "/" + (mDateTimePicker.get(Calendar.MONTH) + 1) + "/" + mDateTimePicker.get(Calendar.DAY_OF_MONTH); String time; if (mDateTimePicker.is24HourView()) { time = mDateTimePicker.get(Calendar.HOUR_OF_DAY) + ":" + mDateTimePicker.get(Calendar.MINUTE); } else { time = mDateTimePicker.get(Calendar.HOUR) + ":" + mDateTimePicker.get(Calendar.MINUTE) + " " + (mDateTimePicker.get(Calendar.AM_PM) == Calendar.AM ? "AM" : "PM"); } button.setText(date + " " + time); mDateTimeDialog.dismiss(); } }); // Cancel the dialog when the "Cancel" button is clicked mDateTimeDialogView .findViewById(R.id.CancelDialog) .setOnClickListener( new View.OnClickListener() { public void onClick(View v) { mDateTimeDialog.cancel(); } }); // Reset Date and Time pickers when the "Reset" button is clicked mDateTimeDialogView .findViewById(R.id.ResetDateTime) .setOnClickListener( new View.OnClickListener() { public void onClick(View v) { mDateTimePicker.reset(); } }); // Setup TimePicker mDateTimePicker.setIs24HourView(is24h); // No title on the dialog window mDateTimeDialog.requestWindowFeature(Window.FEATURE_NO_TITLE); // Set the dialog content view mDateTimeDialog.setContentView(mDateTimeDialogView); // Display the dialog mDateTimeDialog.show(); }