This is a two part question:
1) I'm currently using
android:inputType="numberDecimal|numberSigned"
to control the input in my app.
However, I keep getting crash reports from users with NumberFormatExceptions. One of them was:
Caused by: java.lang.NumberFormatException: Invalid float: "2,7"
So clearly users are somehow circumventing my measures to control the input and using "," instead of ".". I've temporarily used
String.replace(",", ".")
in an attempt to fix it but I still get NumberFormatExceptions .
What do I do?
2) How do I force the soft keyboard app to display only a number pad, "-" and "."?
XML:
<EditText
android:id="@+id/speed"
android:layout_width="0px"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="number|numberDecimal" >
In Activity:
public void onClickCalc(View view)
{
switch(view.getId())
{
String sp = speed.getText().toString().replace(",", ".");
if(functions.isNotValid(sp))
{
Toast.makeText(this, "Invalid input.", Toast.LENGTH_LONG).show();
return;
}
float speed = Float.parseFloat(sp);
... logic here
}
In the class functions:
public static boolean isNotValid(String editable)
{
try
{
float x = Float.parseFloat(editable);
}
catch(NumberFormatException nFE)
{
return true;
}
return false;
}