We have Edittext which can take Decimal value. So I have used
android:inputType="numberDecimal"
android:numeric="decimal"
Now EditText takes decimal values. My Query is can I control the user to enter only upto two decimal places?
For example 1345.99
, he cannot put more than two digit after decimal point.
there can be two solution,
1) let user not allow after he entered something like this xxx.xx.
2) let him enter it and when saving the value or taking an action on the screen, Check the value of edittext and generate a toast if the value entered is not in the desired format.
for second solution here you go. /
String str = editText.getText().toString();
int indexOFdec = str.indexOf(".");
if(indexOFdec >=0) {
if(str.substring(indexOFdec).length() >2)
{
Toast.makeText(getApplicationContext(), "Print a message here", Toast.LENGTH_SHORT).show();
return;
}
}
let me know if it works.
Although I'm not sure if you can do this in XML, a good workaround would be to just make a substring of the EditText after calling getText.toString() and extract the necessary tokens. Just look for the decimal character "." in the String and extract the 2 indexes after it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With