Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setOnEditorActionListener not working with soft keyboard submit button, but does with laptop Enter key?

Tags:

android

Can someone please provide a solution to get a working listener for the soft-keyboard DONE button, and/or explain what I'm doing wrong in my current approach?

My Xml & Java Setup

  • In Xml, there's a simple EditText is set with android:imeOptions="actionDone"
  • In Java, it just has a basic TextView.OnEditorActionListener declared

So setOnEditorActionListener() is not getting called when there is a click from device soft-keyboard submit (aka DONE) button - the green arrow button below ; only the EditText field is cleared

enter image description here

But that listener DOES get called when clicking the computer Enter key (attached via ADB).

I would figure it should work for both those buttons .. is this not right?

XML Layout File:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.demo.MainActivity">

    <EditText
        android:id="@+id/comment_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:maxLines="1"
        android:imeOptions="actionDone"
        />    
</LinearLayout>

Java Activity File:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ((EditText) findViewById(R.id.comment_text)).setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                int i = 5; // added this to set a break point
                return false;
            }
        });
    }
}

Update for Solution

Thanks to @Apoorv Mehrotra's answer, it turns out that my EditText was missing this one attribute in order for the soft keyboard event to be recognized. Adding this to the above Xml solves the problem.

android:inputType="text"

like image 252
Gene Bo Avatar asked Jan 02 '17 07:01

Gene Bo


2 Answers

I had the same problem, for me, adding android:singleLine="true" in EditText did the trick

like image 132
Monica Ivan Avatar answered Sep 22 '22 15:09

Monica Ivan


try this code this is a code with 2 fields in which i have user time feature. in first one keyboard shows next button and in second one soft keypad shows done option and on done option i hide the soft keypad.

below is the xml file code.

  <EditText
                android:id="@+id/txt1"
                android:layout_width="match_parent"
                android:layout_height="100dp"
                android:gravity="top|left"
                android:imeOptions="actionNext"
                android:inputType="text|textNoSuggestions"
                android:text=""
                android:visibility="visible" />

    <EditText
                android:id="@+id/txt2"
                android:layout_width="match_parent"
                android:layout_height="100dp"
                android:gravity="top|left"
                android:imeOptions="actionDone"
                android:inputType="text|textNoSuggestions"
                android:text=""
                android:visibility="visible"
                />

now below is the java code: for hiding soft keypad on next and same can be performed for done option as well.

this code add in onCreate() of your class.

    EditText first_txt = (EditText) findViewById(R.id.txt1);
    EditText second_txt = (EditText) findViewById(R.id.txt2);

    first_txt.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_NEXT) {

                InputMethodManager imm = (InputMethodManager) getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(v.getWindowToken(), 0);

                return true; // Focus will do whatever you put in the logic.
            }
            return false;  // Focus will change according to the actionId
        }
    });

second_txt.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_DONE) {

                InputMethodManager imm = (InputMethodManager) getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(v.getWindowToken(), 0);

                return true; // Focus will do whatever you put in the logic.
            }
            return false;  // Focus will change according to the actionId
        }
    });

This code works for , hope it helps you out!

like image 31
Apoorv Mehrotra Avatar answered Sep 19 '22 15:09

Apoorv Mehrotra