Search code examples
androidandroid-edittext

Android using TextWatcher to replace words


I have an editText, that you write in it something then when u click next .. the text u wrote gets written into another editText .. It's working perfectly .. But I want to use textWatcher to replace some letters..

Example: How do I make the S to be $ or the O to be @

UPDATE:

    final EditText First = (EditText)findViewById(R.id.etFirst);
    String strFirst = First.getText().toString();
    final TextView done = (TextView)findViewById(R.id.tvName);
    String strDone = done.getText().toString();
    Button Trans = (Button) findViewById(R.id.bTrans);

        Trans.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                //First EditText
                if (First.getText().toString().equals("John")) {
                    done.setText("JOHN");
                } else {
                if (First.getText().toString().equals("Ahmed")) {
                    done.setText("AHMED");
                } else  {
                done.setText(First.getText());
                                }
                            }
                        };
                    });

        First.addTextChangedListener(new TextWatcher() {
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

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

            public void afterTextChanged(Editable s) {
            done.setText(First.getText().toString().replaceAll("S", "$"));
            }
            });

my code will type what you wrote in the EditText to a Large TextView below .. when I press the button Trans it sets the text which you wrote but I want to replace some letters example S to $ .. when I type S nothing happens .. just S it don't gets to be $ ..

What am I doing wrong ?


Solution

  • Assign the Textwatcher to your editText then do something like

    editText.setText(editText.getText().toString().replaceAll("S", "$"));
    

    Here is the textWatcher with code applied

        final EditText First = (EditText)findViewById(R.id.etFirst);
        String strFirst = First.getText().toString();
        final TextView done = (TextView)findViewById(R.id.tvName);
        String strDone = done.getText().toString();
        Button Trans = (Button) findViewById(R.id.bTrans);
    
            Trans.setOnClickListener(new View.OnClickListener() {
    
                public void onClick(View v) {
                    //First EditText
                    if (First.getText().toString().equals("John")) {
                        done.setText("JOHN");
                    } else {
                    if (First.getText().toString().equals("Ahmed")) {
                        done.setText("AHMED");
                    } else  {
                    done.setText(First.getText());
                                    }
                                }
                            };
                        });
    
            First.addTextChangedListener(new TextWatcher() {
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                }
    
                public void beforeTextChanged(CharSequence s, int start, int count, int after)    {
                }
    
                public void afterTextChanged(Editable s) {
                    String text = First.getText().toString();
                    text = text.replace('s', 'd'); // Any of the diffrent replace methods should work.
                    text = text.replace('S', '$'); // This uses the char replace method. Note, the ' quotations
                    text = text.replace('O', '@');
                    text = text.replace('o', '@');
                    done.setText(text);
                }
            });
    

    Its possible that the error resided in the fact you are getting the old string again, and applying text.replace on it. Rather than the modified string. I would have expected it to have got the new string, but setText doesnt seem to fire immediately.

    So if you get the string into a variable, then apply all your changes. Then put it back to your text box it will work fine. (I have tested and working code here Pasted above)