so i have 2 textviews that must take user input being the current time only in half hour slots tho so for example
start time can only be 3:30 or 4:30 not 5 not 4 or 3. if the current time is 4 than the time should default start to 4:30
and finish time has the same rules
i have the time view and picker working
the problem is the time being picked is being set in both start and finish text views. can not find out why im still fixing the half hour problem but wish to do that on my own any tips though would be thankfull
public class TimePickerFragment extends DialogFragment
{
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState)
{
Calendar calTime = Calendar.getInstance();
int hour = calTime.get(Calendar.HOUR_OF_DAY);
int min = calTime.get(Calendar.MINUTE );
return new TimePickerDialog(getActivity(), (TimePickerDialog.OnTimeSetListener) getActivity(), hour, min, android.text.format.DateFormat.is24HourFormat(getActivity()));
}
}
displayStartTime.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
DialogFragment startTimePicker = new TimePickerFragment();
startTimePicker.show(getSupportFragmentManager(), "startTimePicker");
}
});
TextView displayFinishTime = (TextView) findViewById(R.id.finishTimeView);
displayFinishTime.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
DialogFragment finishTimePicker = new TimePickerFragment();
finishTimePicker.show(getSupportFragmentManager(), "finishTimePicker");
}
});
public void onTimeSet(TimePicker view, int hourOfDay, int minute)
{
TextView startTextView = (TextView)findViewById(R.id.startTimeView);
startTextView.setText(hourOfDay+":"+minute);
TextView finishTextView = (TextView)findViewById(R.id.finishTimeView);
finishTextView.setText(hourOfDay+":"+minute);
}
<TextView
android:id="@+id/finishTimeView"
android:layout_width="161dp"
android:layout_height="25dp"
android:layout_marginEnd="44dp"
android:layout_marginBottom="99dp"
android:text="Choose Finish Time"
android:textAlignment="center"
android:textColor="#000000"
android:textSize="18sp"
app:layout_constraintBottom_toTopOf="@+id/confirmBooking"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/timefinishText"
app:layout_constraintTop_toBottomOf="@+id/startTimeView" />
<TextView
android:id="@+id/startTimeView"
android:layout_width="161dp"
android:layout_height="25dp"
android:layout_marginEnd="44dp"
android:layout_marginBottom="40dp"
android:text="Choose Start Time"
android:textAlignment="center"
android:textColor="#000000"
android:textSize="18sp"
app:layout_constraintBottom_toTopOf="@+id/finishTimeView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/timestartText"
app:layout_constraintTop_toBottomOf="@+id/dateView" />
There are some problems there, you are implementing this interface TimePickerDialog.OnTimeSetListener
which will be called every time a TimePickerDialog
is closed, so it will get always the last one, what you can do is remove that interface and implement by yourself.
TimePickerDialog.OnTimeSetListener mTimeSetListener = new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
//Do the changes to the TextView
}
};
And you assign it whenever you want to create the TimePickerDialog
, also you can remove the TimePickerFragment.java
and do in the same class at the moment to check if it works, because you'll need to pass it as a parameter then.
And for the 30 min, you need to create a CustomTimePicker
to do that.
public class DurationTimePickDialog extends TimePickerDialog
{
public static final int TIME_PICKER_INTERVAL = 30;
private boolean mIgnoreEvent = false;
public DurationTimePickDialog(
Context context, OnTimeSetListener callBack, int hourOfDay, int minute,
boolean is24HourView)
{
super(context, callBack, hourOfDay, minute, is24HourView);
}
@Override
public void onTimeChanged(TimePicker timePicker, int hourOfDay, int minute)
{
super.onTimeChanged(timePicker, hourOfDay, minute);
if (!mIgnoreEvent)
{
minute = getRoundedMinute(minute);
mIgnoreEvent = true;
timePicker.setCurrentMinute(minute);
mIgnoreEvent = false;
}
}
public static int getRoundedMinute(int minute)
{
if (minute % TIME_PICKER_INTERVAL != 0)
{
int minuteFloor = minute - (minute % TIME_PICKER_INTERVAL);
minute = minuteFloor + (minute == minuteFloor + 1 ? TIME_PICKER_INTERVAL : 0);
if (minute == 60)
{
minute = 0;
}
}
return minute;
}
}
With this class that you can create a TimePickerDialog
as :
DurationTimePickDialog durationTimePickDialog = new DurationTimePickDialog(v.getContext(), mTimeSetListener,hour,min,true);
durationTimePickDialog.show();
You'll be able only to accept 30 min, otherwise will not clickable, but IMHO this is not the correct way to do that, with another widget
would work also.
Hope it helps.