Detect dismiss keyboard event in Android
Asked Answered
L

1

7

I have a edittext which needs to function like the textfield in iOS. When I click on it, should become editable else it should be disabled. Let's say the user wants to edit a certain value and when he presses the back button, along with the keyboard dismiss I want the edittext to become disabled to edit.

All I am using is the setCursorVisible(true) and setCursorVisible(false) method. I did try using keyCode events but they aren't helping. This is what I have tried up until now:

@Override
    public void onBackPressed() {
// Had the InputMethodManager service here..
        if(imm.isAcceptingText())
        {
           Toast.makeText(ProfileActivity.this,"working",Toast.LENGTH_SHORT).show();
        }
else {
            super.onBackPressed();
            Toast.makeText(ProfileActivity.this,"back pressed called",Toast.LENGTH_SHORT).show();
        }
    }

Also tried overriding keyDown event.

@Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        //replaces the default 'Back' button action
        if(keyCode==KeyEvent.KEYCODE_BACK)
        {

            Log.d("123","called,called");
            return  true;

        }
        return super.onKeyDown(keyCode, event);
    }
Limousine answered 20/4, 2015 at 9:41 Comment(0)
F
19

How about creating a subclass of EditText and overriding its onKeyPreIme method

/**
 * This class overrides the onKeyPreIme method to dispatch a key event if the
 * KeyEvent passed to onKeyPreIme has a key code of KeyEvent.KEYCODE_BACK.
 * This allows key event listeners to detect that the soft keyboard was
 * dismissed.
 *
 */
public class ExtendedEditText extends EditText {

    public ExtendedEditText(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

    }

    public ExtendedEditText(Context context, AttributeSet attrs) {
        super(context, attrs);

    }

    public ExtendedEditText(Context context) {
        super(context);

    }

    @Override
    public boolean onKeyPreIme(int keyCode, KeyEvent event) {
        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
            dispatchKeyEvent(event);
            return false;
        }
        return super.onKeyPreIme(keyCode, event);
    }

}
Furculum answered 20/4, 2015 at 9:46 Comment(7)
@Override public boolean onKeyPreIme(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { // User has pressed Back key. So hide the keyboard InputMethodManager mgr = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE); mgr.hideSoftInputFromWindow(this.getWindowToken(), 0); // TODO: Hide your view as you do it in your activity } return false; }Limousine
Did try to use this...but the activity gets finished as soon as back is pressed.I want the activity to get finished on the second time I press back..On first backpress,Dismiss keyboard and setCursorVisible(false) is what i am expecting.Limousine
You mean like this : #8431305 ?Furculum
@Slartibastfast No no not in that manner...may be your solution will work..trying it..I will have to override dispatchEvent in the activity and detect the event there?..right?Limousine
@Slartibastfast My requirement is that after editting when I press back button the edittext should get disabled.Limousine
Did return true instead of return false, and it solved my problem of activity getting finished. Nice solution..!!!Gyratory
Add if (event.keyCode == KeyEvent.KEYCODE_BACK && event.action == ACTION_UP) { to call the function only oncePaapanen

© 2022 - 2024 — McMap. All rights reserved.