how to select year in datepickerdialog android? - android

I am new to Android programming. Please help.
I am using Fragment that creates Material design DatePickerDialog on click of EditText. Trouble is it is set to current date (set by me). But, if user has to select the date in the past ... say, 10 years ago, user has to scroll each month which is painful. e.g. shown below:
Is there a way to make your select the year? This way user can navigate to the year.

At least on Android 7, you can click on the year and get a year selection dialog:

I think than that is because you don't have a minimum date or maxim date. If you try to create a custom dialog with DatePicker, it works. I have a same example with an Edittext. I call this method when the user to click in Edittext.
private void showDateDialog() {
mLayoutInflater = getLayoutInflater();
mCustomDatePicker = mLayoutInflater.inflate(R.layout.custom_date_picker, null);
mDatePicker = (DatePicker) mCustomDatePicker.findViewById(R.id.datePicker);
mDatePicker.setMaxDate(Calendar.getInstance().getTime().getTime());
mCalendar = Calendar.getInstance();
mDialog = new AlertDialog.Builder(this);
mDialog.setView(mCustomDatePicker);
mDialog.setPositiveButton(R.string.button_ok, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
mCalendar.set(mDatePicker.getYear(), mDatePicker.getMonth(), mDatePicker.getDayOfMonth());
mBirthdayEdit.setText(mFormatDate.format(mCalendar.getTime()));
}
}).setNegativeButton(R.string.button_cancel, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
mDialog.create();
mDialog.show();
}
You layout
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<DatePicker
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/datePicker"
android:layout_gravity="center_horizontal"
android:spinnersShown="true"
android:calendarViewShown="false"
android:layout_alignParentTop="true"/>
</RelativeLayout>

public void setMaxDate() {
final Calendar currentDate = Calendar.getInstance();
date = Calendar.getInstance();
DatePickerDialog.OnDateSetListener dateSetListener = new
DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker datePicker, int year, int
monthOfYear, int dayOfMonth) {
date.set(year, monthOfYear, dayOfMonth);
//use this date as per your requirement
}
};
DatePickerDialog datePickerDialog = new
DatePickerDialog(EditProfileActivity.this, dateSetListener,
currentDate.get(Calendar.YEAR),
currentDate.get(Calendar.MONTH ),
currentDate.get(Calendar.DAY_OF_MONTH));
// Limiting access to past dates in the step below:
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.YEAR, -14);
datePickerDialog.getDatePicker().setMaxDate(calendar.getTimeInMillis());
datePickerDialog.show();
}
This is working code ,you can set 14 year ago max date
Enjoy...!

Related

Android DatePickerDialog Max Date visual bug

I'm experiencing a weird visual bug on new DatePickerDialog of Android widgets. I need to set a dialog's selectable date range as 1 month but it shouldn't also select a date from the future. Logic works as expected but the visuals have some sort of bug. Here is how it looks:
Note that the "5" is disabled, I cannot select it. However, it looks as selectable. What should I do to solve this issue? Any help is appreciated, thanks. (Today's day of month is also 5, I assume this bug comes from there.)
Edit: This is how I create the dialog.
#SuppressLint("ValidFragment")
public static class DateDialog extends DialogFragment implements DatePickerDialog.OnDateSetListener
{
private static final long MONTH = 2592000000L;
#Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
// Use previous selected date in the date picker
final Calendar c = Calendar.getInstance();
c.setTimeInMillis(DateUtils.getAtTimeTimestamp(startDate));
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
DatePickerDialog dialog = new DatePickerDialog(getActivity(), R.style.AppTheme_DialogTheme, this, year, month, day);
Date date = new Date(); // get current time
// get target maximum date which can be 1 month above
long targetDateTime = DateUtils.getAtTimeTimestamp(startDate) + MONTH;
if (date.getTime() < targetDateTime)
dialog.getDatePicker().setMaxDate(date.getTime()); // now because it can't be future
else
dialog.getDatePicker().setMaxDate(targetDateTime); // should also be one month above from the start date.
dialog.getDatePicker().setMinDate(DateUtils.getAtTimeTimestamp(startDate));
return dialog;
}
#Override
public void onDateSet(DatePicker datePicker, int i, int i1, int i2)
{
// logic between date selections
}
}
DateUtils.getAtTimeTimestamp(String date) basically converts a string to long integer that is time in milliseconds.

DatePickerDialog display bug on Samsung galaxy S7 run Android 7.x

