Android - can I increase the textSize for the NumberPicker widget?

Viewed 31109

We got a NumberPicker widget in 3.0, but it seems that the textSize for this widget can't be modified. Am I missing something or is this the case? I'd really like to increase the font size, it's quite small with the default value. But I can't see a textSize property for it.

7 Answers

Jumping to API 23, the solution presented by aheuermann seems to have problems. In the source for NumberPicker, the text size is set based on the EditText child at initialization. If you try to change it later, the EditText field will get big, but all other numbers drawn directly by the widget will still be small.

The solution I'm going with has the disadvantage that it must be instantiated programmatically, instead of via layout, but otherwise, it seems to work as intended. First, add the following to your styles.xml file:

<style name="NumberPickerText">
    <item name="android:textSize">40dp</item>
</style>

Then, you can instantiate the NumberPicker with

    ContextThemeWrapper cw = new ContextThemeWrapper(this, R.style.NumberPickerText);
    NumberPicker np = new NumberPicker(cw);

assuming that it's being done within an Activity. If not, replace the "this" with whatever Context you have available. This code does the equivalent of setting a theme that overrides all TextView sizes within your Activity, but only applies it to the NumberPicker and its children.

Related