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












47















I have read Android: Limiting EditText to numbers and How do I show the number keyboard on an EditText in android?. Unfortunately, none of them seems to fit my needs.



I want to restrict my EditText input to only numbers. However, I also want to allow signed and/or decimal input.



Here is my current code (I need to do this programmatically):



EditText edit = new EditText(this);

edit.setHorizontallyScrolling(true);
edit.setInputType(InputType.TYPE_CLASS_NUMBER);


With this, my EditText merrily restricts all input to numerical digits. Unfortunately, it doesn't allow anything else, like the decimal point.



If I change that line to edit.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL), the EditText accepts all input (which isn't what I want...).



I've tried combining flags (in desperation to see if it would work):



edit.setInputType(InputType.TYPE_CLASS_NUMBER);
edit.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL)
edit.setInputType(InputType.TYPE_NUMBER_FLAG_SIGNED)


That didn't work either (the EditText accepted all input as usual).



So, how do I do this?










share|improve this question

























  • Programmatically? So you would be against an XML layout solution?

    – Phil
    Aug 2 '11 at 22:51






  • 2





    I wouldn't be against one, as long as I can create an EditText on the fly.

    – kibibyte
    Aug 3 '11 at 1:54
















47















I have read Android: Limiting EditText to numbers and How do I show the number keyboard on an EditText in android?. Unfortunately, none of them seems to fit my needs.



I want to restrict my EditText input to only numbers. However, I also want to allow signed and/or decimal input.



Here is my current code (I need to do this programmatically):



EditText edit = new EditText(this);

edit.setHorizontallyScrolling(true);
edit.setInputType(InputType.TYPE_CLASS_NUMBER);


With this, my EditText merrily restricts all input to numerical digits. Unfortunately, it doesn't allow anything else, like the decimal point.



If I change that line to edit.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL), the EditText accepts all input (which isn't what I want...).



I've tried combining flags (in desperation to see if it would work):



edit.setInputType(InputType.TYPE_CLASS_NUMBER);
edit.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL)
edit.setInputType(InputType.TYPE_NUMBER_FLAG_SIGNED)


That didn't work either (the EditText accepted all input as usual).



So, how do I do this?










share|improve this question

























  • Programmatically? So you would be against an XML layout solution?

    – Phil
    Aug 2 '11 at 22:51






  • 2





    I wouldn't be against one, as long as I can create an EditText on the fly.

    – kibibyte
    Aug 3 '11 at 1:54














47












47








47


8






I have read Android: Limiting EditText to numbers and How do I show the number keyboard on an EditText in android?. Unfortunately, none of them seems to fit my needs.



I want to restrict my EditText input to only numbers. However, I also want to allow signed and/or decimal input.



Here is my current code (I need to do this programmatically):



EditText edit = new EditText(this);

edit.setHorizontallyScrolling(true);
edit.setInputType(InputType.TYPE_CLASS_NUMBER);


With this, my EditText merrily restricts all input to numerical digits. Unfortunately, it doesn't allow anything else, like the decimal point.



If I change that line to edit.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL), the EditText accepts all input (which isn't what I want...).



I've tried combining flags (in desperation to see if it would work):



edit.setInputType(InputType.TYPE_CLASS_NUMBER);
edit.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL)
edit.setInputType(InputType.TYPE_NUMBER_FLAG_SIGNED)


That didn't work either (the EditText accepted all input as usual).



So, how do I do this?










share|improve this question
















I have read Android: Limiting EditText to numbers and How do I show the number keyboard on an EditText in android?. Unfortunately, none of them seems to fit my needs.



I want to restrict my EditText input to only numbers. However, I also want to allow signed and/or decimal input.



Here is my current code (I need to do this programmatically):



EditText edit = new EditText(this);

edit.setHorizontallyScrolling(true);
edit.setInputType(InputType.TYPE_CLASS_NUMBER);


With this, my EditText merrily restricts all input to numerical digits. Unfortunately, it doesn't allow anything else, like the decimal point.



If I change that line to edit.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL), the EditText accepts all input (which isn't what I want...).



I've tried combining flags (in desperation to see if it would work):



edit.setInputType(InputType.TYPE_CLASS_NUMBER);
edit.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL)
edit.setInputType(InputType.TYPE_NUMBER_FLAG_SIGNED)


That didn't work either (the EditText accepted all input as usual).



So, how do I do this?







android android-edittext






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited May 23 '17 at 11:47









Community

11




11










asked Aug 2 '11 at 22:15









kibibytekibibyte

1,65752339




1,65752339













  • Programmatically? So you would be against an XML layout solution?

    – Phil
    Aug 2 '11 at 22:51






  • 2





    I wouldn't be against one, as long as I can create an EditText on the fly.

    – kibibyte
    Aug 3 '11 at 1:54



















  • Programmatically? So you would be against an XML layout solution?

    – Phil
    Aug 2 '11 at 22:51






  • 2





    I wouldn't be against one, as long as I can create an EditText on the fly.

    – kibibyte
    Aug 3 '11 at 1:54

















Programmatically? So you would be against an XML layout solution?

