Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NumberPicker not graying out when disabled

I believe I may have found a bug in the Android UI, but I'd like to make sure I'm not doing something flat out wrong. Here is a simplified version of the code I'm running that's causing the problem. This listener is set on a specific NumberPicker in my UI, and it properly disables / enables things, but if the user has changed the value of one of the other NumberPickers that it disables, it behaves a little bit oddly.

It still properly disables the input, but it fails to grey the value out, making it look like it's still enabled. Is this intended? Am I doing something wrong?

NumberPicker.OnValueChangeListener diceChangeListener = new NumberPicker.OnValueChangeListener() {
    @Override
    public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
        View parent = (View) picker.getParent();    

        if (newVal == 0) {
            ((NumberPicker) parent.findViewById(R.id.diceCountPicker1)).setEnabled(false);
        } else if (oldVal == 0) {
            ((NumberPicker) parent.findViewById(R.id.diceCountPicker1)).setEnabled(true);
        }
    }
};

Let me know if there's a better way to do this, or if this is a legitimate bug. If it is a bug, is there a way to work around it?

Edit: Here's the definition of diceCountPicker1 in the XML:

<NumberPicker
    android:id="@+id/diceCountPicker1"
    android:layout_width="48dp"
    android:layout_height="128dp"
    android:descendantFocusability="blocksDescendants"
    />

Edit 2:

I have tested this in emulators, and confirmed that the problem doesn't exist before Jellybean (4.1). It works properly in ICS. That's... annoying. But I may be able to live with it for now. I'll leave this open for potential ways to work around the bug, but it looks to me like a real bug, and I doubt it can be fixed.

like image 373
Utoxin Avatar asked Oct 05 '22 15:10

Utoxin


2 Answers

Try in this way.

        NumberPicker npicker = (NumberPicker) findViewById(R.id.diceCountPicker1);
        npicker.setMaxValue(100);
        npicker.setMinValue(0);

        npicker.setOnValueChangedListener(new OnValueChangeListener() {
            @Override
            public void onValueChange(NumberPicker picker, int oldVal,
                    int newVal) {
                // Conditions for Enable/Disable picker                 
                if (newVal == 0) {
                    picker.setEnabled(false);
                } else if (oldVal == 0) {
                    picker.setEnabled(true);
                }
            }
        });

It works well for me and it is behaving properly with Enable/Disable as per added conditions.

Thanks.

like image 191
Pratik Sharma Avatar answered Oct 10 '22 04:10

Pratik Sharma


Well, with no additional feedback coming in on this, I'm going to go ahead and close it out. Here's what I know:

This appears to be a bug introduced in Jellybean. I have tested in emulators, and the UI works as expected in ICS and earlier. For the time being, I'm just going to deal with this, and attempt to submit an official bug report.

like image 33
Utoxin Avatar answered Oct 10 '22 02:10

Utoxin