7

I need to let user choose between two variants when he inputs decimal number:

use comma(,) as separator

use dot(.) as separator

By default if I use inputType="numberDecimal" in the EditText xml config - EditText shows only digits and comma (,) as possible separator.

I've tried to use android:digits="0123456789, in my EditText config, but without result - EditText widget shows just digits and comma.

I want to have both variants (. and ,) available for user on on-screen keyboard when he tries to input decimal number.

Could you please advise?

Community
  • 1
  • 1
XZen
  • 225
  • 5
  • 27
  • 49
  • 1
    what excatly you want...? – Amardeepvijay Apr 18 '14 at 13:22
  • 1
    i think that "edit text shows only digits and comma as possible separator" means that that the onscreen keyboard, after tapping at edittext widget, shows these characters... – pelotasplus Apr 18 '14 at 13:24
  • Sorry if I was expressed not clearly, I've edited the question, please take a look – XZen Apr 18 '14 at 13:28
  • @XZen How about using ``? If I am not mistaken, that was working when I last tried it – Drew Apr 20 '14 at 16:55
  • Here's one option: have two edit texts on the same row separated by a text view with either "." or "," depending on user locale. Have inputType of the edit texts set to numbers only. – Luis Apr 23 '14 at 01:48

4 Answers4

14

Specifying both android:inputType="numberDecimal" and android:digits="0123456789,." will display a keyboard with mostly just numbers and punctuation (depending on the user's chosen keyboard). It will also limit the characters accepted as input to those in your digits attribute.

If you'd like to further limit the keyboard to display only certain characters, you'll need to define a custom one. Here's one of the better tutorials on doing that.

scottt
  • 8,301
  • 1
  • 31
  • 41
  • 2
    I've tried both inputType and android:digits, but the result is not what I want actually. It seems there is no choice and I need to create custom keyboard, thanks for the link – XZen Apr 21 '14 at 06:33
  • This does not work! It shows the options for both but only accepts the dot, not the comma. – FranzHuber23 Sep 02 '18 at 10:22
1

Use proper validation. Let the user see full keyboard but he remain aloof of using it. Means user should not be able to use or input anything using keyboard.

 etlocation = (EditText) findViewById(R.id.etlocation);

and used this

 etlocation.getText().toString();

 if (!isValidLocation(etlocation.getText().toString().trim()))
 {
 etlocation.setError("Invalid location");
 }

validate this

 public static boolean isValidLocation(String str) {
    boolean isValid = false;
    String expression = "^[0-9,.]*$";
    CharSequence inputStr = str;
    Pattern pattern = Pattern.compile(expression);
    Matcher matcher = pattern.matcher(inputStr);
    if (matcher.matches()) {
        isValid = true;
    }
    return isValid;
}
Amardeepvijay
  • 1,626
  • 3
  • 17
  • 47
  • Thank you, it's an option, but I really want to avoid using full keyboard and non-obvious restrictions for the user. Maybe I need to create my own keyboard... – XZen Apr 18 '14 at 13:40
  • 2
    isValidLocation fails with ",.,.,.,.,.," it says that it's a valid number – xgc1986 Apr 27 '14 at 08:15
  • Usability is pretty limited with such a solution. Why showing the alphanumeric keyboard if only numeric input is allowed? – thomasgalliker Dec 01 '20 at 11:41
0

You can read this link and also read allow only number and period(.) in edit text in android. Hopefully are helpful from those links. Best of Luck!

Community
  • 1
  • 1
0

You can create a custom keyboard. The below link shows a nice example of custom keyboard

http://www.mediafire.com/download/39q7of884goa818/myKeyborad2.zip

and check this link also

How to develop a soft keyboard for Android?

Community
  • 1
  • 1
Ameer
  • 2,709
  • 1
  • 28
  • 44