– Phil
Aug 2 '11 at 22:51





Programmatically? So you would be against an XML layout solution?

– Phil
Aug 2 '11 at 22:51




2




2





I wouldn't be against one, as long as I can create an EditText on the fly.

– kibibyte
Aug 3 '11 at 1:54





I wouldn't be against one, as long as I can create an EditText on the fly.

– kibibyte
Aug 3 '11 at 1:54












9 Answers
9






active

oldest

votes


















56














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






share|improve this answer
























  • How exactly would I invoke this? Do I just invoke the calls in a chain?

    – kibibyte
    Aug 3 '11 at 1:50






  • 44





    using your example code: edit.setRawInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);

    – Dan S
    Aug 3 '11 at 4:02













  • Hey its ok, I just picked the setter associated with the android input type. I'm happy you found it and I was close. Yep, its just a bit mask with flags, not all of them but many are.

    – Dan S
    Aug 3 '11 at 4:19






  • 2





    What was the solution? using any of these i still get keys i don't want like "*","#", and parentesis, etc...i only want numbers and -/+/. chars, how to do this?

    – Maxrunner
    Dec 9 '13 at 19:23











  • Check the InputType Reference for more details.

    – Dan S
    Dec 9 '13 at 23:40



















153














There's no reason to use setRawInputType(), just use setInputType(). However, you have to combine the class and flags with the OR operator:



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





share|improve this answer
























  • Will give an uptick if you show the xml code as well for setting those flags.

    – Matt Stevens
    Jan 2 '14 at 1:44











  • This is not sufficient. You can still type multiple decimals. That is not a valid number.

    – Andrew
    Oct 9 '14 at 19:40






  • 4





    Sometimes, Android makes me pull out my hair. Why is something so simple so incredibly hard and bug-prone?

    – DiscDev
    Sep 17 '15 at 19:36











  • damn tried every thing... now i wonder in a single or static dynamic edittext it works with a simple one statement inputtype, with multiple dynamic et (et in an array) it does not i had to come here for the rescue.. thanks man

    – Nasz Njoka Sr.
    Mar 2 '16 at 11:19






  • 1





    note that the signed flag is not needed if you don't want to allow negative values

    – Bart Burg
    Jun 26 '18 at 14:04



















12














The best way to do that programmatically is using the next method:



public static DigitsKeyListener getInstance (boolean sign, boolean decimal) 


Returns a DigitsKeyListener that accepts the digits 0 through 9, plus the minus sign (only at the beginning) and/or decimal point (only one per field) if specified.



This solve the problem about the many '.' in EditText



editText.setKeyListener(DigitsKeyListener.getInstance(true,true)); // decimals and positive/negative numbers. 

editText.setKeyListener(DigitsKeyListener.getInstance(false,true)); // positive decimals numbers.

editText.setKeyListener(DigitsKeyListener.getInstance(false,false)); // positive integer numbers.

editText.setKeyListener(DigitsKeyListener.getInstance(true,false)); // positive/negative integer numbers.





