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 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();
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;
How to show only day and month in date picker dialog in Kotlin(Android)?
I tested with different code and this code works fine. If you use datepicker theme as Theme_Holo_Dialog then it working fine. Working code as per below.
Note: It's not working if you set theme Theme_Material_Dialog
package com.wave18.datepickedialogdemo
import android.annotation.SuppressLint
import android.app.DatePickerDialog
import android.content.res.Resources
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.view.View
import kotlinx.android.synthetic.main.activity_main.*
import java.util.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Date Picker Dialog
val dialog = datePickerDialog()
// Button for Showing Date Picker Dialog
button_show_date_picker.setOnClickListener {
// Show Date Picker
dialog.show()
// Hide Year Selector
val year = dialog.findViewById<View>(Resources.getSystem().getIdentifier("android:id/year", null, null))
if (year != null) {
year.visibility = View.GONE
}
}
}
// Function for Showing Date Picker
@SuppressLint("SetTextI18n")
fun datePickerDialog(): DatePickerDialog {
val c = Calendar.getInstance()
val year = c.get(Calendar.YEAR)
val month = c.get(Calendar.MONTH)
val day = c.get(Calendar.DAY_OF_MONTH)
// Date Picker Dialog
val datePickerDialog = DatePickerDialog(this@MainActivity, android.R.style.Theme_Holo_Dialog, DatePickerDialog.OnDateSetListener { view, year, monthOfYear, dayOfMonth ->
// Display Selected date in textbox
date.text = "$dayOfMonth $monthOfYear, $year"
}, year, month, day)
// Show Date Picker
return datePickerDialog
}
}
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 display from current month and year in date picker to restrict showing previous year and month android?
Set its bounds using:
setMinDate(long minDate)
setMaxDate(long maxDate)
Where the argument is the usual number of milliseconds since January 1, 1970 00:00:00 in the default time zone. You'll still have to calculate these values of course, but that should be trivial to do with the Calendar class: just take the current date and add or substract x years.
EDIT:
You can get the underlying DatePicker from a DatePickerDialog (by simply calling getDatePicker()) or in your code I think it's where you call DatePicker datePicker and just add rows like:
datePicker.setMinDate(<long variable>);
Related Topics
How to Access Data/Data Folder in Android Device
Navigate from One Fragment to Another on Click of a Button
How to Refresh a Previous Activity After the Back Button Is Pressed
Refresh/Reload/Re Instantiate a Fragment in an Android Tab System
Send Post Request With Json Data Using Volley
Send Text to Specific Contact Programmatically (Whatsapp)
Recyclerview Slow Scrolling Performance and Lag/Halt Sometime While Scrolling
Android Refresh a Fragment List from Its Parent Activity
How to Synchronously Load Data from Firebase
Httpmessagenotreadableexception: Could Not Read Json: Unrecognized Field Using Spring for Android
Edittext Background Base Line Color Changing Based on Its Focus in Android
How to Kill Native Applications from 'Adb Shell'
Disable Ssl as a Protocol in Httpsurlconnection
How to Make a Background 20% Transparent on Android
Difference Between Px, Dip, Dp, and Sp
Android Prevent Immediate Trigger of Alarm Service If Alarm Time Has Passed For the Day