29

I'm struggling with the done button on the soft keyboard. I can't get the soft keyboard Done key press to hide the keyboard. From another button, it works perfectly with

imm.hideSoftInputFromWindow(editText.getApplicationWindowToken(), 0);

but the onKeyListener does not function the way I want. When I hit the editText, the soft keyboard shows up and its content is cleared from characters.

Thanks for listening!

The main.xml:

<EditText 
    android:id="@+id/answer" 
    android:layout_gravity="center_horizontal" android:textSize="36px"
    android:inputType="phone"
    android:minWidth="60dp" android:maxWidth="60dp"
/>

The Java file:

private EditText editText;
//...
editText = (EditText)findViewById(R.id.answer);
editText.setOnClickListener(onKeyboard);
editText.setOnKeyListener(onSoftKeyboardDonePress);
//...

// method not working:
private View.OnKeyListener onSoftKeyboardDonePress=new View.OnKeyListener() 
{
    public boolean onKey(View v, int keyCode, KeyEvent event) 
    {
        if (event.getKeyCode() == KeyEvent.FLAG_EDITOR_ACTION)
        {
            // code to hide the soft keyboard
            imm = (InputMethodManager) getSystemService(
                Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(editText.getApplicationWindowToken(), 0);
        }
        return false;
    }
};

private View.OnClickListener onKeyboard=new View.OnClickListener() 
{
    public void onClick(View v) 
    {
        editText.setText("");
    }
};

The working method using a button (in the same java file):

private View.OnClickListener onDone=new View.OnClickListener() 
{
    public void onClick(View v) 
    {
        //....
        // code to hide the soft keyboard
        imm = (InputMethodManager) getSystemService(
            Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(editText.getApplicationWindowToken(), 0);
    }
};

Edit: When I press key no "9" the keyboard hides. That's odd.

Paul Chu
  • 1,249
  • 3
  • 19
  • 27
Benny Skogberg
  • 10,431
  • 11
  • 53
  • 83

7 Answers7

61

Use android:imeOptions="actionDone", like that:

<EditText
    ...
    android:imeOptions="actionDone" />
alcsan
  • 6,172
  • 1
  • 23
  • 19
26
InputMethodManager inputManager = (InputMethodManager)
context.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.toggleSoftInput(0, 0);

with context being your activity.

Doge
  • 6,553
  • 5
  • 24
  • 25
  • 1
    Thanx for your effort! I changed the if-statement to if (event.getKeyCode() == KeyEvent.KEYCODE_ENTER) which made it working with the xml-attribute android:inputType="phone". I'll save your answer until next soft keyboard issue. BR - – Benny Skogberg Aug 05 '10 at 17:02
4

Changed the if-statement to if (event.getKeyCode() == KeyEvent.KEYCODE_ENTER) made it working with the xml-attribute android:inputType="phone".

Benny Skogberg
  • 10,431
  • 11
  • 53
  • 83
1

You should have a look at setOnEditorActionListener() for the EditText:

Set a special listener to be called when an action is performed on the text view. This will be called when the enter key is pressed, or when an action supplied to the IME is selected by the user.

Stan
  • 2,151
  • 1
  • 25
  • 33
1

SoftKeyboard can be hide by following way

In Java class we can write following code to hide keyboard when user press done or enter

etBid.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_SEARCH ||
                    actionId == EditorInfo.IME_ACTION_DONE ||
                    event != null &&
                            event.getAction() == KeyEvent.ACTION_DOWN &&
                            event.getKeyCode() == KeyEvent.KEYCODE_ENTER)
            {
                if (event == null || !event.isShiftPressed())
                {
                    // the user is done typing.
                    InputMethodManager imm = (InputMethodManager)v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
                    return true; // consume.
                }
            }
            return false; // pass on to other listeners.
        }
0

Use below code with android:imeOptions="actionDone" its work for me.

 <EditText
    android:id="@+id/et_switch_name"       
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:hint="Name"
    android:imeOptions="actionDone"       
    android:inputType="textPersonName" />
Sagar Jethva
  • 986
  • 12
  • 26
0

<EditText ... android:inputType="text" android:imeOptions="actionDone" />

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 17 '21 at 09:05