Search code examples
androidnumberpicker

Avoid NumberPicker value automatically getting selected


in my app I'm using 3 NumberPickers to choose an RGB value. But there is always 1 of them selected, even already when the activity opens. On the image you can see what I mean by this.

I was wondering whether this can be avoided and if so, how.

Note that I don't want to disable the ability to click the number which brings up a number input. Only stop the automatic selection (which doesn't cause the number input to come up)

Image displaying the problem:

example of one NumberPicker getting selected

Thanks in advance :)

EDIT:

XML (in a RelativeLayout view)

<NumberPicker
    android:id="@+id/number_picker_R"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textview2"
    android:layout_toLeftOf="@+id/number_picker_G" />

<NumberPicker
    android:id="@+id/number_picker_G"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textview2"
    android:layout_centerHorizontal="true"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp" />

<NumberPicker
    android:id="@+id/number_picker_B"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textview2"
    android:layout_toRightOf="@+id/number_picker_G" />

Java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.tab1_presets, container, false);

    ... (irrelevant code omitted) ...

    this.mNumberPickerR = rootView.findViewById(R.id.number_picker_R);
    this.mNumberPickerG = rootView.findViewById(R.id.number_picker_G);
    this.mNumberPickerB = rootView.findViewById(R.id.number_picker_B);

    // set min and max values for the numberpickers
    this.mNumberPickerR.setMinValue(0);
    this.mNumberPickerR.setMaxValue(255);
    this.mNumberPickerG.setMinValue(0);
    this.mNumberPickerG.setMaxValue(255);
    this.mNumberPickerB.setMinValue(0);
    this.mNumberPickerB.setMaxValue(255);

    // set listeners
    this.mNumberPickerR.setOnValueChangedListener(this);
    this.mNumberPickerG.setOnValueChangedListener(this);
    this.mNumberPickerB.setOnValueChangedListener(this);

    return rootView;

public NumberPicker getNumberPickerR() {
    return this.mNumberPickerR;
}
public NumberPicker getNumberPickerG() {
    return this.mNumberPickerG;
}
public NumberPicker getNumberPickerB() {
    return this.mNumberPickerB;
}

@Override
public void onValueChange(NumberPicker numberPicker, int i, int i1) {
    // get RGB values
    int R = getNumberPickerR().getValue();
    int G = getNumberPickerG().getValue();
    int B = getNumberPickerB().getValue();

    // call parent activity onColorChanged method
    ((MainActivity) getActivity()).onColorChanged(R, G, B);
}

Solution

  • In the parent of your NumberPicker use

    android:descendantFocusability="beforeDescendants"
    android:focusableInTouchMode="true"