28

I'm developing and Android app using the 1.6 SDK. I'm using a TimePicker and I don't want the soft keyboard to come up when you click on the digits in the TimePicker. I only want the TimePicker to be changed using the plus and minus buttons. I've tried using android:focusable="false" and android:focusableInTouchMode="false" hoping those would do it, but they didn't seem to do anything. Is there a way to do this?

keaukraine
  • 5,315
  • 29
  • 54
ashughes
  • 7,155
  • 9
  • 48
  • 54

7 Answers7

64

try to use:

myTimePicker.setDescendantFocusability(TimePicker.FOCUS_BLOCK_DESCENDANTS);

to disable focus on the text views of the internal NumberPickers

user499318
  • 656
  • 6
  • 3
22

Add this in your XML:

android:descendantFocusability="blocksDescendants"

Defines the relationship between the ViewGroup and its descendants when looking for a View to take focus.

IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
4

XML:

<NumberPicker
    ...
    android:descendantFocusability="blocksDescendants" />
Andrew
  • 36,676
  • 11
  • 141
  • 113
2

I just tried (targeting 4.0.3)

myTimePicker.setDescendantFocusability(TimePicker.FOCUS_BLOCK_DESCENDANTS);

with no success. After searching google for "android TimePicker BLOCK_DESCENDANTS not working" i followed the hit

http://code.google.com/p/android/issues/detail?id=38369

where the last post, which is by a "Project Member", states:

"Descendant focusability refers to a view's ability to take focus, as in focus traversal using a keyboard, trackball, or d-pad. It does not affect touch events by design.

If you need to block touch events from a descendant view, consider overriding onInterceptTouchEvent in a parent view and returning true when blocking touch events is desired. This will cause the MotionEvents to be sent to the intercepting parent until the last pointer goes up."

samus
  • 6,102
  • 6
  • 31
  • 69
2

Don't know if this works for TimePicker, but I found the correct paramter to use to prevent the keyboard from displaying when focusing an edit text or using the copy and paste menu on its contents:

First, you should call setInputType(InputType.TYPE_NULL) on the editText.

Next instantiate the InputTypeManager:

InputMethodManager imm = 
(InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);

And then, call hideSoftInputFromWindow() on the imm object, but using the InputMethodManager.HIDE_NOT_ALWAYS as second parameter instead of 0 (as advised in first answer):

imm.hideSoftInputFromWindow(editText.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS)

This will prevent the soft keyboard from ever popping up when clicking/focusing this EditText control, even when using the menu key to perform copy/paste operations.

PS: if you want to prevent copy/paste operations you can call editText.setEditable(false) but then you won't be able to dynamically change the EditText's contents.

Andro Selva
  • 53,910
  • 52
  • 193
  • 240
Laurent
  • 21
  • 1
0

You can do this:

    InputMethodManager imm = 
(InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);

Please refer to this question as well:

Close/hide the Android Soft Keyboard

Community
  • 1
  • 1
MexicanHacker
  • 2,685
  • 3
  • 19
  • 20
  • This doesn't work for the TimePicker. I've done that before with an EditText. Also, I don't just want a way to hide the soft keyboard, but basically to make the EditTexts inside the TimePicker to never have focus. I only want the values to be changed using the plus and minus buttons, and I don't want the user to be able to click in the area where the numbers are and have a cursor come up. I've been trying to figure out how to get a reference to those children inside the TimePicker to do this...but I cannot figure out how. – ashughes Mar 07 '10 at 21:05
0

This does not work either for EditText controls... If you press & hold the Menu key on an EditText on which you have set InputType.TYPE_NULL and called immhideSoftInputFromWindow(...), the soft keyboard will still pop up along with the copy/paste menu options.

Martin Geisler
  • 72,968
  • 25
  • 171
  • 229
Laurent
  • 21
  • 1
  • 1
    PS: this was referring to the previous post on the use of InputtMethodManager, sorry, I meant to use the "feedback" button !! – Laurent Jan 08 '12 at 14:18