Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide Soft keyboard on return key press

I've searched half a dozen other answers on SO, but haven't found one that works. All I'm trying to do is dismiss the soft keyboard when the user presses the enter button. (The equivalent of the insanely easy iOS 'resignKeyboard' call.) In the following code, the onEditorAction method does not get called. I've set up an EditText view in my XML file and the code in my fragment is as follows:

@Override     public View onCreateView(LayoutInflater inflater, ViewGroup container,                              Bundle savedInstanceState) {         fragView = inflater.inflate(R.layout.fragment_encrypt2, container, false);         textField = (EditText) fragView.findViewById(R.id.textField);          textField.setOnEditorActionListener(new TextView.OnEditorActionListener() {              @Override             public boolean onEditorAction(TextView arg0, int actionId,                                           KeyEvent arg2) {                 // hide the keyboard and search the web when the enter key                 // button is pressed                 if (actionId == EditorInfo.IME_ACTION_GO                         || actionId == EditorInfo.IME_ACTION_DONE                         || actionId == EditorInfo.IME_ACTION_NEXT                         || actionId == EditorInfo.IME_ACTION_SEND                         || actionId == EditorInfo.IME_ACTION_SEARCH                         || (arg2.getAction() == KeyEvent.KEYCODE_ENTER)) {                     InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);                     imm.hideSoftInputFromWindow(textField.getWindowToken(), 0);                      return true;                 }                 return false;             }         });          // Inflate the layout for this fragment         return fragView;//inflater.inflate(R.layout.fragment_encrypt2, container, false);     } 

The following is a snippet from the XML file where I define the EditText field. I need EditText to be multiline.

<EditText             android:id="@+id/textField"             android:layout_width="match_parent"             android:layout_height="match_parent"             android:hint="@string/Encrypted_Text_Hint"             android:gravity="top"             android:inputType="textMultiLine"             android:imeOptions="actionDone"/> 
like image 356
cph2117 Avatar asked Oct 30 '14 04:10

cph2117


People also ask

How do I hide my soft keyboard?

Hiding the Soft Keyboard Programmatically You can force Android to hide the virtual keyboard using the InputMethodManager, calling hideSoftInputFromWindow, passing in the token of the window containing your edit field. This will force the keyboard to be hidden in all situations.

How can I hide my keyboard while 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.

How do I hide my keyboard from clicking?

To hide keyboard, use the following code. InputMethodManager inputMethodManager = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE); inputMethodManager. hideSoftInputFromWindow(v. getApplicationWindowToken(),0);


2 Answers

If you don't want multiple line, for you edittext you can just specify a single line. For the edittext and also you can put imeOptions as Done like this:

<EditText     android:id="@+id/edittext_done"    android:layout_width="fill_parent"    android:layout_height="wrap_content"    android:imeOptions="actionDone"    android:singleLine="true"    /> 

I clearly don't know, whether you are trying to achieve this or not.Any way take a look.

EDIT: android:singleLine is deprecated since API 3 due to bad performance, you have to use android:maxLines instead. singleLine will be available because even now some effects are not supported by android:maxLines attribute.

like image 179
George Thomas Avatar answered Oct 09 '22 02:10

George Thomas


I solved the issue by changing the following in the XML file from:

 android:inputType="textMultiLine" 

to:

 android:inputType="textImeMultiLine" 
like image 34
cph2117 Avatar answered Oct 09 '22 02:10

cph2117