I have built a view with a Date Picker and a Time Picker in spinner format. Everything seems to be working good except I can't figure out how to set the minutes to be in 5 minute intervals?
I have seen examples of how to do this using a DialogFragment and have used DialogFragments to launch separate activities with a Date Picker or a Time Picker, but I am trying to create an activity with both.
I need to either modify my current code to change the minute interval or incorporate a DialogFragment in my code that works with Time Picker that is embeded in my view.
I can't find any examples of how to do either one. Any suggestions would be appreciated.
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DatePicker simpleDatePicker = (DatePicker)findViewById(R.id.date_picker); // initiate a date picker
//Above API 26
// ????
simpleDatePicker.setOnDateChangedListener(new DatePicker.OnDateChangedListener() {
@Override
public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
// Do something
}
});
TimePicker simpleTimePicker = (TimePicker)findViewById(R.id.time_picker); // initiate a time picker
simpleTimePicker.setIs24HourView(true);// set to 24 hour
simpleTimePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
@Override
public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
// Do somthing
}
});
}
public void cancelBTN(View view) {
}
public void saveBTN(View view) {
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<RelativeLayout
android:id="@+id/header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<Button
android:id="@+id/cancelBTN"
android:layout_width="wrap_content"
android:layout_height="45dp"
android:background="@color/gray"
android:onClick="cancelBTN"
android:text="Cancel"
android:textColor="@color/white" />
<TextView
android:id="@+id/Title"
android:layout_width="match_parent"
android:layout_height="45dp"
android:background="@color/gray"
android:gravity="center"
android:text="Select Date - Time"
android:textColor="@color/white"
android:textSize="20dp" />
<Button
android:id="@+id/saveBTN"
android:layout_width="wrap_content"
android:layout_height="45dp"
android:layout_alignParentRight="true"
android:background="@color/gray"
android:onClick="saveBTN"
android:text="Save"
android:textColor="@color/white" />
<DatePicker
android:calendarViewShown="false"
android:datePickerMode="spinner"
android:id="@+id/date_picker"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="25dp"
android:layout_marginRight="25dp"
android:layout_below="@+id/saveBTN"
/>
<TimePicker
android:timePickerMode="spinner"
android:id="@+id/time_picker"
android:layout_marginTop="15dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="25dp"
android:layout_marginRight="25dp"
android:layout_below="@+id/date_picker"
/>
</RelativeLayout>
</RelativeLayout>
I found a way to without a DialogFragment. I added a new function and called it after initializing the TimePicker. Se below code for details.
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DatePicker simpleDatePicker = (DatePicker)findViewById(R.id.date_picker); // initiate a date picker
//Above API 26
// ????
simpleDatePicker.setOnDateChangedListener(new DatePicker.OnDateChangedListener() {
@Override
public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
// Do something
}
});
TimePicker simpleTimePicker = (TimePicker)findViewById(R.id.time_picker); // initiate a time picker
simpleTimePicker.setIs24HourView(true);// set to 24 hour
setTimePickerInterval(simpleTimePicker);
simpleTimePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
@Override
public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
// Do somthing
}
});
}
private void setTimePickerInterval(TimePicker timePicker) {
try {
int TIME_PICKER_INTERVAL = 5;
NumberPicker minutePicker = (NumberPicker) timePicker.findViewById(Resources.getSystem().getIdentifier(
"minute", "id", "android"));
minutePicker.setMinValue(0);
minutePicker.setMaxValue((60 / TIME_PICKER_INTERVAL) - 1);
List<String> displayedValues = new ArrayList<String>();
for (int i = 0; i < 60; i += TIME_PICKER_INTERVAL) {
displayedValues.add(String.format("%02d", i));
}
minutePicker.setDisplayedValues(displayedValues.toArray(new String[0]));
} catch (Exception e) {
}
}
public void cancelBTN(View view) {
}
public void saveBTN(View view) {
}
}
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