Only Samsung s7 run android 7.x
Here my code
DatePickerDialog datePickerDialog = new DatePickerDialog(activity
,new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year
, int monthOfYear
, int dayOfMonth) {
listener.onDateSet(year, monthOfYear, dayOfMonth);
}
}
,c.get(Calendar.YEAR)
, c.get(Calendar.MONTH)
, c.get(Calendar.DAY_OF_MONTH));
int firstDayOfWeek = TimeUtils.getFirstDayOfWeek(activity);
datePickerDialog.getDatePicker().setFirstDayOfWeek(firstDayOfWeek);
datePickerDialog.show();
Someone advise me how to solve this problem?
Thanks!
You don't set custom style for dialog so this normal(default style). If you want "fix" this "bug" you must create custom style dialog.
For me works fine on the same device, I extend DialogFrament in my app and use code like this:
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
}
}
and then use this method on the view I need to open the dialog:
public void showDatePickerDialog(View v) {
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getSupportFragmentManager(), "datePicker");
}
Try to implement this or follow the guide on the developer site:
https://developer.android.com/guide/topics/ui/controls/pickers.html
The problem is that the actual implementation of DatePickerDialog's UI is device specific and it can have some issues.
I've got similar issues on devices with the screen width smaller than 360dp. Fixed this by using THEME_HOLO_LIGHT theme.
DatePickerDialog has a constructor which accepts a theme:
DatePickerDialog(Context context, int theme, DatePickerDialog.OnDateSetListener callBack, int year, int monthOfYear, int dayOfMonth)
You may use one of the following:
AlertDialog.THEME_DEVICE_DEFAULT_DARK
AlertDialog.THEME_DEVICE_DEFAULT_LIGHT
AlertDialog.THEME_HOLO_DARK
AlertDialog.THEME_HOLO_LIGHT
AlertDialog.THEME_TRADITIONAL

How to get both date and time in a single dialog box

I am using following code to pick date, however i wanted to pick the date as well as time (in a single dialog box)...
DatePickerDialog.OnDateSetListener beginDate = new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
c.set(Calendar.YEAR,year);
c.set(Calendar.MONTH,monthOfYear);
c.set(Calendar.DAY_OF_MONTH,dayOfMonth);
setBeginDateOnView();
}
};
public void beginDateOnClick(View view) {
new DatePickerDialog(CreateEventActivity.this,beginDate,c.get(Calendar.YEAR),c.get(Calendar.MONTH),c.get(Calendar.DAY_OF_MONTH)).show();
}
public void setBeginDateOnView() {
String dateFormat = "dd-MM-yyyy";
SimpleDateFormat sdf = new SimpleDateFormat(dateFormat, Locale.getDefault());
edtBeginDate.setText(sdf.format(c.getTime()));
There is no built-in "date and time picker in one" solution for Android, so you'll have to combine a DatePicker and a TimePicker into a dialog by yourself, or use a library that does it.
For a library, i can suggest using SlideDateTimePicker, i used it in a project a while back and it works well.
Or you can write your own, by simply creating a new class that extends AlertDialog, setting up 2 tabs inside it and then setting the tabs to include a DatePicker and a TimePicker.

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!

DatePicker date set in android

I need a date-picker dialog in my application. First, I open the date picker dialog box and I select the date. The next time whenever open Date pickerDialog, the date of date picker should be current date of device. However, it shows last selected date in dialog. Please help me to set the code. Here is my code as I have it now:
#Override
protected Dialog onCreateDialog(int id)
{
switch (id)
{
case DATE_DIALOG_ID:
Date d=new Date();
Calendar c=Calendar.getInstance();
c.setTimeZone(tz);
c.setTime(d);
d=c.getTime();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int date = c.get(Calendar.DATE);
DatePickerDialog datePicker
= new DatePickerDialog(this, datePickerListener, year, month, date);
return datePicker;
}
return super.onCreateDialog(id);
}
private DatePickerDialog.OnDateSetListener datePickerListener
= new DatePickerDialog.OnDateSetListener()
{
// when dialog box is closed, below method will be called.
public void onDateSet(DatePicker view, int selectedYear, int selectedMonth, int selectedDay)
{
cors_year = selectedYear;
cors_month = selectedMonth;
cors_date = selectedDay;
getDate("calledfromDialog");
displaySunTime();
displayChoghadiya("Day");
displayTime(3, 1);
dayButtonClicked();
selected_DAY_NIGHT="Day";
}
};
Note that onCreateDialog was deprecated in v13, per the documentation:
Use the new DialogFragment class with FragmentManager instead; this is also available on older platforms through the Android compatibility package.
Which you would use along with this tutorial
However, if you want to stay with the DatePickerDialog, you can use code such as:
protected void onPrepareDialog (int id, Dialog dialog)
{
DatePickerDialog datePickerDialog = (DatePickerDialog) dialog;
// Get the current date
datePickerDialog.updateDate(year, month, day);
}
This is because Android only calls onCreateDialog once per dialog to re-use the dialog. onPrepareDialog is called in order for you to set the state of the Dialog appropriately before it is shown.

Categories

Resources