From fe9147d490c0772919a6c1645818c97266ed6f6d Mon Sep 17 00:00:00 2001 From: Thodoris1999 Date: Sun, 5 Aug 2018 10:19:16 +0300 Subject: [PATCH] add backspace behavior --- .../gr/thmmy/mthmmy/utils/EmojiKeyboard.java | 31 +++++++++++++++---- 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/gr/thmmy/mthmmy/utils/EmojiKeyboard.java b/app/src/main/java/gr/thmmy/mthmmy/utils/EmojiKeyboard.java index 3a83ca5f..193ed928 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/utils/EmojiKeyboard.java +++ b/app/src/main/java/gr/thmmy/mthmmy/utils/EmojiKeyboard.java @@ -1,17 +1,18 @@ package gr.thmmy.mthmmy.utils; import android.content.Context; +import android.os.Handler; import android.support.v7.widget.AppCompatImageButton; import android.text.TextUtils; import android.util.AttributeSet; import android.util.SparseArray; import android.view.LayoutInflater; +import android.view.MotionEvent; import android.view.inputmethod.InputConnection; import android.widget.GridView; import android.widget.LinearLayout; import gr.thmmy.mthmmy.R; -import timber.log.Timber; public class EmojiKeyboard extends LinearLayout { @@ -144,12 +145,30 @@ public class EmojiKeyboard extends LinearLayout { inputConnection.commitText(value, 1); }); AppCompatImageButton backspaceButton = (AppCompatImageButton) findViewById(R.id.backspace_button); - backspaceButton.setOnClickListener(view -> { - CharSequence selectedText = inputConnection.getSelectedText(0); - if (TextUtils.isEmpty(selectedText)) + // backspace behavior + final Handler handler = new Handler(); + Runnable longPressed = new Runnable() { + @Override + public void run() { inputConnection.deleteSurroundingText(1, 0); - else - inputConnection.commitText("", 1); + handler.postDelayed(this, 50); + } + }; + backspaceButton.setOnTouchListener((v, event) -> { + switch (event.getAction()) { + case MotionEvent.ACTION_DOWN: + CharSequence selectedText = inputConnection.getSelectedText(0); + if (TextUtils.isEmpty(selectedText)) + inputConnection.deleteSurroundingText(1, 0); + else + inputConnection.commitText("", 1); + handler.postDelayed(longPressed, 400); + break; + case MotionEvent.ACTION_UP: + handler.removeCallbacks(longPressed); + break; + } + return true; }); }