I want to replace (.) with (,) run time by entering text in edittextview in android. i use textwatcher but it can't help me out.
Asked
Active
Viewed 62 times
3 Answers
1
Add a TextChangedListener to the EditText with the following afterTextChanged:
public void afterTextChanged(Editable s) {
double doubleValue = 0;
if (s != null) {
try {
doubleValue = Double.parseDouble(s.toString().replace(',', '.'));
} catch (NumberFormatException e) {
//Error
}
}
//Do something with doubleValue
}

Aspicas
- 4,498
- 4
- 30
- 53
0
Try this
textMessage.addTextChangedListener(new TextWatcher(){
public void afterTextChanged(Editable s) {
s.toString().replace(".",",");
}
public void beforeTextChanged(CharSequence s, int start, int count, int after){}
public void onTextChanged(CharSequence s, int start, int before, int count){}
});

Fahim
- 12,198
- 5
- 39
- 57
0
Changing text from afterTextChanged()
will cause an infinite loop as it will trigger itself.
So, you have two options:
-the first is to disable the text change listener, change the text and then re enable it, as per the answer here
-the second is to check if the text needs changing before actually changing it, so it will only be called the first time it is changed.

Community
- 1
- 1

Evripidis Drakos
- 872
- 1
- 7
- 15