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.
Asked
Active
Viewed 73 times
0

Bhavik Mehta
- 573
- 8
- 21
2 Answers
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
-
No Lucks, it makes a recursive call – Bhavik Mehta Feb 17 '16 at 11:06
-
what recursive call? – sasikumar Feb 17 '16 at 12:13
-
check my Edited answer – sasikumar Feb 17 '16 at 12:56
-
the issue is , it is working as normal edittext even after implementing your code – Bhavik Mehta Mar 01 '16 at 07:00
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));