I am looking to make an app which converts one unit to another(say currency). So it consists of 2 edit-texts. One in which the user enters the value and the second which contains the result. Now, here, instead of having a 'convert' button to put the value in the second edit text, i would like the converted value to appear in the second edit text AS I ENTER VALUES INTO THE FIRST ONE. How can i achieve this? Thanks
Asked
Active
Viewed 4,710 times
1 Answers
3
Use a TextWatcher
for this. Set it on the EditText
that the user types in:
myEditText1.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
String value = s.toString();
// Perform computations using this string
// For example: parse the value to an Integer and use this value
// Set the computed value to the other EditText
myEditText2.setText(computedValue);
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(final CharSequence s, int start, int before, int count) {
}
});
Edit 1:
Check for empty string ""
:
myEditText1.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
String value = s.toString();
if (value.equals("")) {
myEditText1.setText("0");
// You may not need this line, because "myEditText1.setText("0")" will
// trigger this method again and go to else block, where, if your code is set up
// correctly, myEditText2 will get the value 0. So, try without the next line
// and if it doesn't work, put it back.
myEditText2.setText("0");
} else {
// Perform computations using this string
// For example: parse the value to an Integer and use this value
// Set the computed value to the other EditText
myEditText2.setText(computedValue);
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(final CharSequence s, int start, int before, int count){
}
});

Vikram
- 51,313
- 11
- 93
- 122
-
yes this works. but for example i enter the value in dollar to be converted to rupee as 23. it gives the correct output. then if i press the back button to change it to first remove '3'. then finally if i try to remove '2' as well, then it crashes. I would like it to change to 0 dollar and 0 rupee if i empty the first edittext. How do i do that? – Shivam Bhalla Jul 25 '13 at 12:18
-
Yes, this happens because `onTextChanged(CharSequence, int, int, int)` is triggered whenever text inside `myEditText1` changes. You have to check for empty string `""`. See **Edit 1** above. – Vikram Jul 25 '13 at 16:35
-
@ShivamBhalla You are probably getting a `NumberFormatException`. **Edit 1** above should solve your problem. – Vikram Jul 25 '13 at 16:46
-
i tried that but i am still facing the same issue with numberformatexception invalid double:"" – Shivam Bhalla Jul 25 '13 at 19:07
-
any thoughts on that? – Shivam Bhalla Jul 26 '13 at 17:14
-
@ShivamBhalla Can you edit your question and post your code? It'll be easier to figure out the problem that way. – Vikram Jul 26 '13 at 17:17