2

I have an EditText, that already have inputType is number. This ensure only digit from 0-9 could be entered.

    <androidx.appcompat.widget.AppCompatEditText
        android:id="@+id/uiEditTextNumber"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="36dp"
        android:inputType="number"
        android:text="0"
        android:textAlignment="center" />

However, the problem is, user could still enter 000 or even 0123, which doesn't look legitimate. I also want in such a way, when there's no character, it will return to 0 (instead of blank "")

I search on Stackoverflow, and found most question revolve around limiting the enter to using android:inputType="number", but nothing mentioned how to avoid 00 or 0123 by auto convert them to 0 and 123.

I made the function below, but seems hacky, where i have to manually change the text and move the cursor.

        uiEditTextNumber.doAfterTextChanged {
            if (it.isNullOrBlank()) {
                modifyText("0")
                return@doAfterTextChanged
            }
            val originalText = it.toString()
            try {
                val numberText = originalText.toInt().toString()
                if (originalText != numberText) {
                    modifyText(numberText)
                }
            } catch (e: Exception) {
                modifyText("0")
            }
        }

// ... AND the function

    private fun modifyText(numberText: String) {
        uiEditTextNumber.setText(numberText)
        uiEditTextNumber.setSelection(numberText.length)
    }

Any better solution out there?

Note the answer in Is it possible to forbid the first number in a EditText to be "0" is not helping, as it is just preventing the user from entering 01 and 0123, but doesn't automatically change 0 to 1 when one type 1. Besides, it also doesn't ensure when nothing is in the EditText, it automatically set to 0.

Elye
  • 53,639
  • 54
  • 212
  • 474
  • Does this answer your question? [How to check if a String is numeric in Java](https://stackoverflow.com/questions/1102891/how-to-check-if-a-string-is-numeric-in-java) – jef Apr 04 '20 at 06:46
  • No, that's just to convert string to number. I know how to do that. I want to on the fly change my edittext entry to the legitimate number. I use `doAfterTextChanged`, but that will call `doAfterTextChanged` infinitely ... so I need to do some if-else check to stop the recursive run. Looks hacky – Elye Apr 04 '20 at 06:49
  • I have also ensure the entry is definitely 0-9 using the `inputType="number"`. So it's definitely no issue only getting number. But then, it doesn't prevent one from entering `00` or `0123`, which technical is a number, but it is not something anyone would enter. – Elye Apr 04 '20 at 06:51
  • 1
    Does this answer your question? [Is it possible to forbid the first number in a EditText to be "0"](https://stackoverflow.com/questions/24406447/is-it-possible-to-forbid-the-first-number-in-a-edittext-to-be-0) – UnBanned Apr 04 '20 at 07:42
  • The https://stackoverflow.com/q/24406447/3286489 is not answering it. I have updated my question to state why it is not answering. – Elye Apr 05 '20 at 00:10
  • 1
    I do think this is the best solution @Elye , so thanks for it and the Medium post :D – Daniel Wilson Jul 22 '20 at 17:23

1 Answers1

0

to achieve your goal you can add the following function

fun removeLeadingZeros(strNumber: String): String {
        for (i in strNumber.indices) {
            val c = strNumber[i]
            if (c != '0') {
                return strNumber.substring(i)
            }
        }
        //If the number consists only from zeros, return zero.
        return "0"
    }

Then, when you need to get the text from the editText, you can call the function like so

str = removeLeadingZeros(strNumber)
UnBanned
  • 121
  • 11
  • Thanks. When will `removeLeadingZeros` be called? Isn't that kind of achieve by what I wrote above `val numberText = originalText.toInt().toString()`? Sorry, it still doesn't solve my inquiry of a better way not hacking it manually. – Elye Apr 04 '20 at 07:33
  • Hey, just updated my answer, you can call the function whenever you need to get the text from the editText – UnBanned Apr 04 '20 at 07:36
  • That's not answer my question of not to use `doAfterTextChanged`. I still have to resort to use `doAfterTextChanged` that doesn't seems to be ideal for my case. – Elye Apr 04 '20 at 07:40
  • I tried finding answers on google, looks like this question might be a duplicate. https://stackoverflow.com/questions/24406447/is-it-possible-to-forbid-the-first-number-in-a-edittext-to-be-0/24406700 – UnBanned Apr 04 '20 at 07:41
  • Also, you didn't mention that you don't want a "do after text change", therefore I suggest you edit your question ;) – UnBanned Apr 04 '20 at 07:45
  • The answer https://stackoverflow.com/questions/24406447/is-it-possible-to-forbid-the-first-number-in-a-edittext-to-be-0/24406700 is not answering as it is just toasting error, and not making it to be numeric. – Elye Apr 04 '20 at 09:37
  • I'm okay to use `doAfterTextChanged` if it doesn't need to do so many if-else within. So the problem is not `doAfterTextChanged`, and not how to convert String to Int. But how could we make EditText to dynamically like a calculator only accepting 0, 1, 2, 3... and not 01, 02, 003. – Elye Apr 04 '20 at 09:38
  • I'm confused, you said in your comment "That's not answer my question of not to use doAfterTextChanged." – UnBanned Apr 04 '20 at 10:22
  • However, the link I gave you is the exact question. They solved it by using input filter, which is the only way of making sure that the edit text doesn't accept a leading zero. – UnBanned Apr 04 '20 at 10:24
  • Doesn't accept leading zero is different from when the text is already "0", then when one key in "1", it should be "1" instead of "01". ... The answers in https://stackoverflow.com/q/24406447/3286489 is not helping. They just avoid type on when it is already "0". User have to explicitly remove 0 to type in other number, which is annoying. – Elye Apr 05 '20 at 00:06