ashughes
ashughes

Reputation: 7233

Disable keyboard input on Android TimePicker

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?

Upvotes: 28

Views: 27877

Answers (7)

IntelliJ Amiya
IntelliJ Amiya

Reputation: 75798

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.

Upvotes: 23

Andrew
Andrew

Reputation: 38029

XML:

<NumberPicker
    ...
    android:descendantFocusability="blocksDescendants" />

Upvotes: 4

samus
samus

Reputation: 6202

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

Upvotes: 2

Laurent
Laurent

Reputation: 21

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.

Upvotes: 2

Laurent
Laurent

Reputation: 21

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.

Upvotes: 0

user499318
user499318

Reputation: 656

try to use:

myTimePicker.setDescendantFocusability(TimePicker.FOCUS_BLOCK_DESCENDANTS);

to disable focus on the text views of the internal NumberPickers

Upvotes: 64

MexicanHacker
MexicanHacker

Reputation: 2685

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

Upvotes: 0

Related Questions