Configure Android Edittext to Allow Decimals and Negatives

Configure android EditText to allow decimals and negatives

You are just missing this in your EditText,

android:inputType="numberDecimal|numberSigned"

How do I restrict my EditText input to numerical (possibly decimal and signed) input?

Try using TextView.setRawInputType() it corresponds to the android:inputType attribute.

Android InputType layout parameter - how to allow negative decimals?

Ended up using Pattern and Matcher for each of the axes, leaving the actual text input format open.

Three axes fields, submit button triggers onSave. Fields are validated and either the insert occurs, or error message is raised to submitter about required format for the Axes fields. Proper format is 1-3 digits, optionally prefaced by '-', optionally followed by '.' and additional digits.

Try this code :

private View.OnClickListener onSave=new View.OnClickListener(){
public void onClick(View v){
Pattern xAxis = Pattern.compile("[-+]?([0-9]*\\.)?[0-9]+");
Matcher mForX = xAxis.matcher(xaxis.getText().toString());

Pattern yAxis = Pattern.compile("[-+]?([0-9]*\\.)?[0-9]+");
Matcher mForY = yAxis.matcher(yaxis.getText().toString());

Pattern zAxis = Pattern.compile("[-+]?([0-9]*\\.)?[0-9]+");
Matcher mForZ = zAxis.matcher(zaxis.getText().toString());

//If Axis X and Axis Y and Axis Z are all valid entries, the proceed.
if(mForX.find() && mForY.find() && mForZ.find()){
//handle insert or update statement here
}else{
//give error message regarding correct Axis value format
}
}

}

How to enter a Negative Decimal Number in EditText?

You can use both with the "|" operator. Just specify "numberSigned|numberDecimal".

Allow entering negative sign first for Android EditText of number type

Easy solution would be to use the digits keyword.

<EditText 
android:id="@+id/editText"
android:digits="0,1,2,3,4,5,6,7,8,9,-,."
/>

Harder solution would be to implement an InputFilter or a TextWatcher, and make them "override" the inputType behaviour when character is "-" to assess no further work is needed.

Forcing edittext to only accept values of: numbers, decimals, plus, and minus signs

Doing

android:inputType="numberDecimal|numberSigned"

should work.

Android edittext two decimal places

You should use InputFilter here is an example

public class DecimalDigitsInputFilter implements InputFilter {

Pattern mPattern;

public DecimalDigitsInputFilter(int digitsBeforeZero,int digitsAfterZero) {
mPattern=Pattern.compile("[0-9]{0," + (digitsBeforeZero-1) + "}+((\\.[0-9]{0," + (digitsAfterZero-1) + "})?)||(\\.)?");
}

@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {

Matcher matcher=mPattern.matcher(dest);
if(!matcher.matches())
return "";
return null;
}

}

you can use it like this

editText.setFilters(new InputFilter[] {new DecimalDigitsInputFilter(5,2)});

How to programmatically set EditText's InputType to integer or decimal?

You can use this code:

EditText edt = new EditText(context);
edt.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL); //for decimal numbers
edt.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_SIGNED); //for positive or negative values

If together:

edt.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL | InputType.TYPE_NUMBER_FLAG_SIGNED);

Limit Decimal Places in Android EditText

This implementation of InputFilter solves the problem.

import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.method.DigitsKeyListener;

public class MoneyValueFilter extends DigitsKeyListener {
public MoneyValueFilter() {
super(false, true);
}

private int digits = 2;

public void setDigits(int d) {
digits = d;
}

@Override
public CharSequence filter(CharSequence source, int start, int end,
Spanned dest, int dstart, int dend) {
CharSequence out = super.filter(source, start, end, dest, dstart, dend);

// if changed, replace the source
if (out != null) {
source = out;
start = 0;
end = out.length();
}

int len = end - start;

// if deleting, source is empty
// and deleting can't break anything
if (len == 0) {
return source;
}

int dlen = dest.length();

// Find the position of the decimal .
for (int i = 0; i < dstart; i++) {
if (dest.charAt(i) == '.') {
// being here means, that a number has
// been inserted after the dot
// check if the amount of digits is right
return (dlen-(i+1) + len > digits) ?
"" :
new SpannableStringBuilder(source, start, end);
}
}

for (int i = start; i < end; ++i) {
if (source.charAt(i) == '.') {
// being here means, dot has been inserted
// check if the amount of digits is right
if ((dlen-dend) + (end-(i + 1)) > digits)
return "";
else
break; // return new SpannableStringBuilder(source, start, end);
}
}

// if the dot is after the inserted part,
// nothing can break
return new SpannableStringBuilder(source, start, end);
}
}


Related Topics



Leave a reply



Submit