Search code examples
androidlistenernumberpicker

Android numberpicker, never pass from OnValueChangedListener breakpoint


I extended the NumberPicker class, I overrided to the setOnValueChangedListener but when I debug the code and I click on the plus button of the numberpicker, it never pass in my breakpoint inside the method setOnValueChangedListener. Something is wrong?

@TargetApi(Build.VERSION_CODES.HONEYCOMB)//For backward-compability
public class NumberPickerCustom extends NumberPicker 
{
    private int clickNumber = 0;

    public NumberPickerCustom(Context context) {
        super(context);
    }

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

    public NumberPickerCustom(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        processAttributeSet(attrs);
    }
    private void processAttributeSet(AttributeSet attrs) {
        //This method reads the parameters given in the xml file and sets the properties according to it
        this.setMinValue(attrs.getAttributeIntValue(null, "min", 0));
        this.setMaxValue(attrs.getAttributeIntValue(null, "max", 0));
        this.setValue(attrs.getAttributeIntValue(null, "value", 0));
    }

    @Override
    public void setOnValueChangedListener(
            OnValueChangeListener onValueChangedListener) 
    {
        super.setOnValueChangedListener(onValueChangedListener);
        if (onValueChangedListener==null)
        {
            onValueChangedListener = new OnValueChangeListener() {

                @Override
                public void onValueChange(NumberPicker picker, int oldVal, int newVal) 
                {
                    clickNumber++;
                    if (clickNumber>10)
                    {
                        clickNumber = -1000;
                        Toast.makeText(getContext(), "Clicca sul numero centrale per inserire direttamente un valore", Toast.LENGTH_SHORT).show();
                    }
                }
            };
        }
    }
}

Solution

  • You can initializze your custom number picker

     View rootView = inflater.inflate(R.layout.fragment_random_block, container, false);
     NumberPickerCustom np = (NumberPickerCustom) rootView.findViewById(R.id.numberPickerCustom);
     np.setOnValueChangedListener(this);
    

    Make sure your class implements

      implements OnValueChangeListener
    

    Then

    @Override
    public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
        Toast.makeText(MainActivity.this, "Clicca sul numero centrale per inserire direttamente un valore", Toast.LENGTH_SHORT).show();
         }
    }
    

    Snap shot:

    When you the value changes you get to see the toast modify the same according to your requirement.

    enter image description here

    Edit :

    Change your custom NumberPicker as below

    public class NumberPickerCustom extends NumberPicker implements OnValueChangeListener
    {
        public NumberPickerCustom(Context context) {
            super(context);
        }
    
        public NumberPickerCustom(Context context, AttributeSet attrs) {
            super(context, attrs);
            processAttributeSet(attrs);
        }
    
        public NumberPickerCustom(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
            processAttributeSet(attrs);
        }
        private void processAttributeSet(AttributeSet attrs) {
            //This method reads the parameters given in the xml file and sets the properties according to it
            this.setMinValue(attrs.getAttributeIntValue(null, "min", 0));
            this.setMaxValue(attrs.getAttributeIntValue(null, "max", 0));
            this.setValue(attrs.getAttributeIntValue(null, "value", 0));
            this.setOnValueChangedListener(this);
        }
    
        @Override
        public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
            Toast.makeText(getContext(), "New Value is"+newVal, Toast.LENGTH_SHORT).show();
        }  
    }
    

    Snap shot

    enter image description here