Basic usage and FAQ summary of Android NumberPicker


In the project, I needed a control to select the number of people, so I thought of NumberPicker. This control is not so popular. I used it for the first time, so I encountered some problems. Here is a summary.


First, let's look at the final effect:


The requirement is actually very simple, that is, play a Dialog, select a number and click OK. I won't talk more about Dialog and these buttons. I'll mainly talk about how to use NumberPicker, mainly including the following points:

  • Set content
  • Set cycle status
  • Settings are not editable
  • Set listening
  • Set split line color
  • Set font color and size

First, set the content. If only the maximum and minimum values are set, only numbers will be displayed. If you want to display strings, you need to define an array yourself. Take the above effect as an example. Since there is a "10 +" in the display content, we can't just display numbers. We have to define an array ourselves:

    private String[] numbers = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "10+"};
    //Set the content array to be displayed
    //Set maximum and minimum values
    //Set default location

To set circular display, note that the setWrapSelectorWheel method must be called after the above code.

    //It is set here as no circular display, and the default value is true

NumberPicker can be edited by default, as follows:

Editable status.png

So we need to add a line of code to make NumberPicker uneditable:

    //Settings are not editable

This basically works. Now we need to set monitoring on it to get the selection result:

    numberPicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
        public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
            //Get selection results

The next operation may be a little troublesome. Let's take a look at how to set the color of the split line. Here we use the reflection method:

    private void setNumberPickerDividerColor(NumberPicker numberPicker) {
        Field[] pickerFields = NumberPicker.class.getDeclaredFields();
        for (Field pf : pickerFields) {
            if (pf.getName().equals("mSelectionDivider")) {
                try {
                    //Sets the color value of the split line
                    pf.set(numberPicker, new ColorDrawable(getResources().getColor(R.color.numberpicker_divider_color)));
                } catch (IllegalAccessException e) {

Finally, let's take a look at how to modify the font color and size and create a new class to inherit NumberPicker:

public class TextConfigNumberPicker extends NumberPicker {

    public TextConfigNumberPicker(Context context) {

    public TextConfigNumberPicker(Context context, AttributeSet attrs) {
        super(context, attrs);

    public TextConfigNumberPicker(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

    public void addView(View child) {

    public void addView(View child, ViewGroup.LayoutParams params) {
        super.addView(child, params);

    public void addView(View child, int index, ViewGroup.LayoutParams params) {
        super.addView(child, index, params);

    private void updateView(View view) {
        if (view instanceof EditText) {
            //Sets the color and size of the text
            ((EditText) view).setTextColor(getResources().getColor(;
            ((EditText) view).setTextSize(16);


Well, that's all for the basic usage and frequently asked questions of NumberPicker. I hope it will be helpful to you. If you have any questions, please correct them.

Author: example wave
Source: Jianshu
The copyright belongs to the author. For commercial reprint, please contact the author for authorization, and for non-commercial reprint, please indicate the source.

Tags: Android source code

Posted on Tue, 19 Oct 2021 21:46:45 -0400 by bhola