Reputation: 3399
I have next code:
dateDisplay1.setOnClickListener(new OnClickListener() {
@SuppressWarnings("deprecation")
public void onClick(View v) {
option = 1;
showDialog(DATE_DIALOG_ID);
}
});
And the onCreateDialog function:
protected Dialog onCreateDialog(int id) {
Calendar cal = Calendar.getInstance();
switch (id) {
case DATE_DIALOG_ID:
return new DatePickerDialog(this, dayDate, cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH));
case TIME_DIALOG_ID:
return new TimePickerDialog(this, timeDate, cal.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.MINUTE), false);
}
return null;
}
What I am trying to do is to adapt the showDialog(), that is deprecated, to a DialogFragment, with no success. The code below works but it is not best practices. So I would like to correct the code.
How woould you implement that?
Upvotes: 2
Views: 5942
Reputation: 1024
Below code is helpful for displaying calender in alert dialog with restricted past days in my adapter.
changedate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(type.equals("1") ) {
DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {
// when dialog box is closed, below method will be called.
public void onDateSet(DatePicker view, int selectedYear, int selectedMonth, int selectedDay) {
mmonth = selectedMonth;
mday = selectedDay;
myear = selectedYear;
changedate.setText(new StringBuilder()
.append(mday).append("-").append(mmonth + 1).append("-")
.append(myear).append(" "));
}
};
switch (DATE_DIALOG_ID) {
case DATE_DIALOG_ID:
dat1 = new DatePickerDialog(context, datePickerListener, myear, mmonth, mday);
c = Calendar.getInstance();
c.add(Calendar.DATE, 0);
Date newDate = c.getTime();
dat1.getDatePicker().setMinDate(newDate.getTime()-1000);
dat1.getDatePicker().setMinDate(newDate.getTime() - (newDate.getTime() % (24 * 60 * 60 * 1000)));
dat1.setButton(DatePickerDialog.BUTTON_POSITIVE, context.getString(R.string.ok), dat1);
dat1.setButton(DatePickerDialog.BUTTON_NEGATIVE, context.getString(R.string.cancel), dat1);
}
dat1.show();
}
}
});
Upvotes: 0
Reputation: 2456
public class ReminderEditActivity extends Activity {
private Button mDateButton;
private Button mTimeButton;
public Calendar mCalendar;
private static final String DATE_FORMAT = "yyyy-MM-dd";
private static final String TIME_FORMAT = "kk:mm";
DialogFragment dateFragment;
DialogFragment timeFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.reminder_edit);
mTimeButton = (Button) findViewById(R.id.reminder_time);
mDateButton = (Button) findViewById(R.id.reminder_date);
mCalendar = Calendar.getInstance();
mDateButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showDatePickerDialog(v);
}
});
mTimeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showTimePickerDialog(v);
}
});
}
public void showDatePickerDialog(View v) {
dateFragment = new DatePickerFragment();
dateFragment.show(getFragmentManager(), "datePicker");
}
public void showTimePickerDialog(View v) {
timeFragment = new TimePickerFragment();
timeFragment.show(getFragmentManager(), "timePicker");
}
public void updateDateButtonText() {
SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT);
String dateForButton = dateFormat.format(mCalendar.getTime());
mDateButton.setText(dateForButton);
}
private void updateTimeButtonText() {
SimpleDateFormat timeFormat = new SimpleDateFormat(TIME_FORMAT);
String timeForButton = timeFormat.format(mCalendar.getTime());
mTimeButton.setText(timeForButton);
}
class DatePickerFragment extends DialogFragment implements
DatePickerDialog.OnDateSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// 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);
// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), this, year, month, day);
}
public void onDateSet(DatePicker view, int year, int month, int day) {
mCalendar.set(Calendar.YEAR, year);
mCalendar.set(Calendar.MONTH, month);
mCalendar.set(Calendar.DAY_OF_MONTH, day);
updateDateButtonText();
}
}
public class TimePickerFragment extends DialogFragment implements
TimePickerDialog.OnTimeSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current time as the default values for the picker
final Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
// Create a new instance of TimePickerDialog and return it
return new TimePickerDialog(getActivity(), this, hour, minute,
DateFormat.is24HourFormat(getActivity()));
}
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
mCalendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
mCalendar.set(Calendar.MINUTE, minute);
updateTimeButtonText();
}
}
}
Upvotes: 10