android: the first digit in the edit text can not be zero
Asked Answered
V

7

11

I have an EditText element with a number input type. I don't want users to be able to enter 0 as the first digit. Can I enforce this using XML?

Here is what I have currently:

<EditText 
                android:id="@+id/main_edt_loan"
                android:layout_width="fill_parent"
                android:layout_height="40dip"
                android:background="@drawable/sample_editbox"
                android:ems="10"
                android:layout_margin="10dip"
                android:inputType="number"
                android:hint=""
                android:ellipsize="start" 
                android:maxLines="1"
                android:imeOptions="actionNext"
                android:textSize="18dip"
                android:gravity="center"
                android:digits="0123456789"
                android:maxLength="10"
                android:textColor="#000000"/>
Vidrine answered 5/7, 2013 at 13:26 Comment(0)
L
7

There is an alternative way in which you can use textWatcher. Check the length of editable text inside afterTextChange method. If the length is 1 and the string is 0 then remove 0.

Latisha answered 5/7, 2013 at 13:31 Comment(4)
Thank you. Could u explain it more?Vidrine
edt_loan.addTextChangedListener(new TextWatcher() { @Override public void onTextChanged(CharSequence s, int start, int before, int count) { // TODO Auto-generated method stub length_loan = edt_loan.length(); if (length_loan == 1 && edt_loan.getText().toString() == "0") edt_loan.setText(""); } } });Vidrine
Yes,you are doing right.But just put your code in afterTextChanged method instead of onTextChange and also use "equals()" method instead of "==" .Latisha
if you enter a number and then paste 0 on the start your code will not work. Here is better solution. if (s.toString().startsWith("0")) s.delete(0,1);Fitzger
F
10

Its just simple as that.

edContactNumber.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            if (s.toString().length() == 1 && s.toString().startsWith("0")) {
                s.clear();
            }
        }
    });
Fowlkes answered 26/7, 2018 at 4:36 Comment(0)
L
7

There is an alternative way in which you can use textWatcher. Check the length of editable text inside afterTextChange method. If the length is 1 and the string is 0 then remove 0.

Latisha answered 5/7, 2013 at 13:31 Comment(4)
Thank you. Could u explain it more?Vidrine
edt_loan.addTextChangedListener(new TextWatcher() { @Override public void onTextChanged(CharSequence s, int start, int before, int count) { // TODO Auto-generated method stub length_loan = edt_loan.length(); if (length_loan == 1 && edt_loan.getText().toString() == "0") edt_loan.setText(""); } } });Vidrine
Yes,you are doing right.But just put your code in afterTextChanged method instead of onTextChange and also use "equals()" method instead of "==" .Latisha
if you enter a number and then paste 0 on the start your code will not work. Here is better solution. if (s.toString().startsWith("0")) s.delete(0,1);Fitzger
H
5

No. But what you can do is check in your activity every time the text changes and then do whatever needs to be done when the first symbol is a "0".

EditText main_edt_loan = (EditText) findViewById(R.id.main_edt_loan);
main_edt_loan.addTextChangedListener(new TextWatcher()
{
    public void afterTextChanged(Editable s) 
    {
        String x = s.toString
        if(x.startsWith("0"))
        {
            //your stuff here
        }
    }
    public void beforeTextChanged(CharSequence s, int start, int count, int after)
{

}
public void onTextChanged(CharSequence s, int start, int before, int count)
{

}
}); 
Harquebusier answered 5/7, 2013 at 13:32 Comment(0)
M
1

Here's the Kotlin code

mainEdtLoant.addTextChangedListener(
    afterTextChanged = {
        if (!it.isNullOrBlank() && it[0] == '0') {
            it.delete(0, 1)
        }
    }
)
Marrymars answered 1/12, 2022 at 4:16 Comment(0)
S
0
public class PreventZeroAtBeginningFilter implements InputFilter {

    @Override
    public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
        if (dest.toString().equalsIgnoreCase("")) {
            String charSet = "0";
            if (source != null && charSet.contains(("" + source))) {
                return "";
            }
        }
        return null;
    }
}
Stamey answered 6/3, 2018 at 7:43 Comment(0)
F
0
 private void preFix() {
    phone.setText("+243");
    Selection.setSelection(phone.getText(), phone.getText().length());
    phone.addTextChangedListener(new TextWatcher() {
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // TODO Auto-generated method stub
            if (s.toString().startsWith("+2430")) {
                phone.setText("+243");
                Selection.setSelection(phone.getText(), phone.getText().length());
            }

        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                                      int after) {
            // TODO Auto-generated method stub
        }

        @Override
        public void afterTextChanged(Editable s) {
            if (!s.toString().startsWith("+243")) {
                phone.setText("+243");
                Selection.setSelection(phone.getText(), phone.getText().length());
            }
        }
    });
}
Farrahfarrand answered 1/5, 2020 at 20:22 Comment(0)
L
-3

Try this,

In XML,

android:text="0"

In Java

edit.setSelection(1);
Leucippus answered 5/7, 2013 at 14:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.