Hours, Minutes selector widget - android

I am porting one of my iOS apps over to android, and one of the layouts uses the UIDatePicker with the mode UIDatePickerModeCountDownTimer. I have searched and searched and searched for something even close in android, and the only thing I have come across is the TimePicker widget which only seems to do time of day.
Does something like UIDatePickerModeCountDownTimer exist in android, or do I have to fenagle my own solution for entering this type of time.

There is no one stop shop that includes a date picker and a time picker, both are seperate
so basically you need a date field that shows a DatePickerDialog and a time field that show a TimePickerDialog when clicked

As far as I am aware you will either need a 3rd party library or you will need to create a custom version of the TimePicker.

EDIT :
Basically i've done something similar like your requirement, i have a time picker dialog in dialog fragment, without am/pm indicator. You only have to set the DateFormat to 24 hour format in onCreateDialog method. Something similar like this :
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current time as the default values for the picker
int hour = 0;
int minute = 0;
// Create a new instance of TimePickerDialog without am/pm and return it
return new TimePickerDialog(getActivity(), this, hour, minute,
DateFormat.is24HourFormat(getActivity()));
}
I hope this answer help you :)

Related

How to perform an action on MaterialDatePicker in an Espresso test?

I have a MaterialDatePicker dialog, and I want to write an Espresso test that selects a date. Unfortunately, I can't use PickerActions for this. I'm looking for something similar to this:
onView(withClassName(Matchers.equalTo(DatePicker.class.getName()))).perform(PickerActions.setDate(year, monthOfYear, dayOfMonth));
Does anyone have any ideas on how to go about this?
Thanks in advance!
I found this answer buried in the question linked above, which you may find useful for setting the date when the InputMode is INPUT_MODE_TEXT.
public static void setDate(LocalDate date){
onView(withTagValue((Matchers.is((Object) ("TOGGLE_BUTTON_TAG"))))).perform(click());
onView(withId(com.google.android.material.R.id.mtrl_picker_text_input_date)).perform(replaceText(date.format(DateTimeFormatter.ofPattern("M/d/yy"))));
}
public static void setTime(LocalTime time){
onView(withId(com.google.android.material.R.id.material_timepicker_mode_button)).perform(click());
onView(withId(com.google.android.material.R.id.material_hour_text_input)).perform(click());
onView(allOf(isDisplayed(), withClassName(is(AppCompatEditText.class.getName())))).perform(replaceText(time.format(DateTimeFormatter.ofPattern("hh"))));
onView(withId(com.google.android.material.R.id.material_minute_text_input)).perform(click());
onView(allOf(isDisplayed(), withClassName(is(AppCompatEditText.class.getName())))).perform(replaceText(time.format(DateTimeFormatter.ofPattern("mm"))));
}
It's definitely janky in terms of relying very closely on the ids to not change, but it is effective.
If you want to select a date when the InputMode is INPUT_MODE_CALENDAR, then this worked for me:
onView(
// the date you are selecting must be visible for this to work
withContentDescription("Mon, Jan 1, 1990")
).inRoot(RootMatchers.isDialog())
.perform(click())
onView(withId(com.google.android.material.R.id.confirm_button))
.perform(click())
You could expand this answer further to select a different month/day/year by clicking on the appropriate buttons in the Material Date Picker layout. You can go spelunking for the ids in the source code.

Android DatePicker: How to reset DatePicker to current date automatically after the user selects the date?

Hello StackOverFlow Community,
I have created an app with two DatePickers in one activity. I need for the DatePicker to reset to the current date after the user chooses the date with it. Next, I have a clear button in the activity. I need that to also reset the DatePicker to the current date onClick of that button. Can anyone help me to accomplish this?
I really appreciate your time. Please, if you are going to give me a minus point for this question, can you explain why? I am trying to get better and that is why I am here. Thanks again.
public void onResetClick(){
resetDateToToday();
}
public void onDateSelected(){
resetDateToToday();
}
public void resetDateToToday(){
Calendar cal = Calendar.getInstance();
datePicker.updateDate(cal.get(Calendar.YEAR),cal.get(Calendar.MONTH) - 1,cal.get(Calendar.DAY_OF_MONTH));
}

calendar setFirstDayOfWeek for entire app

I have a time registration app and some users requested to change to first day of the week according to their preferences. I know I can set the first day of the week on a calendar object, but I'm working with a lot of them and I don't feel like setting the first day on all of them. So is there a way to set the first day for my entire app?
I don't feel like setting the first day on all of them
There's a golden rule on software programming that says "Don't repeat yourself", also known as DRY rule. So you shouldn't write the same code again and again to set the first day of week, but some code still should do it for all of them, so....
On those cases, the common way is to build a factory.
class CalendarFactory {
public static void setFirstDayOfWeek(Context context, int value){
// use context to save value in SharedPreferences
}
public static Calendar newInstance(Context context) {
// read the value from SharedPreferences
// create new GregorianCalendar
// setFirstDayOfWeek
// return
}
}
then it's just make sure to acquire instance of calendar ALWAYS through this method.

Changing the title on a TimePicker?

I'm trying to look for a solution to change the title on a TimePicker dialog. Right now it says whatever the system time is (ex. "12:23 AM") but I want to change this to something a little more descriptive. Does anyone have any suggestions?
By request. :)
By TimePicker dialog do you mean the
actual TimePickerDialog? Because that
has a setTitle(CharSequence text)
method. The TimePickerDialog is what
is used in the official tutorial.
http://developer.android.com/reference/android/app/AlertDialog.html#setTitle(java.lang.CharSequence)
This question is rather old, but it shows up on Google results if you search this question. So, I thought I'll post my solution.
DatePicker has setCustomTitle(View view), where you can define your own view (e.g. TextView with your custom text) to be used as a title. This one does not update when changing values.
I dont know how to reply to the comments below question, so I use "Answer".
By looking into source code: SourceCode
You may realize that OnTimeChangedListener is implemented by TimePickerDialog.
To avoid title changed while adjusting time, you may derived from TimePickerDialog and override public void onTimeChanged(TimePicker view, int hourOfDay, int minute);
Notice, dont call super version, or it will setTitle again...

how to show dialog to pick date and time togather?

i am using following code to show date picker in android
DatePickerDialog dial= new DatePickerDialog(this.getContext(),0,mDateSetListener,thisYear,thisMonth,thisDay);
dial.setOnDismissListener(new OnDismissListener() {
#Override
public void onDismiss(DialogInterface arg0) {
// dismiss it.
}
});
dial.show();
now i want to pick time too along with date any one guide me how to achieve this?
Step #1: Create a dialog (e.g., use AlertDialog.Builder).
Step #2: Put a DatePicker and a TimePicker in the dialog (e.g., setView() on AlertDialog.Builder).
Note that this may not work well on small screens, since DatePicker and TimePicker are each large -- there may not be enough room for both and the dialog buttons and such.
As said you can create a dialog with both in however on most screens it will look poor due to the size of them.
You could recreate your own making it smaller (it's not that hard) or reflow your app and have them on separate pop-ups

Categories

Resources