Android : Is It Possible To Show Only Quaters in Time Picker ? 00 : 15 : 30 :45
Android Time Picker Does Not Provide setInterval()
method to do that we need to work with MaterialDateTimepicker
implementation 'com.wdullaer:materialdatetimepicker:3.6.3'
its latest version was conflicting with some of my libraries that's why i used 3.6.3
Also those who are using NumberPicker
to do that it doesn't work for me
Calendar now = Calendar.getInstance();
TimePickerDialog timePickerDialog = TimePickerDialog.newInstance(mTimePickerListener,
now.get(Calendar.HOUR_OF_DAY),
now.get(Calendar.MINUTE),
false
);
timePickerDialog.setThemeDark(false);
timePickerDialog.setTitle("TimePicker Title");
timePickerDialog.setTimeInterval(1, 15, 30); // 15 Minutes Interval
//timePickerDialog.setTimeInterval(1, 30, 60); // 30 Minutes Interval
timePickerDialog.setAccentColor(Color.parseColor("#9C27B0"));
timePickerDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialogInterface) {
Log.d("TimePicker", "Dialog was cancelled");
}
});
timePickerDialog.show(getFragmentManager(), "Timepickerdialog");
}
private TimePickerDialog.OnTimeSetListener mTimePickerListener = new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePickerDialog view, int hourOfDay, int minute, int second) {
}
};
TimePicker with 15 minutes interval in xamarin forms
I have found a workaround for Android. The previous override SetView(...)
method can not get the NumberPicker
now, here we can change the minutes interval in override OnTimeChanged
method as follow:
public override void OnTimeChanged(Android.Widget.TimePicker view, int hourOfDay, int minute)
{
if (_ignoreEvent) return;
if (minute % TimePickerInterval != 0)
{
int minuteFloor = minute - (minute % TimePickerInterval);
minute = minuteFloor + (minute == minuteFloor + 1 ? TimePickerInterval : 0);
if (minute == 60)
minute = 0;
_ignoreEvent = true;
view.CurrentMinute = (Java.Lang.Integer)minute;
_ignoreEvent = false;
}
}
The full android renderer code is as follow:
public class CustomTimePickerRenderer : TimePickerRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.TimePicker> e)
{
base.OnElementChanged(e);
TimePickerDialogIntervals timePickerDlg = new TimePickerDialogIntervals(this.Context, new EventHandler<TimePickerDialogIntervals.TimeSetEventArgs>(UpdateDuration),
Element.Time.Hours, Element.Time.Minutes, true);
var control = new EditText(this.Context);
control.Focusable = false;
control.FocusableInTouchMode = false;
control.Clickable = false;
control.Click += (sender, ea) => timePickerDlg.Show();
control.Text = Element.Time.Hours.ToString("00") + ":" + Element.Time.Minutes.ToString("00");
SetNativeControl(control);
}
void UpdateDuration(object sender, Android.App.TimePickerDialog.TimeSetEventArgs e)
{
Element.Time = new TimeSpan(e.HourOfDay, e.Minute, 0);
Control.Text = Element.Time.Hours.ToString("00") + ":" + Element.Time.Minutes.ToString("00");
}
}
public class TimePickerDialogIntervals : TimePickerDialog
{
public const int TimePickerInterval = 15;
private bool _ignoreEvent = false;
public TimePickerDialogIntervals(Context context, EventHandler<TimePickerDialog.TimeSetEventArgs> callBack, int hourOfDay, int minute, bool is24HourView)
: base(context, (sender, e) => {
callBack(sender, new TimePickerDialog.TimeSetEventArgs(e.HourOfDay, e.Minute * TimePickerInterval));
}, hourOfDay, minute / TimePickerInterval, is24HourView)
{
}
public override void OnTimeChanged(Android.Widget.TimePicker view, int hourOfDay, int minute)
{
base.OnTimeChanged(view, hourOfDay, minute);
if (_ignoreEvent) return;
if (minute % TimePickerInterval != 0)
{
int minuteFloor = minute - (minute % TimePickerInterval);
minute = minuteFloor + (minute == minuteFloor + 1 ? TimePickerInterval : 0);
if (minute == 60)
minute = 0;
_ignoreEvent = true;
view.CurrentMinute = (Java.Lang.Integer)minute;
_ignoreEvent = false;
}
}
}
The effect:
How to get 30 min interval in time picker in Android N
I have resolved my issue using this library and here is my complete code which may help someone in the future.
My complete code is in below.
public class MainActivity extends AppCompatActivity implements TimePickerDialog.OnTimeSetListener {
private TextView timeTextView;
private TimePickerDialog timePickerDialog;
private Button timeButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Find our View instances
timeTextView = findViewById(R.id.time_textview);
timeButton = findViewById(R.id.time_button);
// Show a timepicker when the timeButton is clicked
timeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Calendar now = Calendar.getInstance();
timePickerDialog = TimePickerDialog.newInstance(
MainActivity.this,
now.get(Calendar.HOUR_OF_DAY),
now.get(Calendar.MINUTE),
false
);
timePickerDialog.setThemeDark(false);
timePickerDialog.setTitle("TimePicker Title");
timePickerDialog.setTimeInterval(1, 30, 60);
timePickerDialog.setAccentColor(Color.parseColor("#9C27B0"));
timePickerDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialogInterface)
Log.d("TimePicker", "Dialog was cancelled");
}
});
timePickerDialog.show(getFragmentManager(), "Timepickerdialog");
}
});
}
@Override
public void onTimeSet(TimePickerDialog view, int hourOfDay, int minute, int second) {
updateEndTime(hourOfDay,minute);
}
private void updateEndTime(int hours, int mins) {
String timeSet = "";
if (hours > 12) {
hours -= 12;
timeSet = "PM";
} else if (hours == 0) {
hours += 12;
timeSet = "AM";
} else if (hours == 12) {
timeSet = "PM";
} else {
timeSet = "AM";
}
// Append in a StringBuilder
String aTime = new StringBuilder().append(pad(hours)).append(':')
.append(pad(mins)).append(" ").append(timeSet).toString();
timeTextView.setText(aTime);
}
private static String pad(int c) {
if (c >= 10)
return String.valueOf(c);
else
return "0" + String.valueOf(c);
}
@Override
public void onResume() {
super.onResume();
TimePickerDialog tpd = (TimePickerDialog) getFragmentManager().findFragmentByTag("Timepickerdialog");
if (tpd != null) tpd.setOnTimeSetListener(this);
}
}
My desire output is like this:
Related Topics
Read & Writing Arrays of Parcelable Objects
The Android Emulator Is Not Starting, Showing "Invalid Command-Line Parameter"
Parse JSON Array Without Key in Android
Send Data Back to the Script Which Started the Activity via Adb Shell Am Start
Android Studio, Suddenly Got Gpu Driver Issue When Running Emulator
How to Block a Mobile Number Call and Message Receiving in Android Application Development
How to Set Custom Actionbar Color/Style
Changing Overflow Icon in the Action Bar
How to Send Multiple Sms to Single Number
How to Verify That an Android APK Is Signed with a Release Certificate
How to Determine the Screen Width in Terms of Dp or Dip at Runtime in Android
Display Badge on Top of Bottom Navigation Bar's Icon
Android Notification Is Not Showing
Shadow Effect for a Text in Android
Add a Background Image to Shape in Xml Android
Sniffing/Logging Your Own Android Bluetooth Traffic