0

I am using multi line edit-text, I restrict character not exceed 42 in Text Watcher but I cant able to delete char in my edit-text it moves to new line.How to delete characters and add new line if character in line exceeds the limit?

XML Code :

 <EditText
            android:id="@+id/edt_footer"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Thank you visit again"
            android:maxLines="3"
            android:lines="3"
            android:ems="18"
            android:scrollbars="vertical"
            android:inputType="textMultiLine"
            android:gravity="left|top"
            android:layout_marginLeft="@dimen/margin_20"
            android:textSize="20sp" /> 

Java Code:

footerEdt.addTextChangedListener(new TextWatcher(){
            @Override
            public void afterTextChanged(Editable s) {}
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {}
            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                                      int count) {
                if (count % 42 == 0)
                {
                    ((Editable) s).append("\n");
                    footerEdt.setSelection(footerEdt.getText().length());
                }

            }
        });
appukrb
  • 1,507
  • 4
  • 24
  • 53

1 Answers1

0

Try this

footerEdt.addTextChangedListener(new TextWatcher(){
        @Override
        public void afterTextChanged(Editable s) {}
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                                      int after) {}
        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                                  int count) {
            if (count % 42 == 0)
            {
                ((Editable) s).append("\n");
                footerEdt.setText(s);
                footerEdt.setSelection(footerEdt.getText().length());
            }

        }
    });
mehul chauhan
  • 1,792
  • 11
  • 26