mirror of https://github.com/tasks/tasks
Custom time picker dialog for setting specific date
parent
d03651c801
commit
0bfde0a2c2
Binary file not shown.
After Width: | Height: | Size: 2.0 KiB |
@ -0,0 +1,33 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!--
|
||||
**
|
||||
** Copyright 2007, The Android Open Source Project
|
||||
**
|
||||
** Licensed under the Apache License, Version 2.0 (the "License");
|
||||
** you may not use this file except in compliance with the License.
|
||||
** You may obtain a copy of the License at
|
||||
**
|
||||
** http://www.apache.org/licenses/LICENSE-2.0
|
||||
**
|
||||
** Unless required by applicable law or agreed to in writing, software
|
||||
** distributed under the License is distributed on an "AS IS" BASIS,
|
||||
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
** See the License for the specific language governing permissions and
|
||||
** limitations under the License.
|
||||
*/
|
||||
-->
|
||||
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:padding="5dip"
|
||||
android:orientation="vertical">
|
||||
<TimePicker android:id="@+id/timePicker"
|
||||
android:layout_width="fill_parent"
|
||||
android:layout_height="wrap_content"/>
|
||||
|
||||
<CheckBox android:id="@+id/hasTime"
|
||||
android:layout_width="fill_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/TEA_urgency_specific_time"/>
|
||||
</LinearLayout>
|
@ -0,0 +1,187 @@
|
||||
/*
|
||||
* Copyright (C) 2007 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.todoroo.astrid.widget;
|
||||
|
||||
import java.util.Calendar;
|
||||
|
||||
import android.app.AlertDialog;
|
||||
import android.app.TimePickerDialog;
|
||||
import android.content.Context;
|
||||
import android.content.DialogInterface;
|
||||
import android.content.DialogInterface.OnClickListener;
|
||||
import android.os.Bundle;
|
||||
import android.text.format.DateFormat;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.widget.CheckBox;
|
||||
import android.widget.CompoundButton;
|
||||
import android.widget.TimePicker;
|
||||
import android.widget.CompoundButton.OnCheckedChangeListener;
|
||||
import android.widget.TimePicker.OnTimeChangedListener;
|
||||
|
||||
import com.timsu.astrid.R;
|
||||
|
||||
/**
|
||||
* A dialog that prompts the user for the time of day using a {@link TimePicker}.
|
||||
* This is similar to the Android {@link TimePickerDialog} class
|
||||
* except allows users to specify "no specific time".
|
||||
*/
|
||||
@SuppressWarnings("nls")
|
||||
public class DeadlineTimePickerDialog extends AlertDialog implements OnClickListener,
|
||||
OnTimeChangedListener {
|
||||
|
||||
/**
|
||||
* The callback interface used to indicate the user is done filling in
|
||||
* the time (they clicked on the 'Set' button).
|
||||
*/
|
||||
public interface OnDeadlineTimeSetListener {
|
||||
|
||||
/**
|
||||
* @param view The view associated with this listener.
|
||||
* @param hasTime whether time is set
|
||||
* @param hourOfDay The hour that was set.
|
||||
* @param minute The minute that was set.
|
||||
*/
|
||||
void onTimeSet(TimePicker view, boolean hasTime, int hourOfDay, int minute);
|
||||
}
|
||||
|
||||
private static final String HOUR = "hour";
|
||||
private static final String MINUTE = "minute";
|
||||
private static final String IS_24_HOUR = "is24hour";
|
||||
|
||||
private final TimePicker mTimePicker;
|
||||
private final CheckBox mHasTime;
|
||||
private final OnDeadlineTimeSetListener mCallback;
|
||||
private final Calendar mCalendar;
|
||||
private final java.text.DateFormat mDateFormat;
|
||||
|
||||
int mInitialHourOfDay;
|
||||
int mInitialMinute;
|
||||
boolean mIs24HourView;
|
||||
|
||||
/**
|
||||
* @param context Parent.
|
||||
* @param callBack How parent is notified.
|
||||
* @param hourOfDay The initial hour.
|
||||
* @param minute The initial minute.
|
||||
* @param is24HourView Whether this is a 24 hour view, or AM/PM.
|
||||
*/
|
||||
public DeadlineTimePickerDialog(Context context,
|
||||
OnDeadlineTimeSetListener callBack,
|
||||
int hourOfDay, int minute, boolean is24HourView) {
|
||||
this(context, android.R.style.Theme_Dialog,
|
||||
callBack, hourOfDay, minute, is24HourView);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param context Parent.
|
||||
* @param theme the theme to apply to this dialog
|
||||
* @param callBack How parent is notified.
|
||||
* @param hourOfDay The initial hour.
|
||||
* @param minute The initial minute.
|
||||
* @param is24HourView Whether this is a 24 hour view, or AM/PM.
|
||||
*/
|
||||
public DeadlineTimePickerDialog(Context context,
|
||||
int theme,
|
||||
OnDeadlineTimeSetListener callBack,
|
||||
int hourOfDay, int minute, boolean is24HourView) {
|
||||
super(context, theme);
|
||||
mCallback = callBack;
|
||||
mInitialHourOfDay = hourOfDay;
|
||||
mInitialMinute = minute;
|
||||
mIs24HourView = is24HourView;
|
||||
|
||||
mDateFormat = DateFormat.getTimeFormat(context);
|
||||
mCalendar = Calendar.getInstance();
|
||||
updateTitle(mInitialHourOfDay, mInitialMinute);
|
||||
|
||||
setButton(context.getText(android.R.string.ok), this);
|
||||
setButton2(context.getText(android.R.string.cancel), (OnClickListener) null);
|
||||
setIcon(R.drawable.ic_dialog_time);
|
||||
|
||||
LayoutInflater inflater =
|
||||
(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
|
||||
View view = inflater.inflate(R.layout.time_picker_dialog, null);
|
||||
setView(view);
|
||||
mTimePicker = (TimePicker) view.findViewById(R.id.timePicker);
|
||||
mHasTime = (CheckBox) view.findViewById(R.id.hasTime);
|
||||
mHasTime.setOnCheckedChangeListener(new OnCheckedChangeListener() {
|
||||
@Override
|
||||
public void onCheckedChanged(CompoundButton buttonView,
|
||||
boolean isChecked) {
|
||||
mTimePicker.setEnabled(isChecked);
|
||||
if(isChecked)
|
||||
updateTitle(mTimePicker.getCurrentHour(), mTimePicker.getCurrentMinute());
|
||||
else
|
||||
setTitle(R.string.TEA_urgency_time_none);
|
||||
}
|
||||
});
|
||||
mHasTime.setChecked(true);
|
||||
|
||||
// initialize state
|
||||
mTimePicker.setCurrentHour(mInitialHourOfDay);
|
||||
mTimePicker.setCurrentMinute(mInitialMinute);
|
||||
mTimePicker.setIs24HourView(mIs24HourView);
|
||||
mTimePicker.setOnTimeChangedListener(this);
|
||||
}
|
||||
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
if (mCallback != null) {
|
||||
mTimePicker.clearFocus();
|
||||
mCallback.onTimeSet(mTimePicker,
|
||||
mHasTime.isChecked(),
|
||||
mTimePicker.getCurrentHour(),
|
||||
mTimePicker.getCurrentMinute());
|
||||
}
|
||||
}
|
||||
|
||||
public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
|
||||
updateTitle(hourOfDay, minute);
|
||||
}
|
||||
|
||||
public void updateTime(int hourOfDay, int minutOfHour) {
|
||||
mTimePicker.setCurrentHour(hourOfDay);
|
||||
mTimePicker.setCurrentMinute(minutOfHour);
|
||||
}
|
||||
|
||||
private void updateTitle(int hour, int minute) {
|
||||
mCalendar.set(Calendar.HOUR_OF_DAY, hour);
|
||||
mCalendar.set(Calendar.MINUTE, minute);
|
||||
setTitle(mDateFormat.format(mCalendar.getTime()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Bundle onSaveInstanceState() {
|
||||
Bundle state = super.onSaveInstanceState();
|
||||
state.putInt(HOUR, mTimePicker.getCurrentHour());
|
||||
state.putInt(MINUTE, mTimePicker.getCurrentMinute());
|
||||
state.putBoolean(IS_24_HOUR, mTimePicker.is24HourView());
|
||||
return state;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRestoreInstanceState(Bundle savedInstanceState) {
|
||||
super.onRestoreInstanceState(savedInstanceState);
|
||||
int hour = savedInstanceState.getInt(HOUR);
|
||||
int minute = savedInstanceState.getInt(MINUTE);
|
||||
mTimePicker.setCurrentHour(hour);
|
||||
mTimePicker.setCurrentMinute(minute);
|
||||
mTimePicker.setIs24HourView(savedInstanceState.getBoolean(IS_24_HOUR));
|
||||
mTimePicker.setOnTimeChangedListener(this);
|
||||
updateTitle(hour, minute);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue