Disable soft keyboard on NumberPicker

57,339

Solution 1

Just found this and it works like a charm:

myNumberPicker.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);

You can also set this in XML:

android:descendantFocusability="blocksDescendants" 

Solution 2

Xml version of Andrew Webber's answer

android:descendantFocusability="blocksDescendants"

Example

<NumberPicker
        android:id="@+id/your_numberpicker"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:descendantFocusability="blocksDescendants"/>

Solution 3

After reading through the com/android/internal/widget/NumberPicker.java source code i got to the following solution:

// Hide soft keyboard on NumberPickers by overwriting the OnFocusChangeListener
OnFocusChangeListener fcl = new OnFocusChangeListener() {
    public void onFocusChange(View v, boolean hasFocus) {
        // Do nothing to suppress keyboard
    }
};

((EditText) numberPicker.getChildAt(1)).setOnFocusChangeListener(fcl);

// Suppress soft keyboard from the beginning
((EditText) numberPicker.getChildAt(1)).setInputType(InputType.TYPE_NULL);

Solution 4

Just enhanced the @MaxVogler 's ans (so if wannt vote this vote @MaxVogler too) and make it a robust hack. Also we dont need to call setOnFocusChangeListener and setInputType. Only setFocusable to false will do.

Below is a helper api to enable/disable the feature

public static void enableNumberPickerManualEditing(NumberPicker numPicker,
        boolean enable) {
    int childCount = numPicker.getChildCount();

    for (int i = 0; i < childCount; i++) {
        View childView = numPicker.getChildAt(i);

        if (childView instanceof EditText) {
            EditText et = (EditText) childView;
            et.setFocusable(enable);
            return;
        }
    }
}

Solution 5

Working code Programatically :

mp.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);

XML:

android:descendantFocusability="blocksDescendants" 
Share:
57,339

Related videos on Youtube

damaxxed
Author by

damaxxed

Updated on July 31, 2020

Comments

  • damaxxed
    damaxxed almost 4 years

    I'm trying to deactivate the soft keyboard when using a NumberPicker to enter numerical values (for aesthetic reasons). This is my layout-xml-code:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    
        <LinearLayout
            android:id="@+id/linearLayout2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginBottom="30dp"
            android:layout_marginTop="30dp" >
    
            <NumberPicker
                android:id="@+id/repetitionPicker"
                android:layout_width="40dp"
                android:layout_height="wrap_content" />
    
            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:text="@string/repetitions_short_divider"
                android:textAppearance="?android:attr/textAppearanceMedium" />
    
            <NumberPicker
                android:id="@+id/weightPicker"
                android:layout_width="40dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="40dp" />
    
            <TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:text="@string/pounds"
                android:textAppearance="?android:attr/textAppearanceMedium" />
        </LinearLayout>
    
    
        <Button
            android:id="@+id/saveButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:text="@string/save" />
    
    </LinearLayout>
    

    And finally this is the code where I try to block the keyboard in the onCreate()-method:

    // hide keyboard
    View.OnClickListener disableKeyBoardListener = new View.OnClickListener() {
        public void onClick(View v) {
            ((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE))
                    .hideSoftInputFromWindow(v.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
        }
    };
    
    ((EditText) weightPicker.getChildAt(1)).setInputType(InputType.TYPE_NULL);
    ((EditText) repetitionPicker.getChildAt(1)).setInputType(InputType.TYPE_NULL);
    
    ((EditText) weightPicker.getChildAt(1)).setOnClickListener(disableKeyBoardListener);
    //((EditText) repetitionPicker.getChildAt(1)).setOnClickListener(disableKeyBoardListener);
    //weightPicker.setOnClickListener(disableKeyBoardListener);
    //repetitionPicker.setOnClickListener(disableKeyBoardListener);     
    
    getWindow().setSoftInputMode(
            WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); 
    

    Sadly, the soft keyboard still shows up when clicking on a NumberPicker. Any ideas?

  • damaxxed
    damaxxed over 12 years
    Yes, I had to directly edit the child views of the NumberPicker. The Android API is very sparse sometimes.
  • frenziedherring
    frenziedherring over 10 years
    Unless I'm missing something, this removes the ability to type in a value for the NumberPicker. It's not necessarily a bad thing, depending on how you are using the picker, but it does appear to be a limitation of the solution.
  • Ben Clayton
    Ben Clayton over 10 years
    You can also set this in XML: android:descendantFocusability="blocksDescendants"
  • smoothumut
    smoothumut over 8 years
    only this one helped me to solve my problem, thanks for your approach
  • Lev
    Lev about 8 years
    Aaah,blocksDescendants, ofcourse. Thank you very much :)
  • Hamzeh Soboh
    Hamzeh Soboh over 5 years
    Amazing solution, saved me!
  • hgoebl
    hgoebl almost 4 years
    Only the programmatic version worked for me with androidx 1.2.0-rc2, and I combined it with isClickable=true and isFocusable=true (Kotlin)
  • Bogdan Stolyarov
    Bogdan Stolyarov over 3 years
    If I add android:descendantFocusability="blocksDescendants", after clicking to current value it's hiding. Only adding click listener helped me. Thanks! You save my mind.