TimePicker onTimeChanged() event keeps firing as timepicker spins - android

I am using a timepicker for one of my custom alert dialog and I call a method to validate the values and perform some actions when the timepicker value is changed by user.
The issue is that the onTimeChanged event keeps firing when the timepicker is spinning, and that it does not trigger as a single event when time picker stops spinning.
How to get to know when the timepicker stops spinning, so that I can call my method only then, and not at every value when the timepicker spins?
Here is my current code:
startTimePicker = (TimePicker)rootView.findViewById(R.id.timePickerStart);
startTimePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
#Override
public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
validatemethod();//Keeps firing as the timepicker is spinning
}
});

Related

time picker dialog not working

I am new in android, and I tried to add time picker dialog to my app to take the time from the user.
but the dialog was not appear.
I am using android studio.
this is the code:
Calendar calendar=Calendar.getInstance();
int hour= calendar.get(Calendar.HOUR_OF_DAY);
int minutes1= calendar.get(Calendar.MINUTE);
EditText time=(EditText) findViewById(R.id.TimeEdT);
time.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
TimePickerDialog timePickerDialog=new TimePickerDialog(MainActivity.this, new TimePickerDialog.OnTimeSetListener() {
#Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
time.setText(hourOfDay+":"+minute);
}
},hour, minutes1, true
);
timePickerDialog.setTitle("Select Time");
timePickerDialog.show();
}
});
the dialog is not appear when i tried to run the application.
my goal is to take the time from the user and use it as input to text, then use it for alarm notification.
please help
Your time View is an EditText.
When you click in an EditText it puts the cursor within the text allowing you to edit it, bringing up a soft keyboard if required.
It seems you want to get rid of that functionality and bring up a TimePickerDialog instead when you click on it. So if you don't need that extra functionality, just use a TextView instead.

Lollipop CalenderView DatePicker doesn't call OnDateChanged() method

I'm working with a DatePicker and finding that under Android 5.0 it will not call the OnDateChanged() method in its OnDateChangedListener when it's in CalendarView mode even though a new date has been selected. If android:datePickerMode="spinner" is set in the DatePicker's xml tag, the DatePicker will appear as spinners and it will call OnDateChanged() when a new date is selected. In earlier versions of Android, a DatePicker calls OnDateChanged() when a new date is selected in both CalendarView and Spinners versions. Here's the relevant code:
#SuppressLint("InflateParams")
View v = getActivity().getLayoutInflater().inflate(R.layout.dialog_date, null);
DatePicker datePicker = (DatePicker) v.findViewById(R.id.dialog_date_DatePicker);
datePicker.init(year, month, day, new DatePicker.OnDateChangedListener() {
#Override
public void onDateChanged(DatePicker view, int year, int month, int day) {
//Translate year, month, day into a Date object using a calendar
mDate = new GregorianCalendar(year, month, day).getTime();
//Update argument to preserve selected value on rotation
getArguments().putSerializable(EXTRA_DATE, mDate);
}
});
In my application, onDateChanged() doesn't get called and mDate doesn't get changed if the DatePicker is in CalendarView mode under Lollipop, but OnDateChanged() does get called and mDate does change if the DatePicker is in Spinners mode. Under earlier versions of Android, OnDateChanged() gets called and mDate gets changed in both versions of the DatePicker.
Is there any way to get a CalendarView DatePicker in 5.0 to call OnDateChanged()? Failing that, how else can I retrieve a changed date from the DatePicker when it's in CalendarView mode?
I face the same issue and the thing is onDateChange() and onTimeSet() listeners for DatePicker and TimePicker is not called in Nexus devices with lollipop Update.
The reason is in nexus devices, since the clock app is updated, the listeners are not working.
The work around is once the dialog dismiss, you need to create you own listener and set the values in a calendar object using the datepicker get() methods and pass the calendar to the listener.
A simple sample code is
/**
* Returns the calendar instance once the date and time is set.
* #return
*/
public Calendar getDateTime() {
mCalendar.set(datePicker.getYear(),
datePicker.getMonth(),
datePicker.getDayOfMonth(),
timePicker.getCurrentHour(),
timePicker.getCurrentMinute());
return mCalendar;
}
Set this in your Xml-Layout:
<DatePicker
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:datePickerMode="spinner"
android:spinnersShown="false"
android:id="#+id/datepicker_popupwindow"/>
With "android:spinnersShown="false" " you tell it to not show the Spinner. But it will call the 'onDateChanged' Method.

