2

I want to perform an action when enter is pressed in android editText. I have tried various ways of using EditorInfo.IME_ACTION_DONE, which works, but the problem is, it shows a tick icon, which I don't want. I want it to look like the default enter button on the keyboard.

How do I handle enter pressed event while retaining the "Enter" shape? Is there something like EditorInfo.IME_ACTION_ENTER to get my job done?

My approach (which shows tick icon):

editorEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
        if( (actionId == EditorInfo.IME_ACTION_DONE){
            Toast.makeText(EditorActivity.this, "working", Toast.LENGTH_SHORT).show();

            return true;
        }

        return false;
    }
});

I want it to look like "Enter" shape because I am making a text editor. I am trying to shift to another EditText on the "Enter" button is pressed and don't want it to look like a "Tick" button. That's why I don't want the tick symbol.

Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
Hissaan Ali
  • 2,229
  • 4
  • 25
  • 51

1 Answers1

1

I think just changing the inputType of your EditText in your layout should do the trick to have an "Enter" like button in your soft keyboard in Android.

<EditText
    android:id="@+id/edit"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:inputType="textMultiLine" />

Please note that I have used textMultiLine as the inputType of the EditText.

You can take some action on pressing the "Enter" button in your EditText like the following.

final EditText edittext = (EditText) findViewById(R.id.edit);

edittext.addTextChangedListener(new TextWatcher() {

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        if (before == 0 && count == 1 && s.charAt(start) == '\n') {
            String text = edittext.getText().replace(start, start + 1, "").toString(); // Removes the enter
            Toast.makeText(MainActivity.this, text, Toast.LENGTH_LONG).show();
        }
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    }

    @Override
    public void afterTextChanged(Editable s) {
    }
});

Hope that completes the answer.

Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
  • The thing is, i don't only want to change the keyboard to show enter, i want to listen for it too, this approach does make it look like enter but how i do listen for the enter press using this approach ? – Hissaan Ali Jul 17 '19 at 18:55
  • Can you please check the updated answer? Thanks. :) – Reaz Murshed Jul 17 '19 at 18:58
  • i tried it, but the onKey doesn't get called . nothing happens , – Hissaan Ali Jul 17 '19 at 19:14
  • I have updated the code. Can you please try with that again? And again, please try to put the debug logs to check if the function `setOnKeyListener` was called or not. Then you modify the conditions for the desired actions to be performed. – Reaz Murshed Jul 17 '19 at 19:17
  • i have already checked it. The onKey() isn't called somehow . if you could suggest something with EditorActionListener it would be more appreciative since official doc mention that some keyboards may lead to onKey() not being called ! – Hissaan Ali Jul 17 '19 at 19:23
  • Updated the code and tested by myself. This should work now. :) – Reaz Murshed Jul 17 '19 at 22:19
  • it works . can you explain why did you set `before == 0 && count == 1` ? – Hissaan Ali Jul 18 '19 at 17:33
  • 1
    Great to know that works. I want to share two links with you which might help you to understand. Here you go with the [link 1](https://stackoverflow.com/a/6003815/3145960) and [Link 2](https://developer.android.com/reference/android/text/TextWatcher). Please print out those values as well for better understanding. – Reaz Murshed Jul 18 '19 at 17:45