5

How to set these options 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 automatically replace the key OK by the key Enter. Does anyone know if it is possible to have both keys ?

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

Community
  • 1
  • 1
cleroo
  • 1,145
  • 2
  • 11
  • 17

4 Answers4

4

Hi i am also face the same issue finally i got solution for this.

change

android:inputType="textMultiLine"

to

android:inputType="text"

AND

Inside the .java file access EditText using id

editText.setHorizontallyScrolling(false);

editText.setMaxLines(3);

And Now implement the OnEditorAction over the 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;
        }
    });
saibaba vali
  • 2,641
  • 1
  • 17
  • 17
  • It seems like setting line number and scrolling in layout doesn't work and this must be done in the code as shown above. – steven smith May 28 '22 at 02:21
1

The only thing guaranteed is that Android will pass the inputType and imeOptions to the IME. What the IME does with them is implementation-dependent. Where some IMEs may decide there's sufficient screen real estate to display both keys when in multi-line mode, that behavior shouldn't be relied upon.

Blrfl
  • 6,817
  • 1
  • 25
  • 25
0

Have written an answer here for similar question : https://stackoverflow.com/a/42236407/7550472 and it turned out to be the saving grace for me as nothing else worked. For easier access, i'll paste the code here too for lazy people like me ;).

In your 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;
        }
});

The minLines defined in XML will remain the same while the other two attributes are not required as its handled in the Java code.

Community
  • 1
  • 1
Kaushik NP
  • 6,733
  • 9
  • 31
  • 60
0

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

This question was answered at https://stackoverflow.com/a/5037488/7403656 however I made a little alteration which gets the imeOption from the xml instead of just setting it to the Done option.

@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;
}
Community
  • 1
  • 1
Anthony Cannon
  • 1,245
  • 9
  • 20