Skip to content

Instantly share code, notes, and snippets.

@acnvrk
Created March 30, 2021 02:20
Show Gist options
  • Save acnvrk/5de5f954c65273349c2ee4835bfe78d2 to your computer and use it in GitHub Desktop.
Save acnvrk/5de5f954c65273349c2ee4835bfe78d2 to your computer and use it in GitHub Desktop.
Troubleshoot EditText-setOnEditorActionListener
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:id="@+id/layout_memo"
android:layout_width="fill_parent"
android:layout_height="120dp"
android:paddingLeft="20sp"
android:focusable="true" android:focusableInTouchMode="true"
android:descendantFocusability="beforeDescendants"
>
<TextView
android:id="@+id/label_memo"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="0.70"
android:gravity="center_vertical"
android:textSize="24sp"
android:text="Memo" />
<EditText
android:id="@+id/memo_edit_text"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="0.30"
android:textSize="24sp"
android:inputType="text|textMultiLine|textCapSentences"
android:gravity="top|left"
android:maxLines ="4"
android:maxLength ="2000"
android:scrollHorizontally="false"/>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
package com.example.sampleapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initMemoField(findViewById(R.id.memo_edit_text));
}
void initMemoField(final EditText editText) {
editText.setImeOptions(EditorInfo.IME_ACTION_DONE);
editText.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
Log.d("Main", "Current Text: " + s.toString());
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
public void onTextChanged(CharSequence s, int start, int before, int count) {}
});
editText.setOnEditorActionListener((v, actionId, event) -> {
Log.v("Main", "This listener does not get triggered.");
if(actionId== EditorInfo.IME_ACTION_DONE){
Log.v("Main", "Expect to reach here when Keyboard closed.");
editText.clearFocus();
return true;
}
return false;
});
editText.clearFocus();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment