Android Datepicker Change to Only Month and Year

Android DatePicker change to only Month and Year

Try the following code. It will show a DatePicker with only the year and month (without day)

private DatePickerDialog createDialogWithoutDateField() {
DatePickerDialog dpd = new DatePickerDialog(this, null, 2014, 1, 24);
try {
java.lang.reflect.Field[] datePickerDialogFields = dpd.getClass().getDeclaredFields();
for (java.lang.reflect.Field datePickerDialogField : datePickerDialogFields) {
if (datePickerDialogField.getName().equals("mDatePicker")) {
datePickerDialogField.setAccessible(true);
DatePicker datePicker = (DatePicker) datePickerDialogField.get(dpd);
java.lang.reflect.Field[] datePickerFields = datePickerDialogField.getType().getDeclaredFields();
for (java.lang.reflect.Field datePickerField : datePickerFields) {
Log.i("test", datePickerField.getName());
if ("mDaySpinner".equals(datePickerField.getName())) {
datePickerField.setAccessible(true);
Object dayPicker = datePickerField.get(datePicker);
((View) dayPicker).setVisibility(View.GONE);
}
}
}
}
}
catch (Exception ex) {
}
return dpd;
}

This method returns a date picker dialog. So , in your button's onClick method add the following code to display your dialog.

createDialogWithoutDateField().show();

Select month and year range (without days) Android

See this question:
Android DatePicker change to only Month and Year

I would post that in comments, but I don't have permission to do so.

There is more options than in Pratik Kate's answer.

edit: I know those solutions do not offer range selection, but I think that could be easily solved using two pickers - 'from-to'.

See the screenshots e.g. in this project: Material Date and Time Picker with Range Selection

I think it can look pretty good. I can imagine many users would be confused when asked to select range using only one picker (having to use some sort of long-press or drag event?).

How to show only current month and previews month's dates selectable in datePickerDialog?

Try this solution. It's from first day of previous month to this day.

In Kotlin:

private fun setupUi() {
// Get calendar instance
val calendar = Calendar.getInstance()

// Get current time
val currentYear = calendar.get(Calendar.YEAR)
val currentMonth = calendar.get(Calendar.MONTH)
val currentDay = calendar.get(Calendar.DAY_OF_MONTH)

// Create listener
val listener = DatePickerDialog.OnDateSetListener { view, year, month, day ->
// Show Toast after selection
Toast.makeText(this, "Selected: $day.$month.$year", Toast.LENGTH_SHORT).show()
}

// Max = current
val maxTime = calendar.timeInMillis

// Move day as first day of the month
calendar.set(Calendar.DAY_OF_MONTH, 1)
// Move "month" for previous one
calendar.add(Calendar.MONTH, -1)

// Min = time after changes
val minTime = calendar.timeInMillis

// Create dialog
val datePickerDialog = DatePickerDialog(
this,
listener,
currentYear,
currentMonth,
currentDay
).apply {
// Set dates
datePicker.maxDate = maxTime
datePicker.minDate = minTime
}

// Show dialog
datePickerDialog.show()
}

And in Java:

private void setupUi() {
// Get calendar instance
Calendar calendar = Calendar.getInstance();

// Get current time
int currentYear = calendar.get(Calendar.YEAR);
int currentMonth = calendar.get(Calendar.MONTH);
int currentDay = calendar.get(Calendar.DAY_OF_MONTH);

// Create listener
DatePickerDialog.OnDateSetListener listener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int day) {
// Show Toast after selection
Toast.makeText(MainActivity.this, String.format("Selected: %s.%s.%s", day, month, year), Toast.LENGTH_SHORT).show();
}
};

// Max = current
long maxTime = calendar.getTimeInMillis();

// Move day as first day of the month
calendar.set(Calendar.DAY_OF_MONTH, 1);
// Move "month" for previous one
calendar.add(Calendar.MONTH, -1);

// Min = time after changes
long minTime = calendar.getTimeInMillis();

// Create dialog
DatePickerDialog datePickerDialog = new DatePickerDialog(
this,
listener,
currentYear,
currentMonth,
currentDay
);

// Set dates
datePickerDialog.getDatePicker().setMaxDate(maxTime);
datePickerDialog.getDatePicker().setMinDate(minTime);

// Show dialog
datePickerDialog.show();
}

If you need WHOLE current month (from first to last day) you can replace par with this Kotlin code:

// Move day as first day of the month
calendar.set(Calendar.DAY_OF_MONTH, 1)
// Move "month" for previous one
calendar.add(Calendar.MONTH, -1)

// Min = time after changes
val minTime = calendar.timeInMillis

// Move day as first day of the month
calendar.set(Calendar.DAY_OF_MONTH, 1)
// Move to next month
calendar.add(Calendar.MONTH, +2)
// Go back one day (so last day of current month)
calendar.add(Calendar.DAY_OF_MONTH, -1)

// Max = current
val maxTime = calendar.timeInMillis

and in Java:

// Move day as first day of the month
calendar.set(Calendar.DAY_OF_MONTH, 1);
// Move "month" for previous one
calendar.add(Calendar.MONTH, -1);

// Min = time after changes
long minTime = calendar.getTimeInMillis();

// Move day as first day of the month
calendar.set(Calendar.DAY_OF_MONTH, 1);
// Move to next month
calendar.add(Calendar.MONTH, +2);
// Go back one day (so last day of current month)
calendar.add(Calendar.DAY_OF_MONTH, -1);

// Max = current
long maxTime = calendar.getTimeInMillis();

How to display date picker for android with only month and year fields?

I just have released a new date picker framework which allows you to create custom date picker. I also provided some example date pickers like the one you are looking for. Hope, that it works for you.

the code can be found here: https://github.com/bendemboski/DateSlider

UPDATE 06/2014:
This library was developed 2010 and has been unmaintained since 2011. So it is most likely out of date by now.

Android Date selector with only Day and Month

Here give this a go. Its APIv11+ but there are other ways on lower API versions.

DatePickerDialog dlg = new DatePickerDialog(context, datePickerListener, 
dueDateCalendar.get(Calendar.YEAR),
dueDateCalendar.get(Calendar.MONTH),
dueDateCalendar.get(Calendar.DAY_OF_MONTH));
int year = context.getResources().getIdentifier("android:id/year", null, null);
if(year != 0){
View yearPicker = dlg.getDatePicker().findViewById(year);
if(yearPicker != null){
yearPicker.setVisibility(View.GONE);
}
}
return dlg;

Updated code: This should do the job.

DatePickerDialog dlg = new DatePickerDialog(context, datePickerListener, 
dueDateCalendar.get(Calendar.YEAR),
dueDateCalendar.get(Calendar.MONTH),
dueDateCalendar.get(Calendar.DAY_OF_MONTH))
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
int year = getContext().getResources()
.getIdentifier("android:id/year", null, null);
if(year != 0){
View yearPicker = findViewById(year);
if(yearPicker != null){
yearPicker.setVisibility(View.GONE);
}
}
}
};
return dlg;


Related Topics



Leave a reply



Submit