As we all know that Android has not given sufficient APIs to customize DatePicker widget. Say, for example, you are working on some application where you need to have same font style used across all screens of your application. Then it will not look good if you are not able to set same text font to your DatePicker.
I solved this problem by using java reflection APIs, and its working for all devices from Android version 4.+ and above.
Here is my code:
public class DatePickerUtil {
public static void setFont(Activity activity, DatePicker datepicker) {
if (android.os.Build.VERSION.SDK_INT >= 20)
initLollipop(activity, datepicker);
else if (android.os.Build.VERSION.SDK_INT >= 14)
initKitkat(activity, datepicker);
}
private static void initLollipop(Activity activity, DatePicker datepicker) {
try {
Field delegateField = datepicker.getClass().getDeclaredField(
"mDelegate");
delegateField.setAccessible(true);
Object delegate = new Object();
delegate = delegateField.get(datepicker);
Field field = delegate.getClass().getDeclaredField("mDaySpinner");
setFont(activity, delegate, field);
field = delegate.getClass().getDeclaredField("mMonthSpinner");
setFont(activity, delegate, field);
field = delegate.getClass().getDeclaredField("mYearSpinner");
setFont(activity, delegate, field);
setFontEditText(activity, delegate,
delegate.getClass().getDeclaredField("mDaySpinnerInput"));
setFontEditText(activity, delegate,
delegate.getClass().getDeclaredField("mMonthSpinnerInput"));
setFontEditText(activity, delegate,
delegate.getClass().getDeclaredField("mYearSpinnerInput"));
} catch (SecurityException e) {
Log.d("ERROR", e.getMessage());
} catch (IllegalArgumentException e) {
Log.d("ERROR", e.getMessage());
} catch (IllegalAccessException e) {
Log.d("ERROR", e.getMessage());
} catch (Exception e) {
Log.d("ERROR", e.getMessage());
}
}
private static void initKitkat(Activity activity, DatePicker datepicker) {
try {
Field field = datepicker.getClass().getDeclaredField("mDaySpinner");
setFont(activity, datepicker, field);
field = datepicker.getClass().getDeclaredField("mMonthSpinner");
setFont(activity, datepicker, field);
field = datepicker.getClass().getDeclaredField("mYearSpinner");
setFont(activity, datepicker, field);
setFontEditText(activity, datepicker,
datepicker.getClass().getDeclaredField("mDaySpinnerInput"));
setFontEditText(activity, datepicker,
datepicker.getClass()
.getDeclaredField("mMonthSpinnerInput"));
setFontEditText(activity, datepicker,
datepicker.getClass().getDeclaredField("mYearSpinnerInput"));
} catch (SecurityException e) {
Log.d("ERROR", e.getMessage());
} catch (IllegalArgumentException e) {
Log.d("ERROR", e.getMessage());
} catch (IllegalAccessException e) {
Log.d("ERROR", e.getMessage());
} catch (Exception e) {
Log.d("ERROR", e.getMessage());
}
}
private static void setFont(Activity activity, Object datepicker, Field field)
throws Exception {
field.setAccessible(true);
Object yearPicker = new Object();
yearPicker = field.get(datepicker);
((View) yearPicker).setVisibility(View.VISIBLE);
((View) yearPicker).setPadding(-2, 0, -2, 0);
View childpicker;
childpicker = (View) activity.findViewById(Resources.getSystem().getIdentifier(
"month", "id", "android"));
Field field1 = childpicker.getClass().getDeclaredField("mInputText");
field1.setAccessible(true);
Object edittext = new Object();
edittext = field1.get(yearPicker);
Field field2 = childpicker.getClass().getDeclaredField(
"mSelectorWheelPaint");
field2.setAccessible(true);
Object paint = new Object();
paint = field2.get(yearPicker);
if(CustomFontTextview.sTypeface== null)
CustomFontTextview.sTypeface = Typeface.createFromAsset(activity.getAssets(), "customfont.ttf");
((Paint) paint).setTypeface(Typeface.create(CustomFontTextview.sTypeface, Typeface.BOLD));
((Paint) paint).setTextSize(30);
((Paint) paint).setColor(Color.RED);
((TextView) edittext).setTypeface(CustomFontTextview.sTypeface, Typeface.BOLD);
((TextView) edittext).setTextColor(Color.RED);
((TextView) edittext).setTextSize(30);
}
private static void setFontEditText(Activity activity,Object datePicker, Field field)
throws Exception {
field.setAccessible(true);
Object paint = new Object();
paint = field.get(datePicker);
if(CustomFontTextview.sTypeface== null)
CustomFontTextview.sTypeface = Typeface.createFromAsset(activity.getAssets(), "customfont.ttf");
((EditText) paint).setTypeface(CustomFontTextview.sTypeface, Typeface.BOLD);
((EditText) paint).setTextColor(Color.RED);
((EditText) paint).setTextSize(30);
}
}
I solved this problem by using java reflection APIs, and its working for all devices from Android version 4.+ and above.
Here is my code:
public class DatePickerUtil {
public static void setFont(Activity activity, DatePicker datepicker) {
if (android.os.Build.VERSION.SDK_INT >= 20)
initLollipop(activity, datepicker);
else if (android.os.Build.VERSION.SDK_INT >= 14)
initKitkat(activity, datepicker);
}
private static void initLollipop(Activity activity, DatePicker datepicker) {
try {
Field delegateField = datepicker.getClass().getDeclaredField(
"mDelegate");
delegateField.setAccessible(true);
Object delegate = new Object();
delegate = delegateField.get(datepicker);
Field field = delegate.getClass().getDeclaredField("mDaySpinner");
setFont(activity, delegate, field);
field = delegate.getClass().getDeclaredField("mMonthSpinner");
setFont(activity, delegate, field);
field = delegate.getClass().getDeclaredField("mYearSpinner");
setFont(activity, delegate, field);
setFontEditText(activity, delegate,
delegate.getClass().getDeclaredField("mDaySpinnerInput"));
setFontEditText(activity, delegate,
delegate.getClass().getDeclaredField("mMonthSpinnerInput"));
setFontEditText(activity, delegate,
delegate.getClass().getDeclaredField("mYearSpinnerInput"));
} catch (SecurityException e) {
Log.d("ERROR", e.getMessage());
} catch (IllegalArgumentException e) {
Log.d("ERROR", e.getMessage());
} catch (IllegalAccessException e) {
Log.d("ERROR", e.getMessage());
} catch (Exception e) {
Log.d("ERROR", e.getMessage());
}
}
private static void initKitkat(Activity activity, DatePicker datepicker) {
try {
Field field = datepicker.getClass().getDeclaredField("mDaySpinner");
setFont(activity, datepicker, field);
field = datepicker.getClass().getDeclaredField("mMonthSpinner");
setFont(activity, datepicker, field);
field = datepicker.getClass().getDeclaredField("mYearSpinner");
setFont(activity, datepicker, field);
setFontEditText(activity, datepicker,
datepicker.getClass().getDeclaredField("mDaySpinnerInput"));
setFontEditText(activity, datepicker,
datepicker.getClass()
.getDeclaredField("mMonthSpinnerInput"));
setFontEditText(activity, datepicker,
datepicker.getClass().getDeclaredField("mYearSpinnerInput"));
} catch (SecurityException e) {
Log.d("ERROR", e.getMessage());
} catch (IllegalArgumentException e) {
Log.d("ERROR", e.getMessage());
} catch (IllegalAccessException e) {
Log.d("ERROR", e.getMessage());
} catch (Exception e) {
Log.d("ERROR", e.getMessage());
}
}
private static void setFont(Activity activity, Object datepicker, Field field)
throws Exception {
field.setAccessible(true);
Object yearPicker = new Object();
yearPicker = field.get(datepicker);
((View) yearPicker).setVisibility(View.VISIBLE);
((View) yearPicker).setPadding(-2, 0, -2, 0);
View childpicker;
childpicker = (View) activity.findViewById(Resources.getSystem().getIdentifier(
"month", "id", "android"));
Field field1 = childpicker.getClass().getDeclaredField("mInputText");
field1.setAccessible(true);
Object edittext = new Object();
edittext = field1.get(yearPicker);
Field field2 = childpicker.getClass().getDeclaredField(
"mSelectorWheelPaint");
field2.setAccessible(true);
Object paint = new Object();
paint = field2.get(yearPicker);
if(CustomFontTextview.sTypeface== null)
CustomFontTextview.sTypeface = Typeface.createFromAsset(activity.getAssets(), "customfont.ttf");
((Paint) paint).setTypeface(Typeface.create(CustomFontTextview.sTypeface, Typeface.BOLD));
((Paint) paint).setTextSize(30);
((Paint) paint).setColor(Color.RED);
((TextView) edittext).setTypeface(CustomFontTextview.sTypeface, Typeface.BOLD);
((TextView) edittext).setTextColor(Color.RED);
((TextView) edittext).setTextSize(30);
}
private static void setFontEditText(Activity activity,Object datePicker, Field field)
throws Exception {
field.setAccessible(true);
Object paint = new Object();
paint = field.get(datePicker);
if(CustomFontTextview.sTypeface== null)
CustomFontTextview.sTypeface = Typeface.createFromAsset(activity.getAssets(), "customfont.ttf");
((EditText) paint).setTypeface(CustomFontTextview.sTypeface, Typeface.BOLD);
((EditText) paint).setTextColor(Color.RED);
((EditText) paint).setTextSize(30);
}
}
You just need to call setFont(activity, datepicker); from your code.
Here is link live application where we have used the same code : Zodiac & Astrology
Enjoy Coding :-)
Here is link live application where we have used the same code : Zodiac & Astrology
Enjoy Coding :-)