Knowing when Edit text is done being edited

AndroidAndroid Edittext

Android Problem Overview


How do I know when my edit text is done being edited? Like when the user selects the next box, or presses the done button on the soft keyboard.

I want to know this so I can clamp the input. It looks like text watcher's afterTextChanged happens after each character is entered. I need to do some calculations with the input, so I would like to avoid doing the calculation after each character is entered.

thanks

Android Solutions


Solution 1 - Android

By using something like this

 meditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            switch (actionId){
                case EditorInfo.IME_ACTION_DONE:
                case EditorInfo.IME_ACTION_NEXT:
                case EditorInfo.IME_ACTION_PREVIOUS:
                    yourcalc();
                    return true;
            }
            return false;
        }
    });

Solution 2 - Android

EditText inherits setOnFocusChangeListener which takes an implementation of OnFocusChangeListener.

Implement onFocusChange and there's a boolean parameter for hasFocus. When this is false, you've lost focus to another control.

EDIT

To handle both cases - edit text losing focus OR user clicks "done" button - create a single method that gets called from both listeners.

    private void calculate() { ... }

    btnDone.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            calculate();
        }
    });

    txtEdit.setOnFocusChangeListener(new OnFocusChangeListener() {			
		
		public void onFocusChange(View v, boolean hasFocus) {
			if(!hasFocus)
				calculate();
		}
	});

Solution 3 - Android

Using an EditText object xml defined like this:

    <EditText
        android:id="@+id/create_survey_newquestion_editText_minvalue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:ems="4"
        android:imeOptions="actionDone"
        android:inputType="number" />

We can capture its text i) when the user clicks the Done button on the soft keyboard (OnEditorActionListener) or ii) when the EditText has lost the user focus (OnFocusChangeListener) which now is on another EditText:

	/**
	 * 3. Set the min value EditText listener
	 */
	editText= (EditText) this.viewGroup.findViewById(R.id.create_survey_newquestion_editText_minvalue);
	editText.setOnEditorActionListener(new OnEditorActionListener()
	{
		@Override
		public boolean onEditorAction(TextView v, int actionId, KeyEvent event)
		{
			String input;
			if(actionId == EditorInfo.IME_ACTION_DONE)
			{
				input= v.getText().toString();
				MyActivity.calculate(input);
				return true; // consume.
			}
            return false; // pass on to other listeners.
		}
	});
	editText.setOnFocusChangeListener(new View.OnFocusChangeListener()
	{
		@Override
		public void onFocusChange(View v, boolean hasFocus)
		{
			String input;
			EditText editText;
			
			if(!hasFocus)
			{
				editText= (EditText) v;
				input= editText.getText().toString();
				MyActivity.calculate(input);
			}
		}
	});

This works for me. You can hide the soft keyboard after made the calculations using a code like this:

private void hideKeyboard(EditText editText)
{
	InputMethodManager imm= (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
	imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
}

Edit: added return values to onEditorAction

Solution 4 - Android

I have done this simple thing, when focus is shifted from edit text get the text. This will work if user shifts the focus to select other views like a button or other EditText or any view.

        editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (!hasFocus) {
                EditText editText = (EditText) v;
                String text = editText.getText().toString();
             }
        }
    });

Solution 5 - Android

To be more highlevel you may use TextWatcher's afterTextChanged() method.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMattView Question on Stackoverflow
Solution 1 - AndroidRenoView Answer on Stackoverflow
Solution 2 - AndroidRichView Answer on Stackoverflow
Solution 3 - AndroidjsanmarbView Answer on Stackoverflow
Solution 4 - AndroidMurliView Answer on Stackoverflow
Solution 5 - Androiduser717593View Answer on Stackoverflow