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

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.

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.

Android : How to show DatePicker in onClick EditText - without editable [duplicate]

This question already has answers here:
Datepicker: How to popup datepicker when click on edittext
(32 answers)
Closed 1 year ago.
In my application I'am just calling DatePicker when i click the EditText.
But when I test this, first it ask us to edit and if I again click that then it will show the DatePicker.
I want to show only DatePicker without editable.
put this 2 lines in your edittext xml code
android:focusable="false"
android:focusableInTouchMode="false"
try this. by adding these lines to edittext
android:inputType="none"
android:clickable="true"
android:focusable="false"
and in onclick do wat u want.
private DatePickerDialog.OnDateSetListener pickerListener =
new DatePickerDialog.OnDateSetListener() {
// when dialog box is closed, below method will be called.
#Override
public void onDateSet(DatePicker view, int selectedYear,
int selectedMonth, int selectedDay) {
year = selectedYear;
month = selectedMonth;
day = selectedDay;
ArrivalDate = new Date(year - 1900, month, day);
textArrivalDate.setText(new StringBuilder().append(month + 1)
.append("/").append(day).append("/").append(year)
.append(""));
}
};
Use the following code block with on TextView Click event:
Calendar c = Calendar.getInstance();
datePickerDialog = new DatePickerDialog(this, pickerListener,
c.get(Calendar.YEAR), c.get(Calendar.MONTH),
c.get(Calendar.DAY_OF_MONTH));
datePickerDialog.SetTitle("Select date");
datePickerDialog.show();
Don't use an EditText then.
An EditText is meant to let the user input text using a keyboard...
Use a clickable TextView, or a kind of Button instead.
simplest way to do this use setOnFocusChangeListener instead of OnCLickListner following code is an example which i am using for same purpose.
editText_dob.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus){
hideSoftKeyboard(RegisterationActivity.this);
editText_dob.setRawInputType(InputType.TYPE_CLASS_TEXT);
setDate(editText_dob);
}
}
});

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

Android Date Picker help!

i need a little help here.
In my app a user need`s select one date interval (when press one button), and i'm not know how to do this using date picker!
i'm following this example: http://developer.android.com/resources/tutorials/views/hello-datepicker.html , but i have two questions about this tutorial...
1) How can i do which onClick event called DatePickerDialog.OnDateSetListener ( in the case where i have two EditText that when user click in wich one, the app shows the Date picker Dialog)
2) How can i call the onClick two times after user press one button?
tnhx!
Sorry for poor English!
So from your explanation, you want to create a date interval picker from just one click on a button.
By using analogy from your example you have to do the following:
// create two constants
static final int DATE_DIALOG_FROM = 0;
static final int DATE_DIALOG_TO = 1;
// create case for two pickers
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_FROM:
return new DatePickerDialog(this, fromDateSetListener, mYear, mMonth,mDay);
case DATE_DIALOG_TO:
return new DatePickerDialog(this, toDateSetListener, mYear, mMonth,mDay);
}
return null;
}
// create two listeners for both of the cases
private DatePickerDialog.OnDateSetListener fromDateSetListener = new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
Toast.makeText(AccountsActivity.this,"From:" + year+"."+monthOfYear+"."+dayOfMonth, Toast.LENGTH_SHORT).show();
showDialog(DATE_DIALOG_TO);
}
};
private DatePickerDialog.OnDateSetListener toDateSetListener = new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear,int dayOfMonth) {
Toast.makeText(AccountsActivity.this,"To:" + year+"."+monthOfYear+"."+dayOfMonth, Toast.LENGTH_SHORT).show();
updateDisplay();
}
};
When you run your app, second picker will appear right after finishing with first.
Later you can optimize this code, add custom pickers etc...
If anyone knows a better way to create a DATE INTERVAL PICKER please let us know!

How to create input dialog or date picker on map view?

I created an activity which is show google map on the screen, and consists of 3 option menus to interact with user selection.
When user press on the sub menu I want to show pop-up input dialog box that user can enter date(dd/mm/yyyy) or pop-up date picker. I will use that date to query the location which kept at that date then mark on the map.
My problem is when I tried to created input dialog or whatever inside my sub menu, nothing show on screen except map view. I think it may have something special to do like this. So, I searched for many days, but still do not get an answer.
Any one have an idea or did this ?
Please, suggest me. This is my first time with google map. Appreciate every answer.
Your problem is not specifically with the MapActivity. I have an app that has this feature (a MapActivity subclass that shows a date picker) and haven't had any problems. I suspect the issue lies with how you are showing your date picker. Perhaps try it in an plain activity (ie one that doesn't derive from MapActivity) to confirm this.
So in your class that extends the MapActivity you will need something like:
// the id for your dialog
static final int DATE_DIALOG_ID = 0;
// the date from the picker
Date mDate;
// so you are defining a member variable mDateSetListener which implements
// onDateSet. This gets called when the user selects a date.
private DatePickerDialog.OnDateSetListener mDateSetListener =
new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
Calendar cal = GregorianCalendar.getInstance();
cal.set (year, monthOfYear, dayOfMonth);
// store the date that was picked
mDate = cal.getTime();
// update your map
// ...
}
};
#Override
protected android.app.Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
return new DatePickerDialog(this,
mDateSetListener,
year, month, day);
}
return null;
}
#Override public boolean onMenuItemSelected(int featureId, MenuItem item)
{
switch(item.getItemId())
{
// handle your date selection menu item
case R.id.date:
showDialog(DATE_DIALOG_ID);
break;
}
}

Categories

Resources