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

Zsombor Erdődy-Nagy picture Zsombor Erdődy-Nagy · Aug 5, 2011 · Viewed 29.7k times · Source

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.

Answer

aheuermann picture aheuermann · Aug 23, 2012

I was able to accomplish this by extending the default NumberPicker. Not ideal, but it works.

public class NumberPicker extends android.widget.NumberPicker {

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

   @Override
   public void addView(View child) {
     super.addView(child);
     updateView(child);
   }

   @Override
   public void addView(View child, int index, android.view.ViewGroup.LayoutParams params) {
     super.addView(child, index, params);
     updateView(child);
   }

   @Override
   public void addView(View child, android.view.ViewGroup.LayoutParams params) {
     super.addView(child, params);
     updateView(child);
   }

   private void updateView(View view) {
     if(view instanceof EditText){
       ((EditText) view).setTextSize(25);
       ((EditText) view).setTextColor(Color.parseColor("#333333"));
     }
   }

 }

Then just reference this class in your layout xml.

<com.yourpackage.NumberPicker
        android:id="@+id/number_picker"
        android:layout_width="43dp"
        android:layout_height="wrap_content" />