5

All, I'm customizing a city picker, which uses three numberPicker inside,like this: enter image description here

Its a Chinese province-city-area picker.

The code:

String[] areas = get_from_somewhere();
areaPicker.setDisplayedValues(areas);

I want to reduce the font size,any advice?

Solution: Refer to this

Community
  • 1
  • 1
Ninja
  • 2,289
  • 3
  • 18
  • 32
  • you might be using custom `WheelView` which extends `View` isn't it? – Rustam Oct 22 '14 at 08:50
  • @Rustam yes,I want a WheelView in android style. founded a sample on Github https://github.com/ywenblocker/Provinces-Picker-wheel ,but it not fits the Holo Theme. Give me more info,plz – Ninja Oct 22 '14 at 08:56
  • better use this https://github.com/mstorsjo/vlc-android/tree/master/java-libs/WheelView/src/kankan/wheel/widget – Rustam Oct 22 '14 at 08:58
  • in that there is `AbstractWheelTextAdapter` in `adapter` where you can change the textsize. – Rustam Oct 22 '14 at 09:00
  • @Rustam Thankyou, its style not fit the android Holo theme, I'm not prefer it. – Ninja Oct 22 '14 at 10:54

3 Answers3

8

You can extend the default NumberPicker in your Custom class CustomNumberPicker for this:

public class CustomNumberPicker extends NumberPicker {

    public NumberPicker(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public void addView(View child) {
        super.addView(child);
        if(child instanceof EditText) {
            ((EditText) child).setTextSize(25);
        }
    }
}

Now replace your current NumberPicker in xml with CustomNumberPicker:

<com.pkg.name.CustomNumberPicker
    android:id="@+id/number_picker"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />
Nathan Tuggy
  • 2,239
  • 27
  • 28
  • 36
MysticMagicϡ
  • 27,509
  • 15
  • 68
  • 114
  • Thank your answer, I set a breakpoint on ((EditText) view).setTextSize(25); But nothing happened – Ninja Oct 22 '14 at 09:05
  • Did you use CustomNumberPicker instead of NumberPicker in your xml or activity? – MysticMagicϡ Oct 22 '14 at 09:06
  • Yes, I use " provincePicker = new SmallSizeNumberPicker(context, null); " in my constructor, the break point never invokes. I seach the keyword 'addView(' in the source of NumberPicker.java , and get nothing. – Ninja Oct 22 '14 at 09:18
  • Thank you ,Its what I want – Ninja Oct 22 '14 at 09:26
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/63466/discussion-between-ninja-and-mysticmagic). – Ninja Oct 22 '14 at 09:31
7

Although technically not changing the font size, the size of the text can be changed by changing the scale of the entire NumberPicker View.

<NumberPicker
    android:id="@+id/number_picker"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:scaleX=".7"
    android:scaleY=".7"/>
repkap11
  • 607
  • 7
  • 9
4

Based on John Starbird answer.

You add style.

<style name="NumberPickerText">
    <item name="android:textSize">11sp</item>
</style> 

Add style to your Number Picker in XML.

android:theme="@style/NumberPickerText"
Taier
  • 2,011
  • 9
  • 19
Jakub S.
  • 4,461
  • 35
  • 29