I have a field to choose the time, through a TimerPicker.
It's working perfectly.
I'm looking for a way to restrict or block access at certain times.
For example: The Fields of Opening and Closing Time, saved in the FireStore (Firebase):
- Start time: 08:00
- HourEnd: 7:00 p.m.
I need to block the hours before the Start Time, and the hours after the End Time.
Code TimePicker:
private void timePicker(){
final Calendar c = Calendar.getInstance();
mHour = c.get(Calendar.HOUR_OF_DAY);
mMinute = c.get(Calendar.MINUTE);
TimePickerDialog timePickerDialog = new TimePickerDialog(this,
new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
mHour = hourOfDay;
mMinute = minute;
/*Formato da hora*/
String curTime = String.format("%02d:%02d", hourOfDay, minute);
mHora.setText(curTime);
mHoraString = curTime;
}
}, mHour, mMinute, true);
timePickerDialog.show();
}
I hope this gives you an idea of how to implement what you are looking for. I realize that this isn't the exact solution you are looking for, but it is a start.
This is the code I use for my TimePickerDialogFragment. This is designed to display the minutes segment of the TimePicker
for certain intervals (eg. 5, 10, or 15 minutes)
In the activity that will call the TimePickerDialogFragment you will need to implement the callback listener:
implements TimePickerDialogFragment.EditTimeDialogListener
And you call the dialog like this. This example makes the minutes into 15 minute intervals:
TimePickerDialogFragment picker = TimePickerDialogFragment.newInstance("Select a Time", initialStartHour, 15);
picker.show(getFragmentManager(), "TimePickerDialogFragment");
initialStartHour
is the Hour I initalize the picker with for example 10 For 10 AM.
The code for the my TimePickerDialogFragment is setup to set the minutes in intervals.
public class TimePickerDialogFragment extends DialogFragment {
private static final String ARG_TITLE = "title";
private static final String ARG_START_HOUR = "startHour";
private static final String ARG_MINUTE_INTERVAL = "minute_interval";
private static final DecimalFormat FORMATTER = new DecimalFormat("00");
NumberPicker minutePicker;
private String title;
private int startHour;
private int minuteInterval;
private String selectedTime;
private EditTimeDialogListener mListener;
public TimePickerDialogFragment() {
}
public static TimePickerDialogFragment newInstance(String title, int startHour, int minuteInterval) {
TimePickerDialogFragment fragment = new TimePickerDialogFragment();
Bundle args = new Bundle();
args.putString(ARG_TITLE, title);
args.putInt(ARG_START_HOUR, startHour);
args.putInt(ARG_MINUTE_INTERVAL, minuteInterval);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
title = getArguments().getString(ARG_TITLE);
startHour = getArguments().getInt(ARG_START_HOUR);
minuteInterval = getArguments().getInt(ARG_MINUTE_INTERVAL);
}
}
@Override
public Dialog onCreateDialog(Bundle saveIntsanceState){
final Context context = getActivity();
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
View rootView = inflater.inflate(R.layout.fragment_time_picker_dialog, null, false);
final TimePicker picker = (TimePicker) rootView.findViewById(R.id.timePicker);
View minute = picker.findViewById(Resources.getSystem().getIdentifier("minute", "id", "android"));
setMinutePicker(minute);
//Block number keyboard
picker.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
picker.setCurrentHour(startHour);
picker.setCurrentMinute(0);
// setHour is available for API level 23 and above!!
// picker.setHour(startHour);
final TextView tvTitle = (TextView)rootView.findViewById(R.id.tvTimePickerDialogTitle);
tvTitle.setText(title);
// picker.setSpinnersShown(false);
// picker.setCalendarViewShown(true);
builder.setView(rootView)
// setTitle is available for API level 23 and above!!
// .setTitle(title)
.setPositiveButton(R.string.ok_button_dialog_title, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// call the method on the parent activity when user click the positive button
Integer h = picker.getCurrentHour();
Integer m = picker.getCurrentMinute() * minuteInterval;
String hh = h.toString();
if(h < 10){
hh = "0" + hh;
}
String mm = m.toString();
if(m < 10){
mm = "0" + mm;
}
selectedTime = hh + mm;
if(mListener == null) mListener = (EditTimeDialogListener) context;
mListener.onFinishTimeDialog(selectedTime);
}
})
.setNegativeButton(R.string.cancel_button_dialog_title, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// call the method on the parent activity when user click the negative button
}
});
return builder.create();
}
private void setMinutePicker(View minute){
int n = 60/minuteInterval;
String[] displayValues = new String[n];
for(int i = 0; i < n; i++){
displayValues[i] = FORMATTER.format(i*minuteInterval);
}
if((minute != null) && (minute instanceof NumberPicker)){
minutePicker = (NumberPicker) minute;
minutePicker.setMinValue(0);
minutePicker.setMaxValue(n-1);
minutePicker.setDisplayedValues(displayValues);
}
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
try {
if(mListener == null) mListener = (EditTimeDialogListener) context;
}
catch (Exception ex){
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
public interface EditTimeDialogListener {
void onFinishTimeDialog(String selectedTime);
}
}
Now all you have to do is extract the time selected in the fragment with this in your calling activity:
@Override
public void onFinishTimeDialog(String selectedTime) {
// Now set the TextView text!!
mHora.setText(selectedTime);
}