5

I am aware you can apply a custom TypeFace to a TextView in Android like this:

TextView tv = findViewById(R.id.textview01);
Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/BLACKROSE.TTF");
tv.setTypeface(tf);

Is there any way possible to do this for a DatePicker?

5 Answers5

10

Here is my code. I hope it will be useful to someone.

DatePicker datePicker = (DatePicker) findViewById(R.id.datePicker1);
LinearLayout layout1 = (LinearLayout) datePicker.getChildAt(0);
LinearLayout layout = layout1.getChildAt(0);

// day
LinearLayout day = (LinearLayout) layout.getChildAt(0);
setNumberPicker(day);

// month
LinearLayout month = (LinearLayout) layout.getChildAt(1);
setNumberPicker(month);

// year
LinearLayout year = (LinearLayout) layout.getChildAt(2);
setNumberPicker(year);

...

private void setNumberPicker(LinearLayout ll) {
    ((ImageButton) ll.getChildAt(0)).setBackgroundResource(R.drawable.plus_button);
    ((ImageButton) ll.getChildAt(2)).setBackgroundResource(R.drawable.minus_button);

    EditText et = (EditText) ll.getChildAt(1);
    et.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 14);
    et.setTypeface(youtTypeface);
}
asannov
  • 189
  • 2
  • 8
5

After taking a look at the source, the Datepicker widget holds 3 NumberPicker widgets (for day, month, year)which in turn hold a TextView. So you are going have to set the Typeface for the TextView inside the NumberPickers inside the DatePicker.

I think you'll have to get the source for both NumberPicker and DatePicker and modify the source to achieve this, easier said than done I'm afraid.

triggs
  • 5,890
  • 3
  • 32
  • 31
  • 1
    GrepCode, all android source can be found there. DatePicker http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/1.5_r4/android/widget/DatePicker.java NumberPicker http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/1.5_r4/com/android/internal/widget/NumberPicker.java – triggs Feb 10 '12 at 09:51
4

Here is my solution:

private void overrideFonts(View v) {
    ViewGroup picker;
    try {
        picker = (DatePicker) v;
    } catch (Exception e) {
        picker = (TimePicker) v;
    }
    LinearLayout layout1 = (LinearLayout) picker.getChildAt(0);
    if (picker instanceof TimePicker) {
        if (layout1.getChildAt(1) instanceof NumberPicker) {
            NumberPicker v1 = (NumberPicker) layout1.getChildAt(1);
            final int count = v1.getChildCount();
            for (int i = 0; i < count; i++) {
                View child = v1.getChildAt(i);

                try {
                    Field wheelpaint_field = v1.getClass().getDeclaredField("mSelectorWheelPaint");
                    wheelpaint_field.setAccessible(true);
                    ((Paint) wheelpaint_field.get(v1)).setTypeface(//your font here);
                    ((Paint) wheelpaint_field.get(v1)).setColor(getResources().getColor(R.color.colorOrange));
                    ((EditText) child).setTypeface(// your font here);
                    v1.invalidate();
                } catch (Exception e) {
                    //TODO catch.
                    //If java cant find field then it will catch here and app wont crash.
                }
            }
        }
    }
    LinearLayout layout = (LinearLayout) layout1.getChildAt(0);
    for (int j = 0; j < 3; j++) {
        try {
            if (layout.getChildAt(j) instanceof NumberPicker) {
                NumberPicker v1 = (NumberPicker) layout.getChildAt(j);
                final int count = v1.getChildCount();
                for (int i = 0; i < count; i++) {
                    View child = v1.getChildAt(i);

                    try {
                        Field wheelpaint_field = v1.getClass().getDeclaredField("mSelectorWheelPaint");
                        wheelpaint_field.setAccessible(true);
                        ((Paint) wheelpaint_field.get(v1)).setTypeface(//your font here);
                        ((Paint) wheelpaint_field.get(v1)).setColor(getResources().getColor(R.color.colorOrange));
                        ((EditText) child).setTypeface(//your font here);
                        v1.invalidate();
                    } catch (Exception e) {
                        //TODO catch.
                        //If java cant find field then it will catch here and app wont crash.
                    }
                }
            }
        } catch (Exception e) {
            //TODO catch.
            //If java cant find field then it will catch here and app wont crash.
        }
    }

}
SKG
  • 774
  • 6
  • 7
0

If you can use DatePickerDialog, then u can simply get the buttons of the dialog, like:

DatePickerDialog datePickerDialog; // GET your dialog
datePickerDialog.show();
datePickerDialog.getButton(DatePickerDialog.BUTTON_POSITIVE).setTextSize(30); // 30 is your text size
datePickerDialog.getButton(DatePickerDialog.BUTTON_NEGATIVE).setTextSize(30);
M. Usman Khan
  • 3,689
  • 1
  • 59
  • 69
0

Kotlin-Anko equivalent to SKG's solution

        startTimePicker = timePicker {
            this.applyRecursively {
                when(it) {
                    is NumberPicker -> {
                        val paintField = it.javaClass.getDeclaredField("mSelectorWheelPaint")
                        paintField.isAccessible = true
                        (paintField.get(it) as? Paint)?.typeface = //your font here
                    }
                    is TextView -> {
                        it.typeface = //your font here
                    }
                }
            }