keypad appears first time , then second time when clicked timepicker dialog appears android

When user clicks the edtText the first time the keypad appears, the user needs to click another time so the dialog appears. Even while debugging it reaches the show method of the timePickerDialog, but still on the first time the keypad appears, on the second click the dialog appears
public class NewAssignment extends FragmentActivity implements DatePickerDialog.OnDateSetListener,
TimePickerDialog.OnTimeSetListener
{
EditText timePicker;
timePicker = (EditText) findViewById(R.id.timePicker);
timePicker.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
showTimePickerDialog();
}
});
public void showTimePickerDialog()
{
final Calendar c = Calendar.getInstance();
int hourOfDay = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
TimePickerDialog td = new TimePickerDialog(this, this, hourOfDay, minute, DateFormat.is24HourFormat(this));
td.show();
}
}
i encountered the same problem, while implementing time and date pickers. following code did it for me
timePicker.setInputType(InputType.TYPE_NULL);
but then you would have to click twice to make the time picker dialog appear. so add
timePicker.setFocusable(false);
and everything should work as expected.

need Date Picker Set or cancel listener in Android

i need DatePicker dialog cancel button or set button event listener so that i can move to specific activity by pressing any button
any help will be welcomed.
public void onDateSet(DatePicker view, int year, int month, int day) {
}
When Click on set or Done (in some later OS) this Method is called and simply worked with it to handle my scenario

Manual input not saved in Android's DatePicker(Dialog)

Implementing the DatePicker or DatePickerDialog in Android is easy. But when it comes to data storage, I have a problem with those classes:
If you use the spinners (+ or - button) to change the date, everything works fine. The event "Date changed" or "Date set" is called and you can get the values that the user entered.
But when the year is manually entered into the input field (via keyboard) and the user then clicks "Save" in the dialog, there won't be any event called and you won't get that manually entered value.
It only works when the user changes something with the sliders again after manually entering the year. Because when you use the sliders, the events are fired.
Is this normal behaviour? How can I achieve the desired behaviour, namely that an event is fired when the user enteres something manually and then clicks "Save"?
Thanks in advance!
Just clear focus, and android will set the number from manual input.
eg:
DatePicker datePicker = findViewById(R.id.dp);
When saving just like onClick(), add
datePicker.clearFocus();
This must be working.
I had the same problem and the accepted answer really helped me a lot. My situation is a little different though as I'm using DatePickerDialog. Here's how the DatePicker properly worked finally.
Declare the variables first and then define them.
private DatePickerDialog.OnDateSetListener date;
private DatePickerDialog mDatePickerDialog;
private Calendar myCalendar;
// Get the calendar instance
myCalendar = Calendar.getInstance();
// Define the date set listener first.
date = new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// Do something with year, monthOfYear and dayOfMonth
}
};
// Now define the DatePickerDialog
mDatePickerDialog = new DatePickerDialog(context, date, myCalendar.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH), myCalendar.get(Calendar.DAY_OF_MONTH));
mDatePickerDialog.setButton(DialogInterface.BUTTON_POSITIVE, "Set", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
DatePicker datePicker = mDatePickerDialog.getDatePicker();
// The following clear focus did the trick of saving the date while the date is put manually by the edit text.
datePicker.clearFocus();
date.onDateSet(datePicker, datePicker.getYear(), datePicker.getMonth(), datePicker.getDayOfMonth());
}
});
Then inside an onClick of a button
mDatePickerDialog.show();
If you look at the Date Picker Example, the DatePickerDialog.OnDateSetListener callback is received as soon as the user clicks the 'SET' button on the dialog.
Look at the dialog below
Even if you enter the date using the keyboard, the date itself is not accepted until you click the 'SET" button in the dialog and that is when the DatePickerDialog.OnDateSetListener callback is called.

Categories

Resources