I am currently working on a school project, and in the school project, I need to add a DatePickerDialog in the register page to allow the user to pick their birth date.
I wanted to add a .setMaxDate() property to only allow users above a certain age to sign up for the app. When I added the .setMaxDate() property, the DatePickerDialog stopped showing the picked date as intended.
Here's my code to give you some context:
//[Inside of onCreate()]
final Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, 16-calendar.get(Calendar.YEAR));
datePickerDialog = new DatePickerDialog(this, dateSetListener, Calendar.YEAR, Calendar.MONTH, Calendar.DAY_OF_MONTH);
datePickerDialog.getDatePicker().setMaxDate(calendar.getTimeInMillis());
//[Inside of onCreate()] OnClick to show dialog
datePicker.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
datePickerDialog.show();
}
});
//[Outside of onCreate()]
public DatePickerDialog.OnDateSetListener dateSetListener = new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int month, int day) {
age = new Date(year-1900, month, day);
datePicker.setText(sdf.format(age));
calendarError.setError(null);
}
};
It's important to note that the DatePickerDialog works just fine without this line:
datePickerDialog.getDatePicker().setMaxDate(calendar.getTimeInMillis());
, but setting a max date is a necessity. This is what the DatePickerDialog shows instead of what's intended:
As you can see, the date shown as picked and the actual date selected by the user and saved in a Date object later are different. It's worth noting that I can't show this in a screenshot, but choosing a different year by clicking the top row and changing a year, or clicking on a different date snaps the DatePickerDialog to the current date but 16 years ago, yet it still saves them as the selected date by clicking the OK option.
Related
I need to change default text values on timepicker in android (java) and can't figure out how to do that.
I've found .setTitle and .setmessage, but when clicking on keyborad icon in Timepicker dialog:
and shows another way to change time it has another title and default message as instructions that I would like to change Second timepicker dialog output
.
I'm calling a simple TimePickerDialog. Any suggestions? regards
You can check this out. This works for me good.
#Subscribe
public void setAlarm(OnAlarmClickEvent event) {
arrivalTime = event.arrivalTime;
alarmTime = event.arrivalTime;
int reminder = Integer.parseInt(sharedPreferences.getString(getString(R.string.pref_key_alarm_reminder), "5"));
alarmTime = alarmTime.minusMinutes(reminder);
TimePickerDialog timePickerDialog = new TimePickerDialog(
this,
this,
alarmTime.getHourOfDay(),
alarmTime.getMinuteOfHour(),
DateFormat.is24HourFormat(this)
);
timePickerDialog.setTitle(R.string.alarm_dialog_title);
timePickerDialog.setMessage(getString(R.string.alarm_dialog_message_fmt, DateUtils.formatTime(arrivalTime)));
timePickerDialog.show();
}
I'd like to have a date picker dialog with a "Today" button. If I click on Today button, it should reset datepicker to current date, without closing the dialog.
I have the following code:
Calendar today = Calendar.getInstance();
DatePickerDialog datePickerDialog = new DatePickerDialog(context, R.style.MyDialogStyle, date, myCalendar.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH), myCalendar.get(Calendar.DAY_OF_MONTH));
datePickerDialog.setButton(DialogInterface.BUTTON_NEUTRAL, "Today", (dialog, which) -> {
datePickerDialog.updateDate(today.get(Calendar.YEAR), today.get(Calendar.MONTH), today.get(Calendar.DAY_OF_MONTH));
});
datePickerDialog.show();
Expected behavior: set datepicker's date to current date without closing the dialog.
Actual behavior: dialog closes, date doesn't change.
I also tried with new DatePickerDialog(...), and
datePickerDialog.getDatePicker().init(today.get(Calendar.YEAR), today.get(Calendar.MONTH), today.get(Calendar.DAY_OF_MONTH), null);
but it still doesn't work, only closes the dialog.
What am I missing here?
Is there a way to update datepicker's date programmatically without closing the dialog?
By default click button calls also dismiss() method to close the DatePickerDialog. It applies also to the DialogInterface.BUTTON_NEUTRAL button which you use for 'Today'.
Probably the simplest way to avoid this is to override the DatePickerDialog.dismiss() method and skip calling super.dismiss() if the 'Today' button was clicked.
Here is my working code in Kotlin I use for the same purpose (Today button for the DatePickerDialog). I use skipDismiss flag to know when to skip dismissal.
val datePickerDialog = object : DatePickerDialog(
this#MainActivity,
settings.currentTheme,
DatePickerDialog.OnDateSetListener { _, chosenYear, chosenMonth, dayOfMonth ->
calendar.set(Calendar.YEAR, chosenYear)
calendar.set(Calendar.MONTH, chosenMonth)
calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth)
text_view_chosen_date.text = SimpleDateFormat("dd/MM/yyyy", Locale.US).format(calendar.time)
},
calendar.get(Calendar.YEAR),
calendar.get(Calendar.MONTH),
calendar.get(Calendar.DAY_OF_MONTH)
) {
override fun dismiss() {
if (!skipDismiss) {
super.dismiss()
} else {
skipDismiss = false
}
}
}
And of course the 'Today' button listener should set the skipDismiss flag to true when called. Here is my code for that:
datePickerDialog.setButton(DialogInterface.BUTTON_NEUTRAL, "Today") { dialog, which ->
val today = Calendar.getInstance()
(dialog as DatePickerDialog).updateDate(
today.get(Calendar.YEAR),
today.get(Calendar.MONTH),
today.get(Calendar.DAY_OF_MONTH)
)
skipDismiss = true;
}
I'm using borax12 materialdaterangepicker and want to know how I can change the colours to customise the dialog calendar and also when the user selects a date change to the To tab?
Code:
// Show a datepicker when the dateButton is clicked
dateButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
to = Calendar.getInstance();
DatePickerDialog dpd = com.borax12.materialdaterangepicker.date.DatePickerDialog.newInstance(
Tab2.this,
to.get(Calendar.YEAR),
to.get(Calendar.MONTH),
to.get(Calendar.DAY_OF_MONTH)
);
dpd.setAutoHighlight(mAutoHighlight);
dpd.show(getActivity().getFragmentManager(), "Datepickerdialog");
dpd.setYearRange(2016, 2017);
dpd.isThemeDark();
}
});
return v;
}
1). Change colours, background where the green is and maybe the tabs where the FROM and TO are and also the text. I can probably work all this out if I know how to start customising it.
2) When the user selects a day on the calender then switch to the To tab.
Can I display a toast message based on user's current time?
I need to make an app, Where when I open the screen from 1:00 am to 11:00 am it will display the toast message "Good morning" whereas if I open the screen at 6:00 pm to 11:00 pm it will display the toast "good night"
public class ToastDisplay extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_USER_PRESENT)){
Toast.makeText(context,"Good Morning",Toast.LENGTH_SHORT).show();
}
}
}
What you can do i use Calendar class to get the current time. Check the time and display the toast accordingly.
Calendar currentTime = Calender.getInstance();
int hour = currentTime.get(Calendar.HOUR_OF_DAY);
if(Use your condition here) {
Display toast
} else if(Use your other condition) {
Display toast
}
Just a note, your hour would be in 24 hour format so you need to check keeping that in mind.
int toastDurationInMilliSeconds = 10000;
Toast.LENGTH_SHORT is int, so you have to declare new int
I am trying to build an android app and have come across a situation where I want a user to navigate between Dates by clicking Previous (Month) and Next (Month) buttons. I however do not want the user to go to a month after the current month. I would like the App to prompt a dialog letting the user know that they cant select a month after the current. Below is my code snippet - I am getting an error on two portions
01. if(formattedDate.before(currentMonth) || formattedDate.equals(currentMonth)){
On the above I am getting an error saying
The method before(Calendar) is undefined for the type String
02. new AlertDialog.Builder(HomeFragment.this)
and on the above I am getting an error saying
The constructor AlertDialog.Builder(HomeFragment) is undefined
Below is my full code for the on-click listener
NextPicker.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(formattedDate.before(currentMonth) || formattedDate.equals(currentMonth)){
c.add(Calendar.MONTH, 1);
formattedDate = df.format(c.getTime());
Log.v("NEXT DATE : ", formattedDate);
DatePicker.setText(formattedDate);
}
else{
new AlertDialog.Builder(HomeFragment.this)
.setTitle("Wrong Date Selection!")
.setMessage("The Month Selected must be before or equal to current month")
.setNeutralButton("Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
}
}).show();
}
}
});
Your first problem is that formattedDate is a String. It needs to be a java.util.Date.
Your second problem comes from the fact that the AlertDialog.Builder constructor requires a Context sub-class. Fragment is not a Context sub-class, but Activity is. Try something like
new AlertDialog.Builder(HomeFragment.this.getActivity());