I'm implementing EditText
's and I'm facing with one issue... I've put that the inputType
is numberDecimal
but at the time I type on the EditText
I'd like to put the "." or "," autommatically...
I can put for example 100.00
or 90.00
it depends of the user... Can it be possible?
I tried to create a TextWatcher
and do some things on onTextChanged
like see if the charSequence-lenght()==2
add a "." or "," but doesn't work...
Can I detect when to put the comma or dot even if I have a decimal format like ##.##
or ###.##
? As I said it can be 100.00
or 80.00
.
Edit
@Override
public void afterTextChanged(Editable editable) {
//et1 and et2 are not empty
if(et1.getText().toString().length() == 5 && et2.getText().toString().length() == 5){
double pe1 , pe2;
pe1 = Double.parseDouble(et1.getText().toString());
pe2 = Double.parseDouble(et2.getText().toString());
double res = (pe1+pe2)/2;
String formattedString = String.format("%,.2f", res);
tvPr.setText(formattedString);
}
The problem is on the textInput not on afterTextChanged
I guess... because I have to put the "." manually and I want to avoid this.
EDIT2
Also a friend of mine suggested to me that I could ignore the . and , and since the marks are always 100, 80, 32, etc... on the editText just type those numbers but then tract those as a double and I'd like to put a ,00 when the user finish the editting of editText is that possible?
Are the comparation if(et1.getText().toString().length() == 5 && et2.getText().toString().length() == 5){
good at the time to check wheter is an edittext not empty? I have to update a TextView once both edittext are not empty and do some operations so, I tried this way, is there another one better? (I put lenght()==5 because on EditText I put maxlenght() = 5;
All you have to do is something like this
Modify the above code to do what you need, but this is the basic idea behind making sure the value is of decimal form. It is worth mentioning that when you modify text like this the cursor position is always a "fun" thing to try to get how you want it. What happens if you have "100.00" and someone puts the cursor in a place like "100.0|0" and presses 5. Should the result be "100.0|5" or "100.05|" and if it is the latter how do you handle them putting in the next value, do you use it to round the previous value? It is an interesting situation, but this will get you started.