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

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);
}
}
});

Related

DatePicker selects date before minDate

Updates:
DatePickerDialog datePickerDialog = new DatePickerDialog(getContext(), new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
// do some stuff
}
}, year, month, day);
DatePicker datePicker = datePickerDialog.getDatePicker();
datePicker.updateDate(2018, 2, 7);
datePicker.setMinDate(1272920400000L); //milliseconds since epoch for 2010.04.10
datePickerDialog.show();
The problem happens inside any DatePicker object, it's not because of my code. You just go inside a DatePickerDialog and try to replicate.
I have a DatePicker and I've set minDate to be 4th of May 2010.
But when you select, for example, 1st May 2011, and then click left arrow until you reach May 2010, it will be automatically selecte 1st May 2010, which is unavailable. Then, OK button will work.
What do you think about this? Is there a way to solve it? I've searched all the Internet and didn't find anything. Thank you!

Show 1980 as initial Year in Date Picker

How to show 1980 as initial Year in DatePicker dialog, still i am getting initial date as current date
see my code below, and let me know where i have to do changes in my code
#Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DIALOG_DATE:
return new DatePickerDialog(this, new OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
dateTime.set(year, monthOfYear, dayOfMonth);
editDate.setText(dateFormatter
.format(dateTime.getTime()));
}
}, dateTime.get(Calendar.YEAR),
dateTime.get(Calendar.MONTH),
dateTime.get(Calendar.DAY_OF_MONTH));
}
return null;
That is because the last 3 parameters on the DatePickerDialog constructor are the date to be shown initially in the DatePicker. You are setting the dateTime variable to the current day with
private Calendar dateTime = Calendar.getInstance();
So you must set your dateTime variable to the correct date you want to be shown on the first run. In your OnCreate() method do this
dateTime.set(1980, Calendar.JANUARY , 1);
Now your DatePicker should show the correct value on the first run and since you set the dateTime variable in the callback it should set the correct previous value if the user opens up the dialog again.

Trying to return a date from android date picker

Over the past 24 hours I have been trying to find articles about my issue but am still confused. If I have missed a place where this question was previously answered I apologize but I hope that you all will still help me understand what I am missing.
Synopsis: I want to populate a few text boxes with a date picker and am having trouble understanding how to return the selected date so that I can populate the appropriate textbox.
Basically I'm trying to implement a standard date picker targeting android devices >= 4.0.
public static class DatePickerFragment extends DialogFragment
implements DatePickerDialog.OnDateSetListener {
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
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);
// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), this, year, month, day);
}
public void onDateSet(DatePicker view, int year, int month, int day) {
// Do something with the date chosen by the user
}
}
public void showDatePickerDialog(View v) {
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getSupportFragmentManager(), "datePicker");
}
You will notice that this is the code from the android documentation example that can be found here: http://developer.android.com/guide/topics/ui/controls/pickers.html
I am implementing this code in a larger project and the date picker comes up fine, the date is picked fine etc.
What I am having trouble with is getting the date after the user selects it. Basically the user selects the date and then nothing happens. What I want to do is populate a specific text box with the date after selection. This will happen in a few text boxes so I can't just hard code it into the onDateSet function.
My thought was to pass the textBox's id with the creation of the datepicker but I am not completely sure how I would do that.
This is my first android project and focused on comprehension and not simply getting a working project, thanks.
The "onDateSet" method should be called when the user selects a date.
In this method, you can use the year, month and day passed in parameter.
I don't known if that is the best way to do it, but I solve it this way:
I have two buttons, A and B.
When i click button A, a DatePicker appears and the user selects the date.
After confirm the date, the text in Button A is set to that date that was selected.
Same behavior for button B.
That is the function when button A or B is pressed:
public void selectDateButtons(View view){
if(view.getId() == R.id.buttonA)
{
clickedButton = R.id.buttonA;
}else{
clickedButton = R.id.buttonB;
}
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getSupportFragmentManager(), "datePicker");
}
clickedButton is a Static global variable.
Then declare the sub-class
public static class DatePickerFragment extends DialogFragment
implements DatePickerDialog.OnDateSetListener {
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
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(getActivity(), this, year, month, day);
}
#Override
public void onDateSet(DatePicker view, int year, int month, int day) {
if(clickedButton == R.id.ButtonA)
{
ButtonA.setText(String.valueOf(day) + "/"
+ String.valueOf(month + 1) + "/" + String.valueOf(year));
} else {
ButtonB.setText(String.valueOf(day) + "/"
+ String.valueOf(month + 1) + "/" + String.valueOf(year));
}
}
}
Don't forget, your main activity should extends FragmentActivity.
Hope that works for you too!
Best regards!

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.

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!

Categories

Resources