share|improve this answer































    9














    put this line in xml



     android:inputType="number|numberDecimal"





    share|improve this answer
























    • simple and good one

      – Shylendra Madda
      Apr 13 '17 at 7:03



















    7














    Use this. Works fine



    input.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL | InputType.TYPE_NUMBER_FLAG_SIGNED);
    input.setKeyListener(DigitsKeyListener.getInstance("0123456789"));


    EDIT



    kotlin version



    fun EditText.onlyNumbers() {
    inputType = InputType.TYPE_CLASS_NUMBER or InputType.TYPE_NUMBER_FLAG_DECIMAL or
    InputType.TYPE_NUMBER_FLAG_SIGNED
    keyListener = DigitsKeyListener.getInstance("0123456789")
    }





    share|improve this answer





















    • 1





      It's That i want, Thank you

      – AndroSco
      Nov 6 '17 at 12:17











    • May I know how to allow them to type 11 numbers only? @V.Kalyuzhnyu

      – Leon Zaii
      Oct 31 '18 at 4:14











    • @LeonZaii allow only typing "1" and check that string length divisible by two

      – V. Kalyuzhnyu
      Oct 31 '18 at 6:17











    • noted. Thanks @V.Kalyuzhnyu

      – Leon Zaii
      Oct 31 '18 at 8:38



















    4














    Hi All I also had this problem. I use C# with Xamarin



    Just a slight note that I hope someone else as well.



    I have tried multiple of the methods mentioned here.



    edittext1.SetRawInputType(Android.Text.InputTypes.ClassNumber | Android.Text.InputTypes.NumberFlagDecimal);


    was the closest but still did not work. As Ralf mentioned, you could use




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




    However in my C# I did not have this. instead you do it as follow:



    edittext1.InputType = Android.Text.InputTypes.ClassNumber | Android.Text.InputTypes.NumberFlagDecimal;


    Please note that the ORDER of these is important. If you put Decimal first, it will still allow you to type any Characters, where If Numer is first it is only numeric, but allows a decimal seperator!






    share|improve this answer
























    • Non programatically I manage this with just android:inputType="numberDecimal"

      – Michael Gabay
      Sep 19 '17 at 16:08



















    2














    EditText edit = new EditText(this);

    edit.setHorizontallyScrolling(true);
    edit.setInputType(EditorInfo.TYPE_NUMBER_FLAG_SIGNED|EditorInfo.TYPE_CLASS_NUMBER);





    share|improve this answer

































      1














      TO set the input type of EditText as decimal use this code:



      EditText edit = new EditText(this);
      edit.SetRawInputType(Android.Text.InputTypes.NumberFlagDecimal | Android.Text.InputTypes.ClassNumber);





      share|improve this answer

































        0














        my solution:`



           public void onTextChanged(CharSequence s, int start, int before, int count) {
        char ch=s.charAt(start + count - 1);
        if (Character.isLetter(ch)) {
        s=s.subSequence(start, count-1);
        edittext.setText(s);
        }





        share|improve this answer























          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f6919360%2fhow-do-i-restrict-my-edittext-input-to-numerical-possibly-decimal-and-signed-i%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          9 Answers
          9






          active

          oldest

          votes








          9 Answers
          9






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          56














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






          share|improve this answer
























          • How exactly would I invoke this? Do I just invoke the calls in a chain?

            – kibibyte
            Aug 3 '11 at 1:50






          • 44





            using your example code: edit.setRawInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);

            – Dan S
            Aug 3 '11 at 4:02













          • Hey its ok, I just picked the setter associated with the android input type. I'm happy you found it and I was close. Yep, its just a bit mask with flags, not all of them but many are.

            – Dan S
            Aug 3 '11 at 4:19






          • 2





            What was the solution? using any of these i still get keys i don't want like "*","#", and parentesis, etc...i only want numbers and -/+/. chars, how to do this?

            – Maxrunner
            Dec 9 '13 at 19:23











          • Check the InputType Reference for more details.

            – Dan S
            Dec 9 '13 at 23:40
















          56














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






          share|improve this answer
























          • How exactly would I invoke this? Do I just invoke the calls in a chain?

            – kibibyte
            Aug 3 '11 at 1:50






          • 44





            using your example code: edit.setRawInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);

            – Dan S
            Aug 3 '11 at 4:02













          • Hey its ok, I just picked the setter associated with the android input type. I'm happy you found it and I was close. Yep, its just a bit mask with flags, not all of them but many are.

            – Dan S
            Aug 3 '11 at 4:19






          • 2





            What was the solution? using any of these i still get keys i don't want like "*","#", and parentesis, etc...i only want numbers and -/+/. chars, how to do this?

            – Maxrunner
            Dec 9 '13 at 19:23











          • Check the InputType Reference for more details.

            – Dan S
            Dec 9 '13 at 23:40














          56












          56








          56







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






          share|improve this answer













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







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Aug 2 '11 at 22:21









          Dan SDan S

          8,14723042




          8,14723042













          • How exactly would I invoke this? Do I just invoke the calls in a chain?

            – kibibyte
            Aug 3 '11 at 1:50






          • 44





            using your example code: edit.setRawInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);

            – Dan S
            Aug 3 '11 at 4:02













          • Hey its ok, I just picked the setter associated with the android input type. I'm happy you found it and I was close. Yep, its just a bit mask with flags, not all of them but many are.

            – Dan S
            Aug 3 '11 at 4:19






          • 2





            What was the solution? using any of these i still get keys i don't want like "*","#", and parentesis, etc...i only want numbers and -/+/. chars, how to do this?

            – Maxrunner
            Dec 9 '13 at 19:23











          • Check the InputType Reference for more details.

            – Dan S
            Dec 9 '13 at 23:40



















          • How exactly would I invoke this? Do I just invoke the calls in a chain?

            – kibibyte
            Aug 3 '11 at 1:50






          • 44





            using your example code: edit.setRawInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);

            – Dan S
            Aug 3 '11 at 4:02













          • Hey its ok, I just picked the setter associated with the android input type. I'm happy you found it and I was close. Yep, its just a bit mask with flags, not all of them but many are.

            – Dan S
            Aug 3 '11 at 4:19






          • 2





            What was the solution? using any of these i still get keys i don't want like "*","#", and parentesis, etc...i only want numbers and -/+/. chars, how to do this?

            – Maxrunner
            Dec 9 '13 at 19:23











          • Check the InputType Reference for more details.

            – Dan S
            Dec 9 '13 at 23:40

















          How exactly would I invoke this? Do I just invoke the calls in a chain?

          – kibibyte
          Aug 3 '11 at 1:50





          How exactly would I invoke this? Do I just invoke the calls in a chain?

          – kibibyte
          Aug 3 '11 at 1:50




          44




          44





          using your example code: edit.setRawInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);

          – Dan S
          Aug 3 '11 at 4:02







          using your example code: edit.setRawInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);

          – Dan S
          Aug 3 '11 at 4:02















          Hey its ok, I just picked the setter associated with the android input type. I'm happy you found it and I was close. Yep, its just a bit mask with flags, not all of them but many are.

          – Dan S
          Aug 3 '11 at 4:19





          Hey its ok, I just picked the setter associated with the android input type. I'm happy you found it and I was close. Yep, its just a bit mask with flags, not all of them but many are.

          – Dan S
          Aug 3 '11 at 4:19




          2




          2





          What was the solution? using any of these i still get keys i don't want like "*","#", and parentesis, etc...i only want numbers and -/+/. chars, how to do this?

          – Maxrunner
          Dec 9 '13 at 19:23





          What was the solution? using any of these i still get keys i don't want like "*","#", and parentesis, etc...i only want numbers and -/+/. chars, how to do this?

          – Maxrunner
          Dec 9 '13 at 19:23













          Check the InputType Reference for more details.

          – Dan S
          Dec 9 '13 at 23:40





          Check the InputType Reference for more details.

          – Dan S
          Dec 9 '13 at 23:40













          153














          There's no reason to use setRawInputType(), just use setInputType(). However, you have to combine the class and flags with the OR operator:



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





          share|improve this answer
























          • Will give an uptick if you show the xml code as well for setting those flags.

            – Matt Stevens
            Jan 2 '14 at 1:44











          • This is not sufficient. You can still type multiple decimals. That is not a valid number.

            – Andrew
            Oct 9 '14 at 19:40






          • 4





            Sometimes, Android makes me pull out my hair. Why is something so simple so incredibly hard and bug-prone?

            – DiscDev
            Sep 17 '15 at 19:36











          • damn tried every thing... now i wonder in a single or static dynamic edittext it works with a simple one statement inputtype, with multiple dynamic et (et in an array) it does not i had to come here for the rescue.. thanks man

            – Nasz Njoka Sr.
            Mar 2 '16 at 11:19






          • 1





            note that the signed flag is not needed if you don't want to allow negative values

            – Bart Burg
            Jun 26 '18 at 14:04
















          153














          There's no reason to use setRawInputType(), just use setInputType(). However, you have to combine the class and flags with the OR operator:



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





          share|improve this answer
























          • Will give an uptick if you show the xml code as well for setting those flags.

            – Matt Stevens
            Jan 2 '14 at 1:44











          • This is not sufficient. You can still type multiple decimals. That is not a valid number.

            – Andrew
            Oct 9 '14 at 19:40






          • 4





            Sometimes, Android makes me pull out my hair. Why is something so simple so incredibly hard and bug-prone?

            – DiscDev
            Sep 17 '15 at 19:36











          • damn tried every thing... now i wonder in a single or static dynamic edittext it works with a simple one statement inputtype, with multiple dynamic et (et in an array) it does not i had to come here for the rescue.. thanks man

            – Nasz Njoka Sr.
            Mar 2 '16 at 11:19






          • 1





            note that the signed flag is not needed if you don't want to allow negative values

            – Bart Burg
            Jun 26 '18 at 14:04














          153












          153








          153







          There's no reason to use setRawInputType(), just use setInputType(). However, you have to combine the class and flags with the OR operator:



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





          share|improve this answer













          There's no reason to use setRawInputType(), just use setInputType(). However, you have to combine the class and flags with the OR operator:



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






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 28 '12 at 14:30









          RalfRalf

          10.3k94056




          10.3k94056













          • Will give an uptick if you show the xml code as well for setting those flags.

            – Matt Stevens
            Jan 2 '14 at 1:44











          • This is not sufficient. You can still type multiple decimals. That is not a valid number.

            – Andrew
            Oct 9 '14 at 19:40






          • 4





            Sometimes, Android makes me pull out my hair. Why is something so simple so incredibly hard and bug-prone?

            – DiscDev
            Sep 17 '15 at 19:36











          • damn tried every thing... now i wonder in a single or static dynamic edittext it works with a simple one statement inputtype, with multiple dynamic et (et in an array) it does not i had to come here for the rescue.. thanks man

            – Nasz Njoka Sr.
            Mar 2 '16 at 11:19






          • 1





            note that the signed flag is not needed if you don't want to allow negative values

            – Bart Burg
            Jun 26 '18 at 14:04



















          • Will give an uptick if you show the xml code as well for setting those flags.

            – Matt Stevens
            Jan 2 '14 at 1:44











          • This is not sufficient. You can still type multiple decimals. That is not a valid number.

            – Andrew
            Oct 9 '14 at 19:40






          • 4





            Sometimes, Android makes me pull out my hair. Why is something so simple so incredibly hard and bug-prone?

            – DiscDev
            Sep 17 '15 at 19:36











          • damn tried every thing... now i wonder in a single or static dynamic edittext it works with a simple one statement inputtype, with multiple dynamic et (et in an array) it does not i had to come here for the rescue.. thanks man

            – Nasz Njoka Sr.
            Mar 2 '16 at 11:19






          • 1





            note that the signed flag is not needed if you don't want to allow negative values

            – Bart Burg
            Jun 26 '18 at 14:04

















          Will give an uptick if you show the xml code as well for setting those flags.

          – Matt Stevens
          Jan 2 '14 at 1:44





          Will give an uptick if you show the xml code as well for setting those flags.

          – Matt Stevens
          Jan 2 '14 at 1:44













          This is not sufficient. You can still type multiple decimals. That is not a valid number.

          – Andrew
          Oct 9 '14 at 19:40





          This is not sufficient. You can still type multiple decimals. That is not a valid number.

          – Andrew
          Oct 9 '14 at 19:40




          4




          4





          Sometimes, Android makes me pull out my hair. Why is something so simple so incredibly hard and bug-prone?

          – DiscDev
          Sep 17 '15 at 19:36





          Sometimes, Android makes me pull out my hair. Why is something so simple so incredibly hard and bug-prone?

          – DiscDev
          Sep 17 '15 at 19:36













          damn tried every thing... now i wonder in a single or static dynamic edittext it works with a simple one statement inputtype, with multiple dynamic et (et in an array) it does not i had to come here for the rescue.. thanks man

          – Nasz Njoka Sr.
          Mar 2 '16 at 11:19





          damn tried every thing... now i wonder in a single or static dynamic edittext it works with a simple one statement inputtype, with multiple dynamic et (et in an array) it does not i had to come here for the rescue.. thanks man

          – Nasz Njoka Sr.
          Mar 2 '16 at 11:19




          1




          1





          note that the signed flag is not needed if you don't want to allow negative values

          – Bart Burg
          Jun 26 '18 at 14:04





          note that the signed flag is not needed if you don't want to allow negative values

          – Bart Burg
          Jun 26 '18 at 14:04











          12














          The best way to do that programmatically is using the next method:



          public static DigitsKeyListener getInstance (boolean sign, boolean decimal) 


          Returns a DigitsKeyListener that accepts the digits 0 through 9, plus the minus sign (only at the beginning) and/or decimal point (only one per field) if specified.



          This solve the problem about the many '.' in EditText



          editText.setKeyListener(DigitsKeyListener.getInstance(true,true)); // decimals and positive/negative numbers. 

          editText.setKeyListener(DigitsKeyListener.getInstance(false,true)); // positive decimals numbers.

          editText.setKeyListener(DigitsKeyListener.getInstance(false,false)); // positive integer numbers.

          editText.setKeyListener(DigitsKeyListener.getInstance(true,false)); // positive/negative integer numbers.





          share|improve this answer




























            12














            The best way to do that programmatically is using the next method:



            public static DigitsKeyListener getInstance (boolean sign, boolean decimal) 


            Returns a DigitsKeyListener that accepts the digits 0 through 9, plus the minus sign (only at the beginning) and/or decimal point (only one per field) if specified.



            This solve the problem about the many '.' in EditText



            editText.setKeyListener(DigitsKeyListener.getInstance(true,true)); // decimals and positive/negative numbers. 

            editText.setKeyListener(DigitsKeyListener.getInstance(false,true)); // positive decimals numbers.

            editText.setKeyListener(DigitsKeyListener.getInstance(false,false)); // positive integer numbers.

            editText.setKeyListener(DigitsKeyListener.getInstance(true,false)); // positive/negative integer numbers.





            share|improve this answer


























              12












              12








              12







              The best way to do that programmatically is using the next method:



              public static DigitsKeyListener getInstance (boolean sign, boolean decimal) 


              Returns a DigitsKeyListener that accepts the digits 0 through 9, plus the minus sign (only at the beginning) and/or decimal point (only one per field) if specified.



              This solve the problem about the many '.' in EditText



              editText.setKeyListener(DigitsKeyListener.getInstance(true,true)); // decimals and positive/negative numbers. 

              editText.setKeyListener(DigitsKeyListener.getInstance(false,true)); // positive decimals numbers.

              editText.setKeyListener(DigitsKeyListener.getInstance(false,false)); // positive integer numbers.

              editText.setKeyListener(DigitsKeyListener.getInstance(true,false)); // positive/negative integer numbers.





              share|improve this answer













              The best way to do that programmatically is using the next method:



              public static DigitsKeyListener getInstance (boolean sign, boolean decimal) 


              Returns a DigitsKeyListener that accepts the digits 0 through 9, plus the minus sign (only at the beginning) and/or decimal point (only one per field) if specified.



              This solve the problem about the many '.' in EditText



              editText.setKeyListener(DigitsKeyListener.getInstance(true,true)); // decimals and positive/negative numbers. 

              editText.setKeyListener(DigitsKeyListener.getInstance(false,true)); // positive decimals numbers.

              editText.setKeyListener(DigitsKeyListener.getInstance(false,false)); // positive integer numbers.

              editText.setKeyListener(DigitsKeyListener.getInstance(true,false)); // positive/negative integer numbers.






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Sep 4 '15 at 12:13









              SerSánGalSerSánGal

              429415




              429415























                  9














                  put this line in xml



                   android:inputType="number|numberDecimal"





                  share|improve this answer
























                  • simple and good one

                    – Shylendra Madda
                    Apr 13 '17 at 7:03
















                  9














                  put this line in xml



                   android:inputType="number|numberDecimal"





                  share|improve this answer
























                  • simple and good one

                    – Shylendra Madda
                    Apr 13 '17 at 7:03














                  9












                  9








                  9







                  put this line in xml



                   android:inputType="number|numberDecimal"





                  share|improve this answer













                  put this line in xml



                   android:inputType="number|numberDecimal"






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jan 19 '15 at 11:48









                  Qutbuddin BohraQutbuddin Bohra

                  5811620




                  5811620













                  • simple and good one

                    – Shylendra Madda
                    Apr 13 '17 at 7:03



















                  • simple and good one

                    – Shylendra Madda
                    Apr 13 '17 at 7:03

















                  simple and good one

                  – Shylendra Madda
                  Apr 13 '17 at 7:03





                  simple and good one

                  – Shylendra Madda
                  Apr 13 '17 at 7:03











                  7














                  Use this. Works fine



                  input.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL | InputType.TYPE_NUMBER_FLAG_SIGNED);
                  input.setKeyListener(DigitsKeyListener.getInstance("0123456789"));


                  EDIT



                  kotlin version



                  fun EditText.onlyNumbers() {
                  inputType = InputType.TYPE_CLASS_NUMBER or InputType.TYPE_NUMBER_FLAG_DECIMAL or
                  InputType.TYPE_NUMBER_FLAG_SIGNED
                  keyListener = DigitsKeyListener.getInstance("0123456789")
                  }





                  share|improve this answer





















                  • 1





                    It's That i want, Thank you

                    – AndroSco
                    Nov 6 '17 at 12:17











                  • May I know how to allow them to type 11 numbers only? @V.Kalyuzhnyu

                    – Leon Zaii
                    Oct 31 '18 at 4:14











                  • @LeonZaii allow only typing "1" and check that string length divisible by two

                    – V. Kalyuzhnyu
                    Oct 31 '18 at 6:17











                  • noted. Thanks @V.Kalyuzhnyu

                    – Leon Zaii
                    Oct 31 '18 at 8:38
















                  7














                  Use this. Works fine



                  input.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL | InputType.TYPE_NUMBER_FLAG_SIGNED);
                  input.setKeyListener(DigitsKeyListener.getInstance("0123456789"));


                  EDIT



                  kotlin version



                  fun EditText.onlyNumbers() {
                  inputType = InputType.TYPE_CLASS_NUMBER or InputType.TYPE_NUMBER_FLAG_DECIMAL or
                  InputType.TYPE_NUMBER_FLAG_SIGNED
                  keyListener = DigitsKeyListener.getInstance("0123456789")
                  }





                  share|improve this answer





















                  • 1





                    It's That i want, Thank you

                    – AndroSco
                    Nov 6 '17 at 12:17











                  • May I know how to allow them to type 11 numbers only? @V.Kalyuzhnyu

                    – Leon Zaii
                    Oct 31 '18 at 4:14











                  • @LeonZaii allow only typing "1" and check that string length divisible by two

                    – V. Kalyuzhnyu
                    Oct 31 '18 at 6:17











                  • noted. Thanks @V.Kalyuzhnyu

                    – Leon Zaii
                    Oct 31 '18 at 8:38














                  7












                  7








                  7







                  Use this. Works fine



                  input.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL | InputType.TYPE_NUMBER_FLAG_SIGNED);
                  input.setKeyListener(DigitsKeyListener.getInstance("0123456789"));


                  EDIT



                  kotlin version



                  fun EditText.onlyNumbers() {
                  inputType = InputType.TYPE_CLASS_NUMBER or InputType.TYPE_NUMBER_FLAG_DECIMAL or
                  InputType.TYPE_NUMBER_FLAG_SIGNED
                  keyListener = DigitsKeyListener.getInstance("0123456789")
                  }





                  share|improve this answer















                  Use this. Works fine



                  input.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL | InputType.TYPE_NUMBER_FLAG_SIGNED);
                  input.setKeyListener(DigitsKeyListener.getInstance("0123456789"));


                  EDIT



                  kotlin version



                  fun EditText.onlyNumbers() {
                  inputType = InputType.TYPE_CLASS_NUMBER or InputType.TYPE_NUMBER_FLAG_DECIMAL or
                  InputType.TYPE_NUMBER_FLAG_SIGNED
                  keyListener = DigitsKeyListener.getInstance("0123456789")
                  }






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 20 '18 at 10:25

























                  answered Sep 29 '17 at 19:00









                  V. KalyuzhnyuV. Kalyuzhnyu

                  1,69822025




                  1,69822025








                  • 1





                    It's That i want, Thank you

                    – AndroSco
                    Nov 6 '17 at 12:17











                  • May I know how to allow them to type 11 numbers only? @V.Kalyuzhnyu

                    – Leon Zaii
                    Oct 31 '18 at 4:14











                  • @LeonZaii allow only typing "1" and check that string length divisible by two

                    – V. Kalyuzhnyu
                    Oct 31 '18 at 6:17











                  • noted. Thanks @V.Kalyuzhnyu

                    – Leon Zaii
                    Oct 31 '18 at 8:38














                  • 1





                    It's That i want, Thank you

                    – AndroSco
                    Nov 6 '17 at 12:17











                  • May I know how to allow them to type 11 numbers only? @V.Kalyuzhnyu

                    – Leon Zaii
                    Oct 31 '18 at 4:14











                  • @LeonZaii allow only typing "1" and check that string length divisible by two

                    – V. Kalyuzhnyu
                    Oct 31 '18 at 6:17











                  • noted. Thanks @V.Kalyuzhnyu

                    – Leon Zaii
                    Oct 31 '18 at 8:38








                  1




                  1





                  It's That i want, Thank you

                  – AndroSco
                  Nov 6 '17 at 12:17





                  It's That i want, Thank you

                  – AndroSco
                  Nov 6 '17 at 12:17













                  May I know how to allow them to type 11 numbers only? @V.Kalyuzhnyu

                  – Leon Zaii
                  Oct 31 '18 at 4:14





                  May I know how to allow them to type 11 numbers only? @V.Kalyuzhnyu

                  – Leon Zaii
                  Oct 31 '18 at 4:14













                  @LeonZaii allow only typing "1" and check that string length divisible by two

                  – V. Kalyuzhnyu
                  Oct 31 '18 at 6:17





                  @LeonZaii allow only typing "1" and check that string length divisible by two

                  – V. Kalyuzhnyu
                  Oct 31 '18 at 6:17













                  noted. Thanks @V.Kalyuzhnyu

                  – Leon Zaii
                  Oct 31 '18 at 8:38





                  noted. Thanks @V.Kalyuzhnyu

                  – Leon Zaii
                  Oct 31 '18 at 8:38











                  4














                  Hi All I also had this problem. I use C# with Xamarin



                  Just a slight note that I hope someone else as well.



                  I have tried multiple of the methods mentioned here.



                  edittext1.SetRawInputType(Android.Text.InputTypes.ClassNumber | Android.Text.InputTypes.NumberFlagDecimal);


                  was the closest but still did not work. As Ralf mentioned, you could use




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




                  However in my C# I did not have this. instead you do it as follow:



                  edittext1.InputType = Android.Text.InputTypes.ClassNumber | Android.Text.InputTypes.NumberFlagDecimal;


                  Please note that the ORDER of these is important. If you put Decimal first, it will still allow you to type any Characters, where If Numer is first it is only numeric, but allows a decimal seperator!






                  share|improve this answer
























                  • Non programatically I manage this with just android:inputType="numberDecimal"

                    – Michael Gabay
                    Sep 19 '17 at 16:08
















                  4














                  Hi All I also had this problem. I use C# with Xamarin



                  Just a slight note that I hope someone else as well.



                  I have tried multiple of the methods mentioned here.



                  edittext1.SetRawInputType(Android.Text.InputTypes.ClassNumber | Android.Text.InputTypes.NumberFlagDecimal);


                  was the closest but still did not work. As Ralf mentioned, you could use




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




                  However in my C# I did not have this. instead you do it as follow:



                  edittext1.InputType = Android.Text.InputTypes.ClassNumber | Android.Text.InputTypes.NumberFlagDecimal;


                  Please note that the ORDER of these is important. If you put Decimal first, it will still allow you to type any Characters, where If Numer is first it is only numeric, but allows a decimal seperator!






                  share|improve this answer
























                  • Non programatically I manage this with just android:inputType="numberDecimal"

                    – Michael Gabay
                    Sep 19 '17 at 16:08














                  4












                  4








                  4







                  Hi All I also had this problem. I use C# with Xamarin



                  Just a slight note that I hope someone else as well.



                  I have tried multiple of the methods mentioned here.



                  edittext1.SetRawInputType(Android.Text.InputTypes.ClassNumber | Android.Text.InputTypes.NumberFlagDecimal);


                  was the closest but still did not work. As Ralf mentioned, you could use




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




                  However in my C# I did not have this. instead you do it as follow:



                  edittext1.InputType = Android.Text.InputTypes.ClassNumber | Android.Text.InputTypes.NumberFlagDecimal;


                  Please note that the ORDER of these is important. If you put Decimal first, it will still allow you to type any Characters, where If Numer is first it is only numeric, but allows a decimal seperator!






                  share|improve this answer













                  Hi All I also had this problem. I use C# with Xamarin



                  Just a slight note that I hope someone else as well.



                  I have tried multiple of the methods mentioned here.



                  edittext1.SetRawInputType(Android.Text.InputTypes.ClassNumber | Android.Text.InputTypes.NumberFlagDecimal);


                  was the closest but still did not work. As Ralf mentioned, you could use




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




                  However in my C# I did not have this. instead you do it as follow:



                  edittext1.InputType = Android.Text.InputTypes.ClassNumber | Android.Text.InputTypes.NumberFlagDecimal;


                  Please note that the ORDER of these is important. If you put Decimal first, it will still allow you to type any Characters, where If Numer is first it is only numeric, but allows a decimal seperator!







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jun 17 '16 at 6:03









                  Marius Van WykMarius Van Wyk

                  514




                  514













                  • Non programatically I manage this with just android:inputType="numberDecimal"

                    – Michael Gabay
                    Sep 19 '17 at 16:08



















                  • Non programatically I manage this with just android:inputType="numberDecimal"

                    – Michael Gabay
                    Sep 19 '17 at 16:08

















                  Non programatically I manage this with just android:inputType="numberDecimal"

                  – Michael Gabay
                  Sep 19 '17 at 16:08





                  Non programatically I manage this with just android:inputType="numberDecimal"

                  – Michael Gabay
                  Sep 19 '17 at 16:08











                  2














                  EditText edit = new EditText(this);

                  edit.setHorizontallyScrolling(true);
                  edit.setInputType(EditorInfo.TYPE_NUMBER_FLAG_SIGNED|EditorInfo.TYPE_CLASS_NUMBER);





                  share|improve this answer






























                    2














                    EditText edit = new EditText(this);

                    edit.setHorizontallyScrolling(true);
                    edit.setInputType(EditorInfo.TYPE_NUMBER_FLAG_SIGNED|EditorInfo.TYPE_CLASS_NUMBER);





                    share|improve this answer




























                      2












                      2








                      2







                      EditText edit = new EditText(this);

                      edit.setHorizontallyScrolling(true);
                      edit.setInputType(EditorInfo.TYPE_NUMBER_FLAG_SIGNED|EditorInfo.TYPE_CLASS_NUMBER);





                      share|improve this answer















                      EditText edit = new EditText(this);

                      edit.setHorizontallyScrolling(true);
                      edit.setInputType(EditorInfo.TYPE_NUMBER_FLAG_SIGNED|EditorInfo.TYPE_CLASS_NUMBER);






                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Feb 22 '13 at 18:11

























                      answered Feb 21 '13 at 17:06









                      Yasin HassanienYasin Hassanien

                      3,33111513




                      3,33111513























                          1














                          TO set the input type of EditText as decimal use this code:



                          EditText edit = new EditText(this);
                          edit.SetRawInputType(Android.Text.InputTypes.NumberFlagDecimal | Android.Text.InputTypes.ClassNumber);





                          share|improve this answer






























                            1














                            TO set the input type of EditText as decimal use this code:



                            EditText edit = new EditText(this);
                            edit.SetRawInputType(Android.Text.InputTypes.NumberFlagDecimal | Android.Text.InputTypes.ClassNumber);





                            share|improve this answer




























                              1












                              1








                              1







                              TO set the input type of EditText as decimal use this code:



                              EditText edit = new EditText(this);
                              edit.SetRawInputType(Android.Text.InputTypes.NumberFlagDecimal | Android.Text.InputTypes.ClassNumber);





                              share|improve this answer















                              TO set the input type of EditText as decimal use this code:



                              EditText edit = new EditText(this);
                              edit.SetRawInputType(Android.Text.InputTypes.NumberFlagDecimal | Android.Text.InputTypes.ClassNumber);






                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited Jan 4 '14 at 6:28

























                              answered Jan 4 '14 at 2:16









                              fulgenfulgen

                              962212




                              962212























                                  0














                                  my solution:`



                                     public void onTextChanged(CharSequence s, int start, int before, int count) {
                                  char ch=s.charAt(start + count - 1);
                                  if (Character.isLetter(ch)) {
                                  s=s.subSequence(start, count-1);
                                  edittext.setText(s);
                                  }





                                  share|improve this answer




























                                    0














                                    my solution:`



                                       public void onTextChanged(CharSequence s, int start, int before, int count) {
                                    char ch=s.charAt(start + count - 1);
                                    if (Character.isLetter(ch)) {
                                    s=s.subSequence(start, count-1);
                                    edittext.setText(s);
                                    }





                                    share|improve this answer


























                                      0












                                      0








                                      0







                                      my solution:`



                                         public void onTextChanged(CharSequence s, int start, int before, int count) {
                                      char ch=s.charAt(start + count - 1);
                                      if (Character.isLetter(ch)) {
                                      s=s.subSequence(start, count-1);
                                      edittext.setText(s);
                                      }





                                      share|improve this answer













                                      my solution:`



                                         public void onTextChanged(CharSequence s, int start, int before, int count) {
                                      char ch=s.charAt(start + count - 1);
                                      if (Character.isLetter(ch)) {
                                      s=s.subSequence(start, count-1);
                                      edittext.setText(s);
                                      }






                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Dec 7 '18 at 17:57









                                      Meh yitMeh yit

                                      141




                                      141






























                                          draft saved

                                          draft discarded




















































                                          Thanks for contributing an answer to Stack Overflow!


                                          • Please be sure to answer the question. Provide details and share your research!

                                          But avoid



                                          • Asking for help, clarification, or responding to other answers.

                                          • Making statements based on opinion; back them up with references or personal experience.


                                          To learn more, see our tips on writing great answers.




                                          draft saved


                                          draft discarded














                                          StackExchange.ready(
                                          function () {
                                          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f6919360%2fhow-do-i-restrict-my-edittext-input-to-numerical-possibly-decimal-and-signed-i%23new-answer', 'question_page');
                                          }
                                          );

                                          Post as a guest















                                          Required, but never shown





















































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown

































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown







                                          Popular posts from this blog

                                          MongoDB - Not Authorized To Execute Command

                                          How to fix TextFormField cause rebuild widget in Flutter

                                          in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith