3

I have an edittext which is created programmatically (so not in xml) and I want to launch a numeric keyboard when user presses it. The problem is that I want this keyboard also to contain the decimal separator (.).

I tried many things but nothing seems to work

final EditText input = new EditText(this);

input.setKeyListener(new DigitsKeyListener()); // this gives numeric without decimal separator input.setInputType(InputType.TYPE_CLASS_NUMBER); // this gives numeric without decimal separator input.setInputType(InputType.TYPE_CLASS_PHONE);//this gives phone keyboard with all the digits and decimal separator in the second page. It is not exactly what I want

I know that there is a way to implement custom keyboard:

http://android-developers.blogspot.com/2009/04/creating-input-method.html

http://developer.android.com/resources/samples/SoftKeyboard/index.html

but it seems quite a pain and I am first looking for a quicker alternative.

All I want is one of the following: 1. Launch numeric keyboard that contains decimal separator 2. Launch standard keyboard, but set in the numeric "page" Is something of the above possible? Thank you in advance!

george
  • 1,386
  • 5
  • 22
  • 38
  • Are you interested to perform regular expression over the editText value ? – SALMAN May 23 '12 at 19:45
  • hmm.. I do not understand, how will this change the keyboard that the user sees? Or help him insert decimal values? – george May 23 '12 at 19:48
  • [ok here is the link let the user input anything but during validation if it fails to input decimal number you can show error in Alert Box][1] [1]: http://stackoverflow.com/questions/308122/simple-regular-expression-for-a-decimal-with-a-precision-of-2 – SALMAN May 23 '12 at 19:51

4 Answers4

8

I have showed the keyboard using this code:

        // Set an EditText view to get user input
    final EditText input = new EditText(this);
    alert.setView(input);
    input.setInputType((InputType.TYPE_CLASS_NUMBER + InputType.TYPE_NUMBER_FLAG_DECIMAL));
Wiken
  • 81
  • 1
  • 2
  • The problem with this solution is that user can write only decimal number such as 6.3052 but he cannot use the dot more than once. we need a soft keyboard with numbers and dot that can be used for IP for example. – Udi Reshef Aug 20 '19 at 12:18
3

There is no keyboard for what you are describing, at least not one that you can get as a normal input type. Even if you made a custom keyboard, that wouldn't be a good way of handling this because the user would have to switch keyboards every time they wanted to use your app.

Also, you can't explicitly open the text keyboard "on the number page" because not all Android keyboards even have a separate number page. This is highly customized on each device.

My first suggestion would be to use the normal text keyboard, and let the user decide which keyboard suits the best.

My second suggestion would be to build a number pad on the screen using buttons/views for the specific keys you need.

mtmurdock
  • 12,756
  • 21
  • 65
  • 108
  • that looks thorough thank you. I will research the options a bit and let you know – george May 23 '12 at 20:02
  • from all that I can find it looks that you are very right. So answer accepted and thank you very much! – george May 26 '12 at 11:01
0

Here it goes.

if (editText.getText().matches("^[0-9]+(\.[0-9]{1,2})?$")) {
            System.out.println("It is decimal number");
        }
SALMAN
  • 2,031
  • 1
  • 20
  • 18
  • you do not understand, the question is not how to assure that the input is decimal number, I want to launch a soft keyboard so that the user will have the ten digits and the decimal separator (mostly like the InputType.TYPE_CLASS_NUMBER does, but add the ".") – george May 23 '12 at 20:05
0
new DigitsKeyListener(false, true) 

will give a decimal input keyboard

siliconeagle
  • 7,379
  • 3
  • 29
  • 41