Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change NumberPicker TextSize

Tags:

android

How do i set text size of center selected value of a number picker ? and also when scrolled the size of the center number should be greater than the others.

I have tried using the following code :

public static boolean setNumberPickerTextColor(NumberPicker numberPicker, int color) {
        final int count = numberPicker.getChildCount();
        for (int i = 0; i < count; i++) {
            View child = numberPicker.getChildAt(i);
            if (child instanceof EditText) {
                try {
                    Field selectorWheelPaintField = numberPicker.getClass()
                            .getDeclaredField("mSelectorWheelPaint");
                    selectorWheelPaintField.setAccessible(true);
                    ((Paint) selectorWheelPaintField.get(numberPicker)).setColor(color);
                    ((EditText) child).setTextColor(color);
                    ((EditText) child).setTextSize(48);
                    numberPicker.invalidate();
                    return true;
                } catch (NoSuchFieldException | IllegalAccessException | IllegalArgumentException e) {
                    Log.w("setNumberPickerTextColor", e);
                }
            }
        }
        return false;
    }

but after scroll the text size returns to normal

like image 212
Ankur Avatar asked Oct 24 '25 02:10

Ankur


1 Answers

You can do that using XML and power of styles.

First add following to your styles.xml

<style name="MyTheme.NumberPicker">
    <item name="android:textSize">11dp</item>
</style>

Then use that style in your NumberPicker like so:

<android.widget.NumberPicker        
    ...
    android:theme="@style/MyTheme.NumberPicker"
    .../>

Tested on API +17

like image 175
Mehdi Dehghani Avatar answered Oct 26 '25 17:10

Mehdi Dehghani