EditText onclick action only executed when refocussing the EditText

69 views Asked by At

I am trying to create a simple editText for which I defined a simple onclick action that outputs the name to the window again. My problem is, that it seems like this action is only called when I refocus the editText again. So I focus the editText, type in my name and hit enter. Now the window closes but the name is not put to the window. When I focus the editText again, then the name is put to the window.

When I do exactly the same without having the android:imeOptions="actionDone" set in the XML, everything works fine (except of the keyboard does not close, which does not close of course)

My editText from the XML:

<EditText
            android:id="@+id/name_edit_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="16dp"
            android:hint="Name"
            android:imeOptions="actionDone"
            android:inputType="text"
            android:onClick="readName" />

My readName() method:

public void readName(View view) {
        EditText nameEditText = (EditText) findViewById(R.id.name_edit_text);
        String nameWorking = nameEditText.getText().toString();
        name = nameWorking;
        displayOrderSummary();
    }
2

There are 2 answers

0
remedy. On

Not entirely sure what your entire activity looks like, but I believe that if you remove the binding outside of your readName() method, it should work fine.

Place the code below into your onCreate().

EditText nameEditText = (EditText) findViewById(R.id.name_edit_text);

1
Ernanni On

Instead of using android:onClick you may put the following inside the onCreate() method:

EditText nameEditText = (EditText) findViewById(R.id.name_edit_text);
nameEditText.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        readName(v)
    }
}