Veljko
Veljko

Reputation: 1903

Android EditText file format

I have edit text in my app, that captures only decimal numbers. I've set an input type to DECIMAL_NUMBERS.

The problem is, what if user puts for example 0009 I want to prevent that, and remove zeros on beginning.

Any idea?

Upvotes: 0

Views: 130

Answers (3)

C.d.
C.d.

Reputation: 9995

You can get rid of the zeros via a regex and replaceFirst function of string replaceFirst("^0+(?!$)", "")

You can try the regex here: http://regexr.com?32f1o

Upvotes: 0

Mohamed_AbdAllah
Mohamed_AbdAllah

Reputation: 5322

Override onTextChanged (CharSequence text, int start, int lengthBefore, int lengthAfter) method to correct user input and add it using addTextChangedListener

Upvotes: 3

Veljko
Veljko

Reputation: 1903

This is the solution. Tnx. :)

@Override
public void onTextChanged(CharSequence s, int start, int before,
                int count) {
            if (start == 0 && s.toString().equals("0"))
                editText.setText("");
        };

Upvotes: 0

Related Questions