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.
Related
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
}
});
I want a app that sets a alarm according to a time-picker and not a data-picker, i want the alarm to go off every day at the time the time-picker is set at.
I don't want to have a button which becomes a picker i want to use the time-picker icon.
If you want to have a Button without Text, you can use
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/button_icon"
... />
If you want to set up an Alarm create a new class, that shows TimePicker in FragmentDialog
public static class TimePickerFragment extends DialogFragment
implements TimePickerDialog.OnTimeSetListener {
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current time as the default values for the picker
final Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
// Create a new instance of TimePickerDialog and return it
return new TimePickerDialog(getActivity(), this, hour, minute,
DateFormat.is24HourFormat(getActivity()));
}
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
// Here you get the selected Time, do whatever you want
}
}
And in the Activity, where is the Button:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/pick_time"
android:onClick="showTimePickerDialog" />
Set OnclickListener, which starts FragmentDialog with TimePicker
public void showTimePickerDialog(View v) {
DialogFragment newFragment = new TimePickerFragment();
newFragment.show(getSupportFragmentManager(), "timePicker");
}
http://developer.android.com/guide/topics/ui/controls/pickers.html#TimePicker
I dont really understand, what are you actually asking for. Clarify your question please.
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.
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
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.