How to set MultiLine and imeOptions = "actionDone" for EditText on Android?

How to set these parameters at the same time:

  • android:minLines="3"
  • android:inputType="textMultiLine"
  • android:imeOptions="actionDone"

It seems as soon as I put android:inputType="textMultiLine" , the keyboard will automatically replace the OK key with the Enter key. Does anyone know if it is possible to have both keys?

NB: this answer is not what I am looking for. I need both keys.

+4
source share
4 answers

Hi, I am also facing the same problem, finally I got a solution for this.

change

 android:inputType="textMultiLine" 

to

 android:inputType="text" 

and

Inside .java file access EditText using id

 editText.setHorizontallyScrolling(false); editText.setMaxLines(3); 

And now we implement OnEditorAction over editText.

  editText.setOnEditorActionListener(new TextView.OnEditorActionListener() { @Override public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { if (actionId == 4) { //actionId 4 for actionDone And 6 for actionSend //perform action what you want return true; } else return false; } }); 
+2
source

The only thing guaranteed is that Android will pass the IME to inputType and imeOptions . What the IME does with them depends on the implementation. If some IMEs may decide that there is enough screen real estate to display both keys in multi-line mode, this behavior should not be relied on.

0
source

I wrote here the answer to a similar question: fooobar.com/questions/88366 / ... , and this turned out to be a blessing for me, because nothing else worked. For easier access, I will embed the code here too for lazy people like me;).

In Java code:

 ////////////Code to Hide SoftKeyboard on Enter (DONE) Press/////////////// editText.setRawInputType(InputType.TYPE_CLASS_TEXT|InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD|InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS); editText.setImeActionLabel("DONE",EditorInfo.IME_ACTION_DONE); //Set Return Carriage as "DONE" editText.setImeOptions(EditorInfo.IME_ACTION_DONE); editText.setOnEditorActionListener(new TextView.OnEditorActionListener() { @Override public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { if (event == null) { if (actionId == EditorInfo.IME_ACTION_DONE) { // Capture soft enters in a singleLine EditText that is the last EditText // This one is useful for the new list case, when there are no existing ListItems EditText.clearFocus(); InputMethodManager inputMethodManager = (InputMethodManager) getActivity().getSystemService(Activity.INPUT_METHOD_SERVICE); inputMethodManager.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), 0); } else if (actionId == EditorInfo.IME_ACTION_NEXT) { // Capture soft enters in other singleLine EditTexts } else if (actionId == EditorInfo.IME_ACTION_GO) { } else { // Let the system handle all other null KeyEvents return false; } } else if (actionId == EditorInfo.IME_NULL) { // Capture most soft enters in multi-line EditTexts and all hard enters; // They supply a zero actionId and a valid keyEvent rather than // a non-zero actionId and a null event like the previous cases. if (event.getAction() == KeyEvent.ACTION_DOWN) { // We capture the event when the key is first pressed. } else { // We consume the event when the key is released. return true; } } else { // We let the system handle it when the listener is triggered by something that // wasn't an enter. return false; } return true; } }); 

minLines defined in XML will remain the same, while the other two attributes are not required, since it is processed in Java code.

0
source

If you subclass EditText and insert this function, it should solve your problem.

This question was answered at fooobar.com/questions/20296 / ... , however I made a small change that gets imeOption from xml instead of just setting it to the Done parameter.

 @Override public InputConnection onCreateInputConnection(EditorInfo outAttrs) { InputConnection connection = super.onCreateInputConnection(outAttrs); int imeOptions = getImeOptions(); int imeActions = outAttrs.imeOptions & EditorInfo.IME_MASK_ACTION; if ((imeActions & imeOptions) != 0) { // clear the existing action outAttrs.imeOptions ^= imeActions; // set the DONE action outAttrs.imeOptions |= imeOptions; } if ((outAttrs.imeOptions & EditorInfo.IME_FLAG_NO_ENTER_ACTION) != 0) { outAttrs.imeOptions &= ~EditorInfo.IME_FLAG_NO_ENTER_ACTION; } return connection; } 
0
source

All Articles