You can determine whether the user is typing or not you have to create one CustomEditText class which extends to EditText.  This type of functionality is widely used in Chat application. Here is a snippet of CustomEditText

public class CustomTypingEditText extends CustomEditTextNormal implements TextWatcher {
    private static final int TypingInterval = 800;

    //your listener interface that you implement anonymously from the Activity
    public interface OnTypingModified {
        void onIsTypingModified(EditText view, boolean isTyping);
    }

    private OnTypingModified typingChangedListener;

    //members you would need for the small thread that declares that you have stopped typing
    private boolean currentTypingState = false;
    private Handler handler = new Handler();
    private Runnable stoppedTypingNotifier = new Runnable() {
        @Override
        public void run() {
            //part A of the magic...
            if (null != typingChangedListener) {
                typingChangedListener.onIsTypingModified(CustomTypingEditText.this, false);
                currentTypingState = false;
            }
        }
    };

    public CustomTypingEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.addTextChangedListener(this);

    }


    public CustomTypingEditText(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        this.addTextChangedListener(this);

    }

    public CustomTypingEditText(Context context) {
        super(context);
        this.addTextChangedListener(this);
    }

    public void setOnTypingModified(OnTypingModified typingChangedListener) {
        this.typingChangedListener = typingChangedListener;
    }

    @Override
    public void afterTextChanged(Editable s) {
        //part B of the magic...
        if (null != typingChangedListener) {
            if (!currentTypingState) {
                typingChangedListener.onIsTypingModified(this, true);
                currentTypingState = true;
            }

            handler.removeCallbacks(stoppedTypingNotifier);
            handler.postDelayed(stoppedTypingNotifier, TypingInterval);
        }
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    }


    @Override
    public void onTextChanged(CharSequence text, int start, int before, int after) {
    }

}

Here is an XML declaration

<yourpackagename.CustomTypingEditText
    android:id="@+id/etMessage"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:hint="Type here"
    android:inputType="textCapSentences"/>

And implement its method

etMessage.setOnTypingModified(new CustomTypingEditText.OnTypingModified() {

    @Override
    public void onIsTypingModified(EditText view, boolean isTyping) {

        if(isTyping){
             Log.i(TAG, "onIsTypingModified: User started typing.");
        }else{
             Log.i(TAG, "onIsTypingModified: User stopped typing");
        }
     }

});

 

You may also like

Leave a Reply