Reputation: 3431
I need hide the day field from a datepicker, I found this method, but the problems is in the version of Android 2.3.3, works fine, but in the version 4x of android not hides the day field. I guess is the library "java.lang.reflect.Field" is obsolete , but I'm not sure of this.
I found this solution here
may be going on here?
final DatePicker datePicker = new DatePicker(this);
LLayout.addView(datePicker);
hideDayField(datePicker, "mDayPicker");
private void hideDayField(DatePicker datePicker, String name) {
try {
java.lang.reflect.Field field = DatePicker.class.getDeclaredField(name);
field.setAccessible(true);
View fieldInstance = (View) field.get(datePicker);
fieldInstance.setVisibility(View.GONE);
} catch (Exception e) {
e.printStackTrace();
}
}
Upvotes: 0
Views: 3461
Reputation: 1423
I have done something similar hiding the day and the month, found the solution here
try {
java.lang.reflect.Field[] f = picker.getClass().getDeclaredFields();
for (java.lang.reflect.Field field : f) {
if (field.getName().equals("mDayPicker") || field.getName().equals("mDaySpinner")
|| field.getName().equals("mMonthPicker") || field.getName().equals("mMonthSpinner")) {
field.setAccessible(true);
Object dmPicker = new Object();
dmPicker = field.get(picker);
((View) dmPicker).setVisibility(View.GONE);
}
}
}
Hope it helps.
Upvotes: 1
Reputation: 25755
That approach you're taking uses reflection to mess with some hidden fields of the DatePicker
-class. The problem with this is, that when the internals of a class change (e.g. the name of the field you're getting changed) this won't work anymore.
In your linked question, there are some other options provided, which might be better suited.
Upvotes: 2