3

I have a numeric EditText in a fragment that shows the keyboard as normal when I select the EditText. I want to hide the keyboard when I enter OK. So I use a hide_keyboard() function which is working fine.

The issue I have is when I re-select the EditText, then the soft keyboard doesn't show up anymore. I tried many things but none worked.

Any ideas?

Here is my EditText:

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="number"
    android:ems="7"
    android:id="@+id/kip_time"
    android:hint="Reflexion time"
    android:layout_below="@+id/chronometer_kipling"
    android:layout_alignStart="@+id/chronometer_kipling"
    android:layout_marginTop="10dp"
    />

and my hide_keyboard() function:

   private void hide_keyboard(Context context, View view) {
        InputMethodManager inputManager = (InputMethodManager)
                context.getSystemService(Context.INPUT_METHOD_SERVICE);
        inputManager.toggleSoftInput(0, 0);
    }

and finally my onclicklistener method:

   kip_time.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            reflexion_time = Integer.parseInt(kip_time.getText().toString());
            reflexion_time = reflexion_time * 1000;
            hide_keyboard(context, view);
        }
    });
Pankaj
  • 7,380
  • 6
  • 39
  • 60
narb
  • 892
  • 1
  • 10
  • 35

4 Answers4

6

If your need is to hide keyboard after entering value then simply use

android:imeOptions="actionDone"

It gives a 'done' button on soft-keyboard, which users can click when they done entering values. Add this to your EditText declaration and remove your hide_keyboard() function. update you layout xml as follow.

<EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="number"
        android:ems="7"
        android:id="@+id/kip_time"
        android:hint="Reflexion time"
        android:layout_below="@+id/chronometer_kipling"
        android:layout_alignStart="@+id/chronometer_kipling"
        android:imeOptions="actionDone"
        android:layout_marginTop="10dp"
        />

* to handle done button click event use the listeners as below *

kip_time.setOnKeyListener(new OnKeyListener() {
            public boolean onKey(View v, int keyCode, KeyEvent event) {

                if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
                    (keyCode == KeyEvent.KEYCODE_ENTER)) {
                    // you codes gose here
                    //reflexion_time = Integer.parseInt(kip_time.getText().toString());
                    //reflexion_time = reflexion_time * 1000;
                  return true;
                }
                return false;
            }
        });
Niroshan
  • 975
  • 8
  • 27
  • I did not test enough. it enables the keyboard to appear and disappear BUT it does not trigger the setonclcklistener. – narb Dec 18 '15 at 09:03
  • In the end imeOptions was useless and I removed it. You've just added part of my code? You miss the important one. – narb Dec 19 '15 at 10:51
  • I have placed your code (reflexion_time calculation) as a comment to denote where you can use it. this code will be triggered as soon as user press 'done' button in soft-keyboard if you use 'imeOptions'. – Niroshan Dec 21 '15 at 03:52
4

I fixed my problem by doing:

   kip_time.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if ((event != null && (event.getKeyCode() == KeyEvent.KEYCODE_NUMPAD_ENTER)) || (actionId == EditorInfo.IME_ACTION_DONE)) {
                Log.i("OK", "Enter pressed");
                reflexion_time = Integer.parseInt(kip_time.getText().toString());
                reflexion_time = reflexion_time * 1000;
                hide_keyboard();
            }
            return false;
        }
    });

with the hide keyboard:

   private void hide_keyboard() {
        InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(kip_time.getWindowToken(), 0);
    }

no need for a show_keyboard()

narb
  • 892
  • 1
  • 10
  • 35
0

Use the below method for hiding keypad and check whether it works to show the keypad again when you click on editText:

private void hideKeypad() {
        View view = context.getCurrentFocus();

        InputMethodManager inputManager = (InputMethodManager) context
                .getSystemService(Context.INPUT_METHOD_SERVICE);
        if (view instanceof EditText) {
            inputManager.hideSoftInputFromWindow(context
                    .getCurrentFocus().getWindowToken(),
                    InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }
Pankaj
  • 7,380
  • 6
  • 39
  • 60
  • issue is the same. keyboard disappears and next I click on the edittext the keyboard doesn't show up. – narb Dec 18 '15 at 09:05
0

If it is not a multi-line input, just add this to the EditText

android:singleLine="true"

The user just tabs Enter/OK on soft-key and that is it...

Haadka
  • 36
  • 4