I am trying to have a Date picker and a time picker that extends dialog fragment. I am able to get the Date picker when I run the app, however when I added the time picker, I am getting an error cannot resolve method DateFormat.is24HourFormat.
I am using import android.support.v4.app.DialogFragment; as per the post
TimepickerFragment show() method cannot be resolved
but the issue still persists
Main activity
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;
import java.text.DateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
public class MainActivity extends AppCompatActivity implements DatePickerDialog.OnDateSetListener, TimePickerDialog.OnTimeSetListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void datePicker(View view) {
DatePickerFragment fragment = new DatePickerFragment();
fragment.show(getSupportFragmentManager(), "date");
}
public void TimePicker(View view) {
TimePickerFragment fragment = new TimePickerFragment();
fragment.show(getSupportFragmentManager(), "time");
}
private void setDate(final GregorianCalendar calendar) {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
final DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM);
((TextView) findViewById(R.id.pic_date)).setText(dateFormat.format(calendar.getTime()));
}
}
@Override
public void onDateSet(DatePicker view, int year, int month, int day) {
GregorianCalendar cal = null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
cal = new GregorianCalendar(year, month, day);
}
setDate(cal);
}
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
Calendar cal = null;
((TextView) findViewById(R.id.start_time)).setText(String.valueOf(hourOfDay)+ String.valueOf(minute));
}
public static class DatePickerFragment extends DialogFragment {
@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(), (DatePickerDialog.OnDateSetListener) getActivity(), year, month, day);
}
}
public static class TimePickerFragment extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in 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 DatePickerDialog and return it
return new TimePickerDialog(getActivity(), this, hour, minute, DateFormat.is24HourFormat(getActivity()));
}
}
}
I had the same problem. I resolved it by importing
instead of
which is what I had, or in your case I would try replacing this:
(see here: https://stackoverflow.com/a/15100358/3615383 )