Search code examples
androidandroid-edittexttextwatcher

Clear EditText data inside beforeTextChanged() of TextWatcher


I have to watch EditText in my App in such a way that if in EditText data is in 2 lines then again I want to write in 3rd line then previous 2 line's data should get clear.

For this I am using following way to do

mTextWatcher = new TextWatcher() {

        private int lines;

        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
            Log.v("", "inside ontextchnaged");

        }

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

            lines = getTotalLines(txtDataSource);
            if (lines == 2) {                   

                   txtDataSource.removeTextChangedListener(mTextWatcher);
                Log.v("", txtDataSource.getText().toString());
                txtDataSource.setText("");
                txtDataSource.addTextChangedListener(mTextWatcher);

            }
        }

        @Override
        public void afterTextChanged(Editable s) {
            Log.v("", "inside aftertextchanged");

        }
    };

    txtDataSource.addTextChangedListener(mTextWatcher);

and I am getting no of lines enter by the below code

private int getTotalLines(EditText editText) {
    int lineNumber = 0;
    String text = editText.getText().toString()
            .substring(0, editText.getSelectionStart());

    for (int i = 0; i < text.length(); i++) {
        if (String.valueOf(text.charAt(i)).equalsIgnoreCase("\n")) {
            lineNumber++;
        }
    }

    return lineNumber;
}

So when i m getting 2 lines data in EditText then I am removing watcher from EditText then clearing data of EditText and again I added watcher on EditText .

Every thing working fine but problem is after 2 lines when I start to wrote then it is not writing first character which I enter from Keypad it starts writing from second character input.

So after 2 lines when enter 2 characters then it didnt write first char which i enter it starts writing from second character.

Please help me to resolve this.

Thanks ....


Solution

  • Finally I solved my issue.

    It may not be standard way to do, but I didn't find any other way to do this inside beforeTextChanged()

    I solved using the below code inside onTextChanged()

         @Override
         public void onTextChanged(CharSequence s, int start, int before,
                    int count) {
    
     lines = getTotalLines(txtDataSource);
    
      if (lines == 2 ) {
    
         String frstChar = String.valueOf(s.toString().charAt(s.toString().length() - 1));
         txtDataSource.removeTextChangedListener(mTextWatcher);
    
     txtDataSource.setText("" + frstChar);
    
         txtDataSource.setSelection(txtDataSource.getText().toString().length());
    
     txtDataSource.addTextChangedListener(mTextWatcher);
    
    }
       }
    

    and now in 3rd line first character is getting displayed in EditText.

    I am sharing my way so that if someone else would face such issue they can solve by this way..

    Thanks