Can someone please help me? How I can force user to enter the numberDecimal
in EditText
field in XX.XX format(Eg.56.75).
I used max length attribute to restrict max length to 5.
Can someone please help me? How I can force user to enter the numberDecimal
in EditText
field in XX.XX format(Eg.56.75).
I used max length attribute to restrict max length to 5.
You can go the easiest way
<EditText
//.....
android:inputType="numberDecimal" /> // set inputformat
You can use InputFilter
if you need to make sure there are 2 digits before .
and after.
Try this under onCreate
youreditText.setFilters(new InputFilter[] {new DecimalDigitsInputFilter()});
this is just a class for this
EDIT: Had to rewrite the pattern as it seems that matcher matches the input starting with the first character and checks each one so you need to add a lot of optional parts in the patterns so that it passes each check that is being made. Now it doesn't allow 222 as it did in your case
public class DecimalDigitsInputFilter implements InputFilter {
@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
if (end > start) {
String destTxt = dest.toString();
String resultingTxt = destTxt.substring(0, dstart) + source.subSequence(start, end) + destTxt.substring(dend);
if (!resultingTxt.matches("^\\d(\\d(\\.\\d{0,2})?)?")) {
return "";
}
}
return null;
}
}
you can try
TextWatcher t1;
EditText e;
t1=new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if(s.length()==3 && !s.toString().contains("."))
{
e.removeTextChangedListener(t1);
e.setText(s.subSequence(0,1)+"."+s.charAt(2));
e.addTextChangedListener(t1);
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
};
e.addTextChangedListener(t1);
try this:
edittext.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {}
@Override
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start,
int before, int count) {
DecimalFormat formatter = new DecimalFormat("##.##");
String yourFormattedString = formatter.format(s.toString());
edittext.setText(yourFormattedString);
}
});
Try this
exitText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
if(charSequence.length() == 2){
exitText.setText("");
exitText.setText(charSequence+".");
}
}
@Override
public void afterTextChanged(Editable editable) {
}
});
If you want to have decimal number format, you can use this in XML:
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="numberDecimal" />
To limit the number of digits, you might need to use InputFilter
mentioned in other answers or here: Limit Decimal Places in Android EditText
This following code is also working :
Inside OnCreateView :
myEditText.setFilters(new InputFilter[] {new DecimalDigitsInputFilter(2,2)});
Java Class :
public class DecimalDigitsInputFilter implements InputFilter {
final int maxDigitsBeforeDecimalPoint ;
final int maxDigitsAfterDecimalPoint ;
public DecimalDigitsInputFilter(int digitsBeforeDecimal,int digitsAfterDecimal ){
maxDigitsBeforeDecimalPoint = digitsBeforeDecimal;
maxDigitsAfterDecimalPoint = digitsAfterDecimal;
}
@Override
public CharSequence filter(CharSequence source, int start, int end,
Spanned dest, int dstart, int dend) {
StringBuilder builder = new StringBuilder(dest);
builder.replace(dstart, dend, source
.subSequence(start, end).toString());
if (!builder.toString().matches(
"(([1-9]{1})([0-9]{0," + (maxDigitsBeforeDecimalPoint - 1) + "})?)?(\\.[0-9]{0," + maxDigitsAfterDecimalPoint + "})?"
)) {
if (source.length() == 0)
return dest.subSequence(dstart, dend);
return "";
}
return null;
}
}