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 :-)
Did u forget the "CustomFontTextview" some where?
ReplyDeleteThis comment has been removed by the author.
ReplyDeletei there! The smaller font under each word tells the actual name of the font. The "sparkling" one is called Sweet Pea font and the link is: http://www.dafont.com/sweet-pea.font
ReplyDeleteThe clickable links to all of them are right under the image!
Thanks!
Dafont.com is a site where you can download a ton of free fonts. You can search for a specific typeface, or search by the type of lettering you want, whether it’s serif or sans serif, hand lettered or grunge style. You can also put in your own phrase to see how it looks in a particular font. A lot of these fonts are very decorative and many are handdrawn, so it’s not always the best place to search for body text fonts. Each selection also tells you whether your download is free for personal or commercial use. The download is easy – you get a zip file with the font file inside. Unzip, install, and you’re ready to go.
ReplyDeleteDafont.com is a site where you can download a ton of free fonts. You can search for a specific typeface, or search by the type of lettering you want, whether it’s serif or sans serif, hand lettered or grunge style. You can also put in your own phrase to see how it looks in a particular font. A lot of these fonts are very decorative and many are handdrawn, so it’s not always the best place to search for body text fonts. Each selection also tells you whether your download is free for personal or commercial use. The download is easy – you get a zip file with the font file inside. Unzip, install, and you’re ready to go.
ReplyDeleteTop Casino Apps 2021 - MapYRO
ReplyDeleteGet the best Casino app for your 익산 출장샵 iPhone or Android! Best Casino Apps 2021 - 김포 출장샵 MapYRO 파주 출장안마 We've 과천 출장샵 got the best casino apps and games apps for iOS devices 안동 출장샵 in 2021.