3

I have an activity where the whole screen is dedicated to sending one message. Being one EditText on the top half, and the SoftKeyboard always visible on the bottom half. When i press back, the SoftKeyboard hides and i have to press back again to leave the activity.

The behavior that i'm struggling to get is : finishing the activity right away when i press the back button, instead of hiding the keyboard. You can find this behavior in the twitter app for example, when writing a new tweet.

I tried with overriding the onBackPressed() function, but seems like when the keyboard is visible, the function is not called.

@Override
public void onBackPressed() {
     finish();
}

Any help would be really appreciated!

Gabriel Morin
  • 1,971
  • 1
  • 12
  • 22

5 Answers5

2

So after trying many things, here something that worked :

Subclass EditText and override the onKeyPreIme() function to send a call back. Here's the code for the subclass :

OnKeyPreImeListener onKeyPreImeListener;

public void setOnKeyPreImeListener(OnKeyPreImeListener onKeyPreImeListener) {
    this.onKeyPreImeListener = onKeyPreImeListener;
}

@Override
public boolean onKeyPreIme(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) {
        if(onKeyPreImeListener != null)
            onKeyPreImeListener.onBackPressed();
        Log.d(TAG, "HIDING KEYBOARD");
        return false;
    }
    return super.dispatchKeyEvent(event);
}

public interface OnKeyPreImeListener {
    void onBackPressed();
}

Then in your activity for each of your TextView :

EditTextGraphee.OnKeyPreImeListener onKeyPreImeListener = 
        new EditTextGraphee.OnKeyPreImeListener() {
        @Override
        public void onBackPressed() {
            Log.d(TAG, "CALL BACK RECEIVED");
            MyActivity.this.onBackPressed();
        }
    };
editText.setOnKeyPreImeListener(onKeyPreImeListener);
Gabriel Morin
  • 1,971
  • 1
  • 12
  • 22
1

new answer:

so apparently you don't receive the onBackPressed callback, but that doesn't mean you can't detect the keyboard closing.

Using the technique described here: How to check visibility of software keyboard in Android?

you can detect when the keyboard open/close, so when the keyboard closes you call finish();

deprecated, original answer:

simply override the back press event in the activity:

@Override
public void onBackPressed() {
   super.onBackPressed();
   finish();
}
Community
  • 1
  • 1
Budius
  • 37,587
  • 15
  • 92
  • 134
  • Yes i actually tried it also before! It's "kind-of" working, it's triggered when the keyboard is hidden, but also when shown, and on some other more random events. I can't find for now how to make sure this event is triggered because of a hidden keyboard and not something else. – Gabriel Morin Nov 12 '15 at 14:33
  • On that answer the code is very "bare bones". I shared on gist the code I use for keyboard detector, it's pretty reliable. https://gist.github.com/anonymous/1d5fee1d25c1f1b04dbb give it a try – Budius Nov 12 '15 at 14:48
  • Thanks a lot, i actually found another way to make it work ! Finally ! I'm gonna post it here – Gabriel Morin Nov 12 '15 at 15:04
1

I assume that since the soft keyboard is visible probably an edittext has a focus. So you can catch the back pressed event by adding an OnEditorActionListener on that EditText and finish activity.

yourEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {

                if (event.getAction() == KeyEvent.ACTION_UP){

                    if (event.getKeyCode() == KeyEvent.KEYCODE_BACK){
                        finish();
                    }

                }
                return false;

            }
        });
z3n105
  • 1,630
  • 11
  • 13
0

You nee to extend EdtText class and implement onKeyPreIme method.

public class MyEditText extends EditText {
/* Must use this constructor in order for the layout files to instantiate the class properly */
public MyEditText(Context context, AttributeSet attrs)
{
    super(context, attrs);
    // TODO Auto-generated constructor stub
}

@Override
public boolean onKeyPreIme (int keyCode, KeyEvent event)
{
    // do your stuff here.
    return true;
}

}

The Dude
  • 357
  • 2
  • 9
  • Sadly no, i tried, even if i put a Log in this function, it just doesn't call onBackPressed when the keyboard is visible. – Gabriel Morin Nov 12 '15 at 13:45
-1

Override onBackPressed() method like this :

 @Override
public void onBackPressed() {
   hideKeyboard();
   finish();
}

For hideKeyboard() function please search in the Internet .

Soheil Tayyeb
  • 317
  • 2
  • 13
  • Tried it, but the issue seems to be that it just does not call the onBackPressed() on this activity. The only moment it gets called is when i tap two times quickly the back button. If i tap once, the keyboard hides and it doesn't call the onBackPressed(), if i wait a bit after the keyboard is hidden, it finish() the activity, but without even calling the onBackPressed() function. – Gabriel Morin Nov 12 '15 at 14:03