1

I created an activity which should have the keypad always popped up. So, I used requestFocus() for the edittext. But I don't want only keypad disappear. Instead I want the whole activity to finish() when back button is pressed even when keypad is present. I tried this answer, but it didn't work. I tried this too though it looked a bit extra work, but this isn't working with textwatcher. Below is the edittext. I kept it's dimension attributes to 0dp because to avoid showing user even the password field. Any help on achieving this is appreciated. Please let me know if any further information is needed.

<EditText
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:id="@+id/passwordet"
        android:inputType="number"
        /> 

P.S: Overriding onBackPressed() doesn't work because it doesn't get fired when keypad is present on Activity.

Community
  • 1
  • 1
Kanth
  • 6,681
  • 3
  • 29
  • 41

3 Answers3

0

You can override the onBackPressed method to finish the activity:

@Override
public void onBackPressed() {
    finish();
}
G.T.
  • 1,557
  • 1
  • 12
  • 24
  • I would have felt happy if you had read my question entirely. The onBackPressed() doesn't get fired when the keypad is present. – Kanth Jul 09 '14 at 17:24
0

Extend EditText and add following method.

public class MyEditText extends EditText {

OnKeyListener onKeyListener = null;
public static int BACK_KEY = 10001;

public void setOnKeyListener(OnKeyListener l) {
    super.setOnKeyListener(l);
    onKeyListener = l;
    Log.i(TAG, "setOnKeyListener Called");
}

public boolean onKeyPreIme(int keyCode, KeyEvent event) {
    Log.i(TAG, "onKeyPreIme");
    if (keyCode == KeyEvent.KEYCODE_BACK
            && event.getAction() == KeyEvent.ACTION_UP) {
        if (onKeyListener != null) {
            onKeyListener.onKey(this, BACK_KEY, event);
        }
        return false;
    }
    return super.dispatchKeyEvent(event);
}

}

And from activity editText.setOnKeyListener(this); and handle on onKey method

Ganesh Kanna
  • 2,269
  • 1
  • 19
  • 29
  • Can you please kindly read my question? I linked this answer already in my question and it didn't work. – Kanth Jul 09 '14 at 17:40
  • Boss, onBackKeyPressed wont get called if the keyboard is visible .. To get notified on back key we need to do this way.. Display a toast on OnKeyPrime method first and confirm whether its getting called. And then u need to use your own call back or any thing to make it notified to activity.. If you want full source code.. post your sample application i can do that. – Ganesh Kanna Jul 10 '14 at 04:43
0

You need to create a custom EditText in order to capture keyBoardDismissEvent.

You can try something like this:

public class CustomEditText extends EditText {
    private OnKeyboardDismissListener listener;

    public void setOnKeyBoardDismissListener(OnKeyboardDismissListener listener) {
        this.listener = listener;
    }

    public CustomEditText(Context context) {
        super(context);
    }

    @Override
    public boolean onKeyPreIme(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && listener != null)
            listener.onKeyBoardDismiss();
        return super.onKeyPreIme(keyCode, event);
    }

    static interface OnKeyboardDismissListener {
        void onKeyBoardDismiss();
    }

}

In layout

 <YourPackageName.CustomEditText  android:id="@+id/editText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

Inside Activity:

CustomEditText editText = (CustomEditText) findViewById(R.id.editText);
        editText.setOnKeyBoardDismissListener(new OnKeyboardDismissListener() {
            @Override
            public void onKeyBoardDismiss() {
                MainActivity.this.finish();
            }
        });
vipul mittal
  • 17,343
  • 3
  • 41
  • 44