1

I want to change on type a character in android. If user type space or - it will change it to _

So I tried:

TextWatcher tt = null;

final EditText etUsername = (EditText) findViewById(R.id.etUsername);
   tt = new TextWatcher() {
        public void afterTextChanged(Editable s){
            etUsername.setSelection(s.length());
        }
        public void beforeTextChanged(CharSequence s,int start,int count, int after){}
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            etUsername.removeTextChangedListener(tt);
            etUsername.setText(etUsername.getText().toString().replace(" ", "_"));
            etUsername.setText(etUsername.getText().toString().replace("-", "_"));
            etUsername.addTextChangedListener(tt);
        }
    };
    etUsername.addTextChangedListener(tt);

it "works" but if user types fast some letters will not appear and I got some warnings:

W/IInputConnectionWrapper: getSelectedText on inactive InputConnection
W/IInputConnectionWrapper: requestCursorAnchorInfo on inactive InputConnection
W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection
W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection
W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection
W/IInputConnectionWrapper: commitText on inactive InputConnection
W/IInputConnectionWrapper: endBatchEdit on inactive InputConnection
W/IInputConnectionWrapper: getSelectedText on inactive InputConnection
W/IInputConnectionWrapper: requestCursorAnchorInfo on inactive InputConnection
W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection
W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection
W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection
W/IInputConnectionWrapper: commitText on inactive InputConnection
W/IInputConnectionWrapper: endBatchEdit on inactive InputConnection
W/IInputConnectionWrapper: getSelectedText on inactive InputConnection
W/IInputConnectionWrapper: requestCursorAnchorInfo on inactive InputConnection
W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection
W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection
W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection
W/IInputConnectionWrapper: commitText on inactive InputConnection
W/IInputConnectionWrapper: endBatchEdit on inactive InputConnection
W/IInputConnectionWrapper: getSelectedText on inactive InputConnection
W/IInputConnectionWrapper: requestCursorAnchorInfo on inactive InputConnection
W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection
W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection
W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection
W/IInputConnectionWrapper: commitText on inactive InputConnection
W/IInputConnectionWrapper: endBatchEdit on inactive InputConnection
W/IInputConnectionWrapper: getSelectedText on inactive InputConnection
W/IInputConnectionWrapper: requestCursorAnchorInfo on inactive InputConnection
W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection
W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection
W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection
W/IInputConnectionWrapper: finishComposingText on inactive InputConnection
W/IInputConnectionWrapper: finishComposingText on inactive InputConnection

any ideas what is wrong?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
RGS
  • 4,062
  • 4
  • 31
  • 67

2 Answers2

1

Try this Textwatcher

  TextWatcher watch = new TextWatcher(){

    @Override
    public void afterTextChanged(Editable arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
            int arg3) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onTextChanged(CharSequence s, int a, int b, int c) {
        // TODO Auto-generated method stub

        output.setText(s);
        if(a == 9){
            Toast.makeText(getApplicationContext(), "Maximum Limit Reached", Toast.LENGTH_SHORT).show();
        }
    }};
ITSGuru
  • 194
  • 8
  • more guide available in android developer - [link](https://developer.android.com/reference/android/text/TextWatcher.html) – ITSGuru Mar 29 '17 at 08:39
1

No need to remove and add text change listener again and again on text change, just put a condition to check if your editable is having "-" or " " then just replace and set it to EditText.

etUsername.addTextChangedListener(new TextWatcher() {
            @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 (s.length() > 0 && (s.toString().contains("-") || s.toString().contains(" "))) {
                    etUsername.setText(s.toString().replace("-", "_").replace(" ", "_"));
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
                etUsername.setSelection(s.length());
            }
        });
Ready Android
  • 3,529
  • 2
  • 26
  • 40