Lacondeguy Basic
Lacondeguy Basic

Reputation: 23

Check EditText for character count

I writing Android app via Xamarin (C#)

I have EditText field. And have check for NullOrEmpty.

            if (string.IsNullOrEmpty (ulitsa.Text) ) {
                Toast.MakeText (this, "Заполните поле 'Ваша Улица'", ToastLength.Long).Show ();
            }

I want to set max and min character filters. Min-3, Max-6 ,and if user don't have this count of characters show toast notification.

How I can realize this?

Upvotes: 1

Views: 525

Answers (2)

Lacondeguy Basic
Lacondeguy Basic

Reputation: 23

I solve problem

if(string.IsNullOrEmpty(kod.Text ) || kod.ToString().Length < 3 ||kod.ToString().Length > 3 )
            {
                Toast.MakeText (this, "Заполните поле 'Код'", ToastLength.Long).Show();
            }
            if(string.IsNullOrEmpty(tel.Text ) || tel.ToString().Length < 1 ||tel.ToString().Length > 7)
            {
                Toast.MakeText (this, "Заполните поле 'Телефон'", ToastLength.Long).Show();
            }

Upvotes: 0

Nicolas Cortell
Nicolas Cortell

Reputation: 649

myEditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // TODO Auto-generated method stub
            }

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

            @Override
            public void afterTextChanged(Editable s) {

                //Here, get text length:
                Integer len = myEditText.getText().toString().length();

               // Then, depending on the length display the toast and do what you want
            }
        });

But you can actually set the max length programmatically:

InputFilter[] filterArray = new InputFilter[1];
filterArray[0] = new InputFilter.LengthFilter(6);
myEditText.setFilters(filterArray);

Upvotes: 1

Related Questions