I want to create a textedit field where the user enters a date only (no time). The date will get stored in MY SQL. Whats the best way to do this with the least amount of validation? Is there like a built in textfield for dates that keeps it in the proper format?
I have this:
public static void AddEditTextDate(Context context, LinearLayout linearlayout, String text, int id) {
EditText edittext = new EditText(context);
edittext.setInputType(InputType.TYPE_DATETIME_VARIATION_DATE);
edittext.setText(text);
edittext.setId(id);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
edittext.setLayoutParams(params);
linearlayout.addView(edittext);
}
But when I try to type in it, it looks like a normal keyboard. I would expect it to go into the number pad by default or something...
EDIT: It needs to work with android 2.1+ (i.e. v7)
Does anyone know?
Thanks
You said Whats the best way to do this with the least amount of validation? Is there like a built in textfield for dates that keeps it in the proper format?
There is one way which comes to my mind, using which you might not need to check any validation of the date format that user entered. You can call a DatePickerDialog on click of your EditText box. Then user can choose the date using that. After user has chosen the date you can update your EditText with the chosen date. This way you lesses the effort of validation of the date format entered and user can easily and intuitively choose the date. You might so something like:
Calendar myCalendar = Calendar.getInstance();
DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
myCalendar.set(Calendar.YEAR, year);
myCalendar.set(Calendar.MONTH, monthOfYear);
myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
updateLabel();
}
};
//When the editText is clicked then popup the DatePicker dialog to enable user choose the date
edittext.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
new DatePickerDialog(new_split.this, date, myCalendar
.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
myCalendar.get(Calendar.DAY_OF_MONTH)).show();
}
});
// Call this whn the user has chosen the date and set the Date in the EditText in format that you wish
private void updateLabel() {
String myFormat = "MM/dd/yyyy"; //In which you need put here
SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
edittext.setText(sdf.format(myCalendar.getTime()));
}
Source: This answer on Datepicker: How to popup datepicker when click on edittext question. Hope this helps.
Related
i want create such functionality where user can choose date between today's date to 30 days after today's date so i set max date and min date. and plus i want a functionality where i am having two buttons one to increase date and another to decrease date and from those two buttons also user can choose date between today's date to 30 days after today's date but problem is when someone is clicking on button set max date and min date is not working and in date picker dialog first time it is working fine but next time someone is clicking on calendar it is showing 30 days from that date but i don't want that i want to show today's date and 30 days after that date.
to set max date and min date
try {
pickerDialog = com.wdullaer.materialdatetimepicker.date.DatePickerDialog.newInstance(MainActivity.this,
calendar.get(Calendar.YEAR),
calendar.get(Calendar.MONTH),
calendar.get(Calendar.DAY_OF_MONTH));
pickerDialog.setMinDate(Calendar.getInstance());
calendar.add(Calendar.DAY_OF_MONTH, 30);
pickerDialog.setMaxDate(calendar);
pickerDialog.show(getFragmentManager(), "Date Picker Dialog");
} catch (Exception e) {
e.printStackTrace();
}
to increase date and decrease date
ivBack.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
calendarSh.add(Calendar.DAY_OF_MONTH, -1);
formattedDate = simpleDateFormat.format(calendarSh.getTime());
Log.v("PREVIOUS DATE : ", formattedDate);
tvDate.setText(formattedDate);
}
});
ivNext.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
calendarSh.add(Calendar.DAY_OF_MONTH, +1);
formattedDate = simpleDateFormat.format(calendarSh.getTime());
Log.v("NEXT DATE : ", formattedDate);
tvDate.setText(formattedDate);
ivBack.setEnabled(true);
}
});
There are a couple of issues with your code. First, let's consider the date picker. In your code, you are using some variable calendar, which is initialised externally to your date picker intialisation code. When setting up your date picker, you add 30 days to it - but then you are reusing this variable again next time - so adding another 30 days to it - an so on. Instead you should initialise it internally. The code would be something like this:
try {
Calendar newCalendar = Calendar.getInstance();
pickerDialog = com.wdullaer.materialdatetimepicker.date.DatePickerDialog.newInstance(
MainActivity.this,
newCalendar .get(Calendar.YEAR),
newCalendar .get(Calendar.MONTH),
newCalendar .get(Calendar.DAY_OF_MONTH)
);
pickerDialog.setMinDate(newCalendar);
newCalendar.add(Calendar.DAY_OF_MONTH, 30);
pickerDialog.setMaxDate(newCalendar);
pickerDialog.show(getFragmentManager(), "Date Picker Dialog");
} catch (Exception e) {
e.printStackTrace();
}
Now, when you use buttons to increase/decrease dates, your min/max from the date picker do not apply anymore - you have to check against min/max every time. You code could look something like this:
final Calendar minDate = Calendar.getInstance();
final Calendar.maxDate = Calendar.getInstance();
maxDate.add(Calendar.DAY_OF_MONTH, 30);
ivBack.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (calendarSh.after(minDate)) {
calendarSh.add(Calendar.DAY_OF_MONTH, -1);
formattedDate = simpleDateFormat.format(calendarSh.getTime());
Log.v("PREVIOUS DATE : ", formattedDate);
tvDate.setText(formattedDate);
}
}
});
ivNext.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (calendarSh.before(maxDate)) {
calendarSh.add(Calendar.DAY_OF_MONTH, +1);
formattedDate = simpleDateFormat.format(calendarSh.getTime());
Log.v("NEXT DATE : ", formattedDate);
tvDate.setText(formattedDate);
}
}
});
My app uses a DatePicker. When I open the DatePicker and click on Done the date gets choosen. The problem is, that the date also gets choosen, if the user clicks besides the DatePicker. See here:
The Listener should only react if the Done Button is clicked. But it also reacts if the surrounding (red) of the DatePicker is clicked.
My Question:
How do I prevent this?
If you want to see some code, pls leave a comment.
I already tried this: How to handle date picker dialog not to set in edittext when clicked outside of dialog android?
Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
//DatePickerDialog mDatePicker;
DatePickerDialog mDatePicker;
mDatePicker = new DatePickerDialog(this, AlertDialog.THEME_DEVICE_DEFAULT_LIGHT, new DatePickerDialog.OnDateSetListener() {
private boolean fired;
public void resetFired(){
fired = false;
}
#Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
if(view.isShown()){
if(fired){
setDate(year, monthOfYear, dayOfMonth);
//ListView erneuern
adapter.notifyDataSetChanged();
return;
}
fired = true;
}
}
}, mYear, mMonth, mDay);
mDatePicker.setCanceledOnTouchOutside(true);
mDatePicker.setTitle("Wähle ein Datum");
mDatePicker.show();
But it does not work correctly. The click on the background bug gets solved. But now the Done Button stops working.
A simple dialog containing an DatePicker.
So You can set to no-cancelable.
using setCancelable API
.setCancelable(false);
Dialog reference
I want to change my date picker view to standard mode
From
To
My code is
dateOfBirthET = (EditText) findViewById(R.id.dateOfBirth);
//setting dateSetListener
final DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
myCalendar.set(Calendar.YEAR, year);
myCalendar.set(Calendar.MONTH, monthOfYear);
myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
updateLabel();
updateLabelToSave();
}
};
//setting onClickListener on setDate
dateOfBirthET.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
DatePickerDialog dpd = new DatePickerDialog(RegisterActivity.this, date,
myCalendar.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
myCalendar.get(Calendar.DAY_OF_MONTH));
//setting maxDate on tempCal
long maxDate = new Date().getTime();
tempCal.setTimeInMillis(maxDate);
tempCal.set(Calendar.YEAR, tempCal.get(Calendar.YEAR) - 16);
dpd.getDatePicker().setMaxDate(tempCal.getTimeInMillis());
dpd.show();
}
});
}
I am tried this code also but not working
dpd.getDatePicker().setCalendarViewShown(false);
You just need to change theme you want while creating DatePickerDialog instance
DatePickerDialog dpd = new DatePickerDialog(RegisterActivity.this, android.R.style.Theme_Holo_Dialog ,date,
myCalendar.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
myCalendar.get(Calendar.DAY_OF_MONTH));
Working for me tested android 6.0 marshmallow.
From the setCalendarViewShown doc it says
Calling this method has no effect when the DatePicker_datePickerMode
attribute is set to calendar.
And as of Android L, with the Material theme selected, the default layout of android:datePickerMode is calendar (see here)
So if you want to change your DatePickerDialog to spiner style, you must change android:datePickerMode
And now I see datePickerMode can not change programmatically, it can only change by config XML like
<DatePicker
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:datePickerMode="spinner"
/>
Therefore I think you should create a custom your DatePickerDilog layout
For disabling previous date I am doing the following. The previous dates (from current date) are becoming faded. But I can still select them, which I don't want. Any suggestion what to do? I have read previous posts on this topic but looks like no one faced such issue.
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Calendar calendar = Calendar.getInstance();
DatePickerDialog dpd = new DatePickerDialog(getActivity(), this, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
dpd.getDatePicker().setMinDate(calendar.getTimeInMillis());
return dpd;
}
I'm working on an android app. That uses a 'datepicker' as part of a data entry form. I've written a getter method "onDateSet" to return data from the object but i'm unable to call the method from the instance of the class.
line: datePicker.getSelectedDate(); I'm getting a 'method cannot be resolved' error
I get the same message when I try and access the variable directly. datePicker.selecteddate
I would be grateful if someone could point me in the right direction.
public 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);
}
int selectedDate;
public void onDateSet(DatePicker view, int year, int month, int day) {
// Do something with the date chosen by the user
selectedDate = day+month+year;
}
public int getSelectedDate() {
return selectedDate;
}
}
public void showDatePickerDialog(View view) {
DialogFragment datePicker = new DatePickerFragment();
datePicker.show(getFragmentManager(), "datePicker");
int output = datePicker.getSelectedDate();
}
You have:
DialogFragment datePicker = new DatePickerFragment();
You mean:
DatePickerFragment datePicker = new DatePickerFragment();
This is because your methods are members of your subclassed DatePickerFragment, not of the base class DialogFragment.
Essentially, when you refer to an object through its base type, the compiler only knows about the methods the base type declares. It has no way of knowing that an arbitrary DialogFragment is actually a DatePickerFragment (or any other derived type).
An alternative, if you know it is a DatePickerFragment, is to explicitly cast datePicker to a DatePickerFragment:
DialogFragment datePicker = new DatePickerFragment();
...
int output = ((DatePickerFragment)datePicker).getSelectedDate();
This, of course, will fail with a ClassCastException if datePicker isn't actually a DatePickerFragment.