Reputation: 101
I am trying to show the android keyboard which shows only numbers and a decimal point it works fine in devices like Lenovo but it doesn't works in devices like Samsung. But when i installed an app the keyboard works same in both the above mentioned devices. I would like to know whether it is a custom keyboard or not.Even i have tried adding the code for the keyboard:
amt.setRawInputType(Configuration.KEYBOARD_12KEY);
but it didn't works.I would like to know how to get the same keyboard as mentioned in the below image which works in all devices.
Upvotes: 1
Views: 2785
Reputation: 85
I recently had to work through this problem at my job. The Samsung Keyboard app has a single button for negative and decimal in some versions. I don't have a screenshot at the moment, I can try to grab one tomorrow. In order to get this button to show up when you select your EditText, on the XML side add:
android:inputType="numberDecimal|numberSigned"
android:digits="0123456789.-"
Then in your Activity / Fragment with the EditText, find the EditText and apply regex to ensure it's a number.
EditText editText = (EditText) findViewById(R.id.your_edit_text);
final String regex = "^\\-?(\\d*|\\d*\\.\\d+)$";
editText.setFilters(new InputFilter[]{ new InputFilter() {
@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
//if we are adding text
if (end > start) {
String destinationString = dest.toString();
String resultingTxt = destinationString.substring(0, dstart) + source.subSequence(start, end) + destinationString.substring(dend);
// return null to accept the input or empty to reject it
return resultingTxt.matches(regex) ? null : "";
}
// removing: always accept
return null;
}
}});
IMPORTANT: Also make sure you don't have a NumberKeyListener attached to the EditText. If you have a NumberKeyListener attached to the EditText the Samsung keyboard will hide the "-/." button you're trying to show.
Edit: If you don't want negative numbers you can also change the regex.
final String regex = "^(\\d*|\\d*\\.\\d+)$";
Upvotes: 2
Reputation: 357
Try this line amt.setRawInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
Upvotes: 2
Reputation: 2258
add this attribute to you'r EditText in XML
android:inputType="numberDecimal"
try this
java
editText.setInputType(InputType.TYPE_CLASS_NUMBER);
Upvotes: 1
Reputation:
Try add comma ",". It may cause from different format of digits.
<EditText
android:inputType="numberDecimal|number"
android:digits="0123456789.," />
Upvotes: 2
Reputation: 4345
Try this
By Xml
<EditText
android:inputType="numberDecimal|number"
android:digits="0123456789."
/>
By Java
input.setInputType(InputType.TYPE_CLASS_NUMBER); //OR
input.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL);
This may helps you.
Upvotes: 2