Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to NOT close keyboard when DONE on keyboard is pressed

When the user presses "Done" on the soft keyboard, the keyboard closes. I want it so that it will only close if a certain condition is true (eg. the password was entered correctly).

This is my code (sets up a listener for when the "Done" button is pressed):

final EditText et = (EditText)findViewById(R.id.et);
et.setOnEditorActionListener(new OnEditorActionListener() 
{        
   @Override
   public boolean onEditorAction(TextView v, int actionId, KeyEvent event)
   {
      if(actionId==EditorInfo.IME_ACTION_DONE)
      {
         if (et.getText().toString().equals(password)) // they entered correct
         {
             // log them in
         }
         else
         {
             // bring up the keyboard
             getWindow().setSoftInputMode(
             WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);

             Toast.makeText(Main.this, "Incorrect.", Toast.LENGTH_SHORT).show();
         }
      }
      return false;
   }
});

I realize that the reason this doesn't work is probably because it runs this code before it actually closes the soft keyboard on its own, but that's why I need help. I don't know another way.

A possible topic for answers could be working with:

activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 

and that sort of thing, but I don't know for sure.


SOLUTION:

EditText et = (EditText)findViewById(R.id.et);
et.setOnEditorActionListener(new OnEditorActionListener() 
{        
  @Override
  public boolean onEditorAction(TextView v, int actionId, KeyEvent event)
  {
    if(actionId==EditorInfo.IME_ACTION_DONE)
    {
       if (et.getText().toString().equals(password)) // they entered correct
       {
           // log them in
           return false; // close the keyboard
       }
       else
       {
           Toast.makeText(Main.this, "Incorrect.", Toast.LENGTH_SHORT).show();
           return true; // keep the keyboard up
       }
    }
    // if you don't have the return statements in the if structure above, you
    // could put return true; here to always keep the keyboard up when the "DONE"
    // action is pressed. But with the return statements above, it doesn't matter
    return false; // or return true
  }
});
like image 776
Michael Yaworski Avatar asked Oct 18 '13 20:10

Michael Yaworski


People also ask

How do I hide the soft keyboard on Android after clicking outside EditText?

Ok everyone knows that to hide a keyboard you need to implement: InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE); imm. hideSoftInputFromWindow(getCurrentFocus(). getWindowToken(), 0);

How do I change the Done button on my Android keyboard?

First you need to set the android:imeOptions attribute equal to “actionDone” for your target EditText as seen below. That will change your 'RETURN' button in your EditText's soft keyboard to a 'DONE' button.

How do you hide the keyboard when typing?

Tap the back button on your Android. It's the left-pointing arrow button at the bottom of the screen, either at the bottom-left or bottom-right corner. The keyboard is now hidden. The back button may be a physical button or on the touch screen. To bring the keyboard back into view, tap the typing area.

How do I get rid of soft keyboard on Android?

Here pass HIDE_IMPLICIT_ONLY at the position of showFlag and 0 at the position of hiddenFlag . It will forcefully close soft Keyboard.


1 Answers

if your return true from your onEditorAction method, action is not going to be handled again. In this case you can return true to not hide keyboard when action is EditorInfo.IME_ACTION_DONE.

like image 99
Devrim Avatar answered Oct 12 '22 11:10

Devrim