datepicker disabling previous date - java

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

Related

how to set limit of calendar to some date and increase and decrease date on click of a button to same limit in android

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

DatePicker: Listener reacting when clicking beside the Button

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

datepickerdialog theme_holo_light not displayed correctly in Samsung J7 Core

I use java code to display datepickerdialog with a theme THEME_HOLO_LIGHT.
My compiled SDK version is 27. The theme is displayed perfectly in an oppo device (Nougat 7.0). However, in samsung J7 Core, the output is a different THEME.
Sample output of THEME_HOLO_LIGHT
The result of an output in Samsung is THEME_DEVICE_DEFAULT_DARK.
I want the THEME_HOLO_LIGHT to display. Please help.
datePickerDialog = new DatePickerDialog(this,AlertDialog.THEME_HOLO_LIGHT, new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
USERBIRTHDATE = dateFormat.format(new Date(year - 1900, month, dayOfMonth));
dateTxt.setText(USERBIRTHDATE);
flag = true;
}
}, 1997, 0, 01);
Instead of AlertDialog.THEME_HOLO_LIGHT write android.R.style.Theme_Holo_Light.
If this doesn't work, replace it with this:
datePickerDialog = new DatePickerDialog(this,new ContextThemeWrapper(this, android.R.style.Theme_Holo_Light), new DatePickerDialog.OnDateSetListener() {..}

How to show timepickerdialog with 12h AM/PM

I'm learning Android so I made an "app" but when the app open the timepickerdialog it shows a 24h clock. I need to show 12h AM/PM.
I read that I need to use the is24HourView, but I dont know how and where. Can you show me where? Here is part of the code.
private void setDateTimeField() {
theHour.setOnClickListener(this);
Calendar newCalendar = Calendar.getInstance();
theHourPickerDialog = new TimePickerDialog(this, new OnTimeSetListener() {
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
Calendar theTime = Calendar.getInstance();
theTime.set(Calendar.HOUR_OF_DAY, hourOfDay);
theTime.set(Calendar.MINUTE, minute);
theHour.setText(timeFormatter.format(theTime.getTime()));
}
}, newCalendar.get(Calendar.HOUR_OF_DAY), newCalendar.get(Calendar.MINUTE),true);
}
I tried to put theHourPickerDialog.is24HourView(false) but it did not work XD
Thanks!
Notice that you should call setIs24HourView(false) before you set time on here.

How to create a textedit for date only programatically in android?

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.

Categories

Resources