Search code examples
androidandroid-edittextnumberpicker

How to reference the EditText inside a NumberPicker?


I need to get the reference to the EditText inside a NumberPicker. I know it's going to be some sort of findViewById on the picker view itself, but I haven't been able to find android's id for it:

final NumberPicker numberPicker = (NumberPicker) view.findViewById(R.id.numberpicker);
EditText numberPickerEditText = (EditText) numberPicker.findViewById(WHAT IS THE ID???)

I know NumberPicker has most of the useful methods for the value etc., but I need some finer control such as displaying the keyboard when the picker opens and I don't think I can do this without a reference to the EditText itself (feel free to correct me if I'm wrong in this!)


Solution

  • If you would look at the xml file being used by the NumberPicker you'll find that the id of the EditText you're looking for is numberpicker_input :

    <merge xmlns:android="http://schemas.android.com/apk/res/android">
    
        <ImageButton android:id="@+id/increment"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@android:drawable/numberpicker_up_btn"
            android:paddingTop="22dip"
            android:paddingBottom="22dip"
            android:contentDescription="@string/number_picker_increment_button" />
    
        <EditText
            android:id="@+id/numberpicker_input"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textAppearance="@style/TextAppearance.Large.Inverse.NumberPickerInputText"
            android:gravity="center"
            android:singleLine="true"
            android:background="@drawable/numberpicker_input" />
    
        <ImageButton android:id="@+id/decrement"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@android:drawable/numberpicker_down_btn"
            android:paddingTop="22dip"
            android:paddingBottom="22dip"
            android:contentDescription="@string/number_picker_decrement_button" />
    
    </merge>
    

    So, you can do it by using the same id:

    NumberPicker numberPicker = (NumberPicker) view.findViewById(R.id.numberpicker);
    EditText numberPickerEditText = (EditText) numberPicker.findViewById(R.id.numberpicker_input)
    

    EDIT

    Ok, so looks like this id is inside android.internal.id, since it's a system id, to be able to access it you'll have to do this:

    numberPicker.findViewById(Resources.getSystem().getIdentifier("numberpicker_input", "id", "android"))