0

I am trying to add a functionality to an existing app, The Functionality is to add a specific symbol with a space, while the user is typing. i have already tried this with afterTextChaanged(); What i want to do is Supporse the user types : i am fine thank you, then it should be automatically converted to : @i @am @fine @thankyou. can you please help me with this? Many thanks in advance.

Bhavik Mehta
  • 573
  • 8
  • 21

2 Answers2

2
edittext.addTextChangedListener(new TextWatcher(){
public void afterTextChanged(Editable s) {
    edittext.removeTextChangedListener(this);
    String oldstr=edittext.getText().toString();
    String newstr=oldstr.replaceAll("\\s+","@");
    edittext.setText(newstr);
    edittext.removeTextChangedListener(this);
      }
public void beforeTextChanged(CharSequence s, int start, int count, int after)  {

       }

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


     }
}); 
sasikumar
  • 12,540
  • 3
  • 28
  • 48
0

Have a look here.

You should be able to replace all the spaces with something like ' @'.

Just call

string = string.replace(" ", " @"); 

in your listener.

Of course, you should also take care of the first word, which (typically) should not have any space before. You could use a placeholder to do so, like

<string name="converted_string">@%1$s</string>

then in java

your_textView.setText(getString(R.string.converted_string, yourNewReplaceString));
Community
  • 1
  • 1
Lampione
  • 1,622
  • 3
  • 21
  • 39