I want to catch an event when the user finishes editing EditText.
How can it be done?
Better way, you can also use EditText onFocusChange listener to check whether user has done editing: (Need not rely on user pressing the Done or Enter button on Soft keyboard)
((EditText)findViewById(R.id.youredittext)).setOnFocusChangeListener(new OnFocusChangeListener() { @Override public void onFocusChange(View v, boolean hasFocus) { // When focus is lost check that the text field has valid values. if (!hasFocus) { { // Validate youredittext } } });
Note : For more than one EditText, you can also let your class implement View.OnFocusChangeListener
then set the listeners to each of you EditText and validate them as below
((EditText)findViewById(R.id.edittext1)).setOnFocusChangeListener(this); ((EditText)findViewById(R.id.edittext2)).setOnFocusChangeListener(this); @Override public void onFocusChange(View v, boolean hasFocus) { // When focus is lost check that the text field has valid values. if (!hasFocus) { switch (view.getId()) { case R.id.edittext1: // Validate EditText1 break; case R.id.edittext2: // Validate EditText2 break; } } }
When the user has finished editing, s/he will press Done
or Enter
((EditText)findViewById(R.id.youredittext)).setOnEditorActionListener( new EditText.OnEditorActionListener() { @Override public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { if (actionId == EditorInfo.IME_ACTION_SEARCH || actionId == EditorInfo.IME_ACTION_DONE || event != null && event.getAction() == KeyEvent.ACTION_DOWN && event.getKeyCode() == KeyEvent.KEYCODE_ENTER) { if (event == null || !event.isShiftPressed()) { // the user is done typing. return true; // consume. } } return false; // pass on to other listeners. } } );
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With