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);
    }
halfer
  • 19,824
  • 17
  • 99
  • 186
AndroidMech
  • 1,676
  • 3
  • 20
  • 31

1 Answers1

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);
    }

}
Vrashabh Irde
  • 14,129
  • 6
  • 51
  • 103
  • @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; } – AndroidMech Apr 20 '15 at 09:47
  • 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. – AndroidMech Apr 20 '15 at 09:49
  • 1
    You mean like this : http://stackoverflow.com/questions/8430805/android-clicking-twice-the-back-button-to-exit-activity ? – Vrashabh Irde Apr 20 '15 at 09:54
  • @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? – AndroidMech Apr 20 '15 at 09:57
  • @Slartibastfast My requirement is that after editting when I press back button the edittext should get disabled. – AndroidMech Apr 20 '15 at 09:58
  • 1
    Did return true instead of return false, and it solved my problem of activity getting finished. Nice solution..!!! – anurag_dake Dec 10 '15 at 10:16
  • Add `if (event.keyCode == KeyEvent.KEYCODE_BACK && event.action == ACTION_UP) {` to call the function only once – Oz Shabat Dec 22 '20 at 14:15