Display Month Fields In Datepickerdialog In Numeric Format Instead Of Alphabetical
The picture below shows the current date picker I have in my android app, however I want to display all the months as 01, 02, 03...12 instead of Jan, Feb, Mar... Dec. Any help woul
Solution 1:
You could design your own Dialog
with NumberPicker
, but if you still want to use DatePickerDialog
, this should work:
DatePickerDialogmDatePicker=newDatePickerDialog(MainActivity.this, android.R.style.Theme_Holo_Light_Dialog, newDatePickerDialog.OnDateSetListener() {
publicvoidonDateSet(DatePicker view, int selectedyear, int selectedmonth, int selectedday) {
// Stuff
}
}, mYear, mMonth, mDayOfMonth) {
finalintmonth= getContext().getResources().getIdentifier("android:id/month", null, null);
final String[] monthNumbers = newString[]{ "01","02","03","04","05","06","07","08","09","10","11","12"}
@OverridepublicvoidonDateChanged(@NonNull DatePicker view, int y, int m, int d) {
super.onDateChanged(view, y, m, d);
// Since DatePickerCalendarDelegate updates the month spinner too, we need to change months as numbers here alsoif(month != 0){
NumberPickermonthPicker= findViewById(month);
if(monthPicker != null){
monthPicker.setDisplayedValues(monthNumbers);
}
}
}
@OverrideprotectedvoidonCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// Hide day spinnerintday= getContext().getResources().getIdentifier("android:id/day", null, null);
if(day != 0){
NumberPickerdayPicker= findViewById(day);
if(dayPicker != null){
dayPicker.setVisibility(View.GONE);
}
}
// Show months as Numbersif(month != 0){
NumberPickermonthPicker= findViewById(month);
if(monthPicker != null){
monthPicker.setDisplayedValues(monthNumbers);
}
}
}
};
mDatePicker.setTitle("Select Date");
mDatePicker.show();
Solution 2:
You can use number picker to achieve this :
1) Create an array of strings:
String mValues[] = { "01","02","03","04","05","06","07","08","09","10","11","12"};
2) create a method to create number picker with custom values:
private void setNubmerPicker(NumberPicker nubmerPicker,String [] numbers ){
nubmerPicker.setMaxValue(numbers.length-1);
nubmerPicker.setMinValue(0);
nubmerPicker.setWrapSelectorWheel(true);
nubmerPicker.setDisplayedValues(numbers);
}
3) call your number picker method with your custom values:
setNubmerPicker(yourNumberPicker,mValues);
The advantages of number picker :
- Added in API level 11 so you can use it on all of your devices.
- You don`t need to open a dialog, it will keep your app more simple(imagine of you want to make an order and for every small detail you had to open dialog) and of course if you would like you can put your number picker inside dialog every time.
- If you will use my answer you can use Unicode Characters inside your data array(in my case - mValues), I think it can really be nice addition with almost no effort.
Solution 3:
I used @Dispew answer, but I got monthPicker as null, so I changed this:
finalintmonth= getContext().getResources().getIdentifier("android:id/month", null, null);
// Show months as Numbersif(month != 0){
NumberPickermonthPicker= findViewById(month);
if(monthPicker != null){
monthPicker.setDisplayedValues(monthNumbers);
}
}
to this:
NumberPickermonthPicker= mDatePicker.findViewById((Resources.getSystem().getIdentifier("month", "id", "android")));
if (monthPicker != null){
monthPicker.setDisplayedValues(monthNumbers);
}
Post a Comment for "Display Month Fields In Datepickerdialog In Numeric Format Instead Of Alphabetical"