Why does text keyboard show on Android's TimePicker's onScroll?

When using a TimePicker set to spinner mode, if I click on a number (minutes or hours), the number keyboard shows up.

But whenever I scroll any of the spinners, the keyboard changes to the text inputType.

How can I avoid this?

I've tried calling timePicker.setAddStatesFromChildren(true) and setting an OnTimeChangedListener, but that won't work, for if I scroll just enough for the spinner to move but not for the time to change, the listener is not triggered but the keyboard changes to text inputType anyway.

Also, timePicker.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS) is not what I'm looking for, for I still want the keyboard to show up, but only that it won't change its inputType to text.

Upvotes: 2

Views: 307

Answers (2)

In the end, I couldn't find which view was making the keyboard appear. I tried removing the next focus from every view inside the TimePicker, but nothing. Then, I thought the problem was that, since I was using a 24-hour format spinner, the view to blame was the hidden AM/PM CustomTextView inside the TimePicker. I made it non-focusable, but still the same issue. So I concluded that the problem was somewhere in the implementation of the TimePicker itself, who manages some event and displays the keyboard.

So I decided to iterate over the NumberPickers inside TimePicker —which are three— and set an OnScrollListener on them that hides the keyboard. But still, I get to see the text keyboard appearing before being dismissed. But that's the best I've managed to do.

public static <T extends View> List<T> getViewsByClassNameFromView(ViewGroup viewGroup, Class<T> clazz) {

    final List<T> matches = new LinkedList<>();

    final int childCount = viewGroup.getChildCount();

    for (int i = 0; i < childCount; i++) {
        final View child = viewGroup.getChildAt(i);
        if (clazz.isInstance(child)) {
            matches.add((T) child);
        } else if (child instanceof ViewGroup) {
            matches.addAll(getViewsByClassNameFromView((ViewGroup) child, clazz));
        }
    }

    return matches;
}

public void hideSoftKeyboard(View view) {
    InputMethodManager imm =
            (InputMethodManager) view.getContext().getApplicationContext()
                    .getSystemService(Activity.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
    view.clearFocus();
}

private void fixTimePicker() {
    final List<NumberPicker> numberPickers = ViewUtil.getViewsByClassNameFromView(timePicker, NumberPicker.class);
    for (final NumberPicker numberPicker: numberPickers) {
        numberPicker.setOnScrollListener(new NumberPicker.OnScrollListener() {
            @Override
            public void onScrollStateChange(NumberPicker view, int scrollState) {
                hideSoftKeyboard(view);
            }
        });
    }
}

Upvotes: 1

Deependra Kumar
Deependra Kumar

Reputation: 126

android:descendantFocusability="blocksDescendants" use this attribute in DatePicker Xml and it will resolve your issue.

Upvotes: 0

Related Questions