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"/>
Hunter McMillen
  • 59,865
  • 24
  • 119
  • 170
Dariush Malek
  • 914
  • 1
  • 6
  • 14

7 Answers7

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();
            }
        }
    });
Prashant Jajal
  • 3,469
  • 5
  • 24
  • 38
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.

Aldan
  • 674
  • 9
  • 23
kaushal trivedi
  • 3,405
  • 3
  • 29
  • 47
  • Thank you. Could u explain it more? – Dariush Malek Jul 05 '13 at 14:02
  • 2
    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(""); } } }); – Dariush Malek Jul 05 '13 at 14:03
  • 1
    Yes,you are doing right.But just put your code in afterTextChanged method instead of onTextChange and also use "equals()" method instead of "==" . – kaushal trivedi Jul 05 '13 at 14:14
  • 1
    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); – Yuri Popiv Jul 19 '18 at 08:59
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)
{

}
}); 
Lukas Ruge
  • 2,204
  • 3
  • 30
  • 42
1

Here's the Kotlin code

mainEdtLoant.addTextChangedListener(
    afterTextChanged = {
        if (!it.isNullOrBlank() && it[0] == '0') {
            it.delete(0, 1)
        }
    }
)
Julio Rahman
  • 29
  • 1
  • 4
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;
    }
}
Anh Pham
  • 2,108
  • 9
  • 18
  • 29
Anish Vahora
  • 243
  • 3
  • 10
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());
            }
        }
    });
}
-3

Try this,

In XML,

android:text="0"

In Java

edit.setSelection(1);
No_Rulz
  • 2,679
  • 1
  • 20
  • 33