Ruairi
Ruairi

Reputation: 35

Xamarin Android EditText - Required field error displaying when control not empty

Creating a dynamic EditText and adding 'some text' will display the required field error message even though text has been added to the control...

//Create EditText
editText = new EditText((Activity)form);
editText.SetTextAppearance(Resource.Style.MyCustomTheme);
editText.Text = "Some Text";
editText.Error = Html.FromHtml("<font color='black'>Cannot be Empty</font>").ToString();

Error is displayed even though text is in the control

Upvotes: 1

Views: 1539

Answers (2)

AskNilesh
AskNilesh

Reputation: 69754

you have give condition for it to check if edittext is empty or not try this

editText = new EditText((Activity)form);
editText.SetTextAppearance(Resource.Style.MyCustomTheme);
editText.Text = "Some Text";
editText.TextChanged += (sender, e) => {

   if (TextUtils.IsEmpty((sender as EditText).Text))
    {
        editText.SetError("Text Can't be empty!", null);

    }
};

Upvotes: 0

VenkyDhana
VenkyDhana

Reputation: 905

Try the below

editText = new EditText((Activity)form);
editText.TextChanged += (sender, e) => {

       if (TextUtils.IsEmpty((sender as EditText).Text))
        {
            editText.SetError("Text Can't be empty!", null);

        }
};

Upvotes: 2

Related Questions