Ishika Sharma
Ishika Sharma

Reputation: 55

TextWatcher makes Typing slow in Edittext(Android)

The following code i am using for getting status of typing in Text Changes function.....

mEditText.addTextChangedListener(new TextWatcher() {
        public void afterTextChanged(Editable s) {
            try {
                session.typing();
            } catch (OmegleException e) {
                e.printStackTrace();
            }
        }
        public void beforeTextChanged(CharSequence s, int start, 
                int count, int after) {

        }
        public void onTextChanged(CharSequence s, int start, 
                int before, int count) {

        }
    });

Upvotes: 2

Views: 2949

Answers (2)

KishuDroid
KishuDroid

Reputation: 5451

It can be fixed by changing the EditText width to 0dp using weighted widths to 
match/fill the parent.

I don't know for sure why this was occurring, however I believe it is because when the width of the EditText is set to wrap content it will adjust/redraw itself so that everything fits. So by making the EditText have a fixed width, this redraw is no longer required.

What does the "0dp" signify for the width? That it should take up all the available space?

yes, it will. The layout_weight tag will do that.

 <EditText
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="5"/>

For you problem i have one advice for you :

Set your status as typing till user don't press done button on keyboard.

editText.setOnEditorActionListener(new EditText.OnEditorActionListener() {
@Override
public boolean onEditorAction(EditText v, int actionId, KeyEvent event) {
 if (actionId == EditorInfo.IME_ACTION_DONE) {
 //do here your stuff f
 return true;
 }
 return false;
} });

Upvotes: 0

Elltz
Elltz

Reputation: 10869

try something like this -guess is a chat app right so -

final Random ran = new Random();
mEditText.addTextChangedListener(new TextWatcher() {
    public void afterTextChanged(Editable s) {}
    public void beforeTextChanged(CharSequence s, int start, 
            int count, int after) {}
    public void onTextChanged(CharSequence s, int start, 
            int before, int count) {
             if(start %2 ==0 && ran.nextBoolean()){
                 try {
                   session.typing();
                 } catch (OmegleException e) {
                       e.printStackTrace();
                 }
    }
});

Edit

b4 i do your final request, do you mind runing session.typing on new Thread, like this

new Thread(new Runnable() {
            @Override
            public void run() {
              try {
                   session.typing();
                 } catch (OmegleException e) {
                       e.printStackTrace();
                 }
              }
           }).start();

Edit 2

on first letter input

mEditText.addTextChangedListener(new TextWatcher() {
    public void afterTextChanged(Editable s) {}
    public void beforeTextChanged(CharSequence s, int start, 
            int count, int after) {}
    public void onTextChanged(CharSequence s, int start, 
            int before, int count) {
             if(start ==1){
                 try {
                   session.typing();
                 } catch (OmegleException e) {
                       e.printStackTrace();
                 }
    }
});

Upvotes: 2

Related Questions