Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable keyboard input on Android TimePicker

Tags:

android

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?

like image 249
ashughes Avatar asked Mar 06 '10 03:03

ashughes


5 Answers

try to use:

myTimePicker.setDescendantFocusability(TimePicker.FOCUS_BLOCK_DESCENDANTS);

to disable focus on the text views of the internal NumberPickers

like image 112
user499318 Avatar answered Nov 06 '22 04:11

user499318


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.

like image 45
IntelliJ Amiya Avatar answered Nov 06 '22 02:11

IntelliJ Amiya


XML:

<NumberPicker
    ...
    android:descendantFocusability="blocksDescendants" />
like image 23
Andrew Avatar answered Nov 06 '22 03:11

Andrew


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.

like image 37
Laurent Avatar answered Nov 06 '22 03:11

Laurent


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."

like image 2
samus Avatar answered Nov 06 '22 04:11

samus