Currently I am using date picker native one but i want to change it in such a way that i only needed month and year how to modify this my code is following
http://learnlinky.com/2016/12/disable-day-selection-android-datepicker/
This worked for me :)
DatePickerDialog datePickerDialog =
new DatePickerDialog(getActivity(), AlertDialog.THEME_HOLO_DARK,
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view,
int year,
int monthOfYear,
int dayOfMonth) {
txtMonth.setText(dayOfMonth + "-" + (monthOfYear + 1) + "-" + year);
}
}, mYear, mMonth, mDay);
((ViewGroup)datePickerDialog.getDatePicker())
.findViewById(Resources.getSystem().getIdentifier("day", "id", "android"))
.setVisibility(View.GONE);
datePickerDialog.show();
I modified few things like: Made style the android default Theme_Holo_Dialog and delete the day from the visibility.
Following few changes my code is:
pick_expire_date.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Get Current Date
final Calendar c = Calendar.getInstance();
cyear = c.get(Calendar.YEAR);
cmonth = c.get(Calendar.MONTH);
cday = c.get(Calendar.DAY_OF_MONTH);
DatePickerDialog datePickerDialog = new DatePickerDialog(
CheckoutPaymentActivity.this,
android.R.style.Theme_Holo_Dialog,
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
pick_expire_date.setText((monthOfYear + 1)+"/"+year);
mMonth = monthOfYear + 1;
mYear = year;
}
},
cyear, cmonth, cday);
datePickerDialog.getDatePicker().findViewById(getResources().getIdentifier("day","id","android")).setVisibility(View.GONE);
datePickerDialog.show();
}
});
This worked for me after trying out several different approaches.
datePicker = (DatePicker) findViewById(R.id.date_picker);
public void hideDay() {
try {
java.lang.reflect.Field[] f = datePicker.getClass().getDeclaredFields();
for (java.lang.reflect.Field field : f) {
if (field.getName().equals("mDayPicker") || field.getName().equals("mDaySpinner")) {
field.setAccessible(true);
Object dmPicker = new Object();
dmPicker = field.get(datePicker);
((View) dmPicker).setVisibility(View.GONE);
}
}
}
catch (SecurityException e) {
TLog.d("ERROR", e.getMessage());
}
catch (IllegalArgumentException e) {
TLog.d("ERROR", e.getMessage());
}
catch (IllegalAccessException e) {
TLog.d("ERROR", e.getMessage());
}
}
After Brain Storming half a day i have found this solution this may help other having such scenarios
My Custom class for date Picker is Following that will update the date in title every time user change month/year
class CustomDatePickerDialog extends DatePickerDialog implements OnDateChangedListener {
private DatePickerDialog mDatePicker;
@SuppressLint("NewApi")
public CustomDatePickerDialog(Context context,int theme, OnDateSetListener callBack,
int year, int monthOfYear, int dayOfMonth) {
super(context, theme,callBack, year, monthOfYear, dayOfMonth);
mDatePicker = new DatePickerDialog(context,theme,callBack, year, monthOfYear, dayOfMonth);
mDatePicker.getDatePicker().init(2013, 7, 16, this);
updateTitle(year, monthOfYear);
}
public void onDateChanged(DatePicker view, int year,
int month, int day) {
updateTitle(year, month);
}
private void updateTitle(int year, int month) {
Calendar mCalendar = Calendar.getInstance();
mCalendar.set(Calendar.YEAR, year);
mCalendar.set(Calendar.MONTH, month);
// mCalendar.set(Calendar.DAY_OF_MONTH, day);
mDatePicker.setTitle(getFormat().format(mCalendar.getTime()));
}
public DatePickerDialog getPicker(){
return this.mDatePicker;
}
/*
* the format for dialog tile,and you can override this method
*/
public SimpleDateFormat getFormat(){
return new SimpleDateFormat("MMM, yyyy");
};
}
and the following is the code that will hide the day and its spinner from the date picker dialog
CustomDatePickerDialog dp = new CustomDatePickerDialog(context, android.R.style.Theme_Holo_Light_Dialog, datePickerListener, year, month, day);
DatePickerDialog obj = dp.getPicker();
try{
Field[] datePickerDialogFields = obj.getClass().getDeclaredFields();
for (Field datePickerDialogField : datePickerDialogFields) {
if (datePickerDialogField.getName().equals("mDatePicker")) {
datePickerDialogField.setAccessible(true);
DatePicker datePicker = (DatePicker) datePickerDialogField.get(obj);
Field datePickerFields[] = datePickerDialogField.getType().getDeclaredFields();
for (Field datePickerField : datePickerFields) {
if ("mDayPicker".equals(datePickerField.getName()) || "mDaySpinner".equals(datePickerField
.getName())) {
datePickerField.setAccessible(true);
Object dayPicker = new Object();
dayPicker = datePickerField.get(datePicker);
((View) dayPicker).setVisibility(View.GONE);
}
}
}
}
}catch(Exception ex){
}
obj.show();
Simply way - just to hide NumberPicker in datePickerDialog here is code from my project
public class SelectDateFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Calendar c = Calendar.getInstance();
DatePickerDialog dialog = new DatePickerDialog(getActivity(), this, c.get(Calendar.YEAR),
c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH));
dialog.getDatePicker().setSpinnersShown(true);
// hiding calendarview and daySpinner in datePicker
dialog.getDatePicker().setCalendarViewShown(false);
LinearLayout pickerParentLayout = (LinearLayout) dialog.getDatePicker().getChildAt(0);
LinearLayout pickerSpinnersHolder = (LinearLayout) pickerParentLayout.getChildAt(0);
pickerSpinnersHolder.getChildAt(0).setVisibility(View.GONE);
dialog.setTitle("Pick a date");
return dialog;
}
public void onDateSet(DatePicker view, int yy, int mm, int dd) {
Calendar result = Calendar.getInstance();
result.set(Calendar.YEAR, yy);
result.set(Calendar.MONDAY, mm);
result.set(Calendar.DAY_OF_MONTH, dd);
if (currentWeek != -1) {
updateDateWeek(result.getTimeInMillis());
} else {
updateDate(result.getTimeInMillis());
}
}
}
to show picker just call
DialogFragment newFragment = new SelectDateFragment();
newFragment.show(getActivity().getSupportFragmentManager(), "DatePicker");
This might work for you.. if you are doing for only English.. also can be changes according to the requirements.
Locale.setDefault(Locale.UK);
This will make date picker to appear in the form DD/MM/YYYY
Following code removes day spiner
LinearLayout pickerParentLayout =
(LinearLayout) mDatePicker.getChildAt(0);
LinearLayout pickerSpinnersHolder = (LinearLayout) pickerParentLayout.getChildAt(0);
NumberPicker picker = (NumberPicker) pickerSpinnersHolder.getChildAt(0);
picker.setVisibility(View.GONE);