I am trying to hide the year field from the date picker widget.This may look like a repeated question but the answers given for previous questions are not helping me to hide the year field..
Here goes my code::
@Override
protected void onCreate(Bundle savedInstanceState) {
int month = 0;
int year = 0;
int day = 0;
OnDateSetListener datePickerListener = null;
CustomDatePickerDialog dp = new CustomDatePickerDialog(getBaseContext(), android.R.style.Theme_Holo_Light_Dialog, datePickerListener, year, month, day);
DatePickerDialog obj = dp.getPicker();
picker = (DatePicker) findViewById(R.id.datePicker1);
try {
java.lang.reflect.Field[] f = obj.getClass().getDeclaredFields();
// Field f[] = picker.getClass().getDeclaredFields();
for (java.lang.reflect.Field field : f) {
if (field.getName().equals("prabs")) {
field.setAccessible(true);
DatePicker datePicker = (DatePicker) field.get(obj);
java.lang.reflect.Field datePickerFields[] = obj.getClass().getDeclaredFields();
for (java.lang.reflect.Field datePickerField : datePickerFields) {
if ("mYearPicker".equals(datePickerField.getName()) || "mYearSpinner".equals(datePickerField
.getName()))
{
datePickerField.setAccessible(true);
Object yearPicker = new Object();
yearPicker = datePickerField.get(datePicker);
((View) yearPicker).setVisibility(View.GONE);
}
}
/* Object yearPicker = new Object();
yearPicker = field.get(picker);
((View) yearPicker).setVisibility(View.GONE);*/
}
}
}
catch (SecurityException e) {
Log.d("ERROR", e.getMessage());
}
catch (IllegalArgumentException e) {
Log.d("ERROR", e.getMessage());
}
catch (IllegalAccessException e) {
Log.d("ERROR", e.getMessage());
}
obj.show();
}
outside the main class::
class CustomDatePickerDialog extends DatePickerDialog implements OnDateChangedListener
{
private DatePickerDialog prabs;
@SuppressLint("NewApi")
public CustomDatePickerDialog(Context context, int theme,
OnDateSetListener listener, int year, int monthOfYear,
int dayOfMonth)
{
super(context, theme, listener, year, monthOfYear, dayOfMonth);
// TODO Auto-generated constructor stub
prabs = new DatePickerDialog(context, theme, listener, year, monthOfYear, dayOfMonth);
prabs.getDatePicker().init(2015, 01, 8, this);
updateTitle(monthOfYear,dayOfMonth);
}
private void updateTitle(int Month, int Day)
{
// TODO Auto-generated method stub
Calendar mCalendar = Calendar.getInstance();
mCalendar.set(Calendar.MONTH, Month);
mCalendar.set(Calendar.DAY_OF_MONTH, Day);
prabs.setTitle(getFormat().format(mCalendar.getTime()));
}
public SimpleDateFormat getFormat() {
// TODO Auto-generated method stub
return new SimpleDateFormat("MMM, dd");
}
public void onDateChanged(DatePicker view,int year, int month,int day)
{
updateTitle(month,day);
}
public DatePickerDialog getPicker()
{
return this.prabs;
}
}
before adding obj.show();
it was displaying the entire widget with calender,but I want only widget with date and month
trying to solve this from many days. But failed every time :( Any suggestions would be of great help.. Thank you..
EDIT::
year field means the date picker widget's year field of eclipse
I referred How to display year only in date picker in android and Custom Date Picker Dialog
Error log::
01-09 11:26:20.780: E/AndroidRuntime(23840): Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application this is a crash report