SpaceApple
SpaceApple

Reputation: 1327

Xamarin SetError validation on EditText

So I am wondering why my SetError method is not showing my icon on the editText?

All I want to do is show the "Tick" icon for correct validation and a "Cross" icon when validation failed.

Here is my activity

    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);
        SetContentView(Resource.Layout.LoginView);
        // Create your application here

        emailEditText = (EditText)FindViewById<EditText>(Resource.Id.etUserName);
        passEditText = (EditText)FindViewById<EditText>(Resource.Id.etPass);

        Drawable errorIcon = Resources.GetDrawable(Resource.Drawable.Icon);
        Drawable correctIcon = Resources.GetDrawable(Resource.Drawable.Icon_sy);

        //Android.Graphics.Drawables.Drawable.CreateFromPath("@icon");

        emailEditText.TextChanged += (object sender, Android.Text.TextChangedEventArgs e) =>
        {
            if (IsValidEmail(e.Text.ToString()))
            {
                //change icon to green
                emailEditText.SetError("right", errorIcon);
                //emailEditText.SetError(
            }
            else
            {
                //emailEditText.set
                emailEditText.SetError("wrong", correctIcon);
                //change icon to red
            }
        };

    }

Here is my layout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:local="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity"
    android:background="#000000">
    <FrameLayout
        android:id="@+id/content"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:minWidth="25px"
        android:minHeight="25px" />
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:padding="10dp">
        <EditText
            android:id="@+id/etUserName"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/edittext_top_bg"
            android:padding="10dp"
            android:hint="Email"
            android:textColorHint="#ADA6A6"
            style="@style/DefaultTextBox"
            android:drawableLeft="@drawable/email"
            android:inputType="textEmailAddress" />
        <EditText
            android:id="@+id/etPass"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/edittext_bottom_bg"
            android:layout_marginTop="-2dp"
            android:padding="10dp"
            android:hint="Password"
            android:textColorHint="#ADA6A6"
            android:password="true"
            style="@style/DefaultTextBox"
            android:drawableLeft="@drawable/password"
            android:inputType="textPassword" />
        <LinearLayout
            android:orientation="horizontal"
            android:minWidth="25px"
            android:minHeight="25px"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/linearLayout1">
            <CheckBox
                android:text="Remember Me?"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:id="@+id/checkBox1" />
        </LinearLayout>
        <Button
            android:id="@+id/btnSingIn"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:layout_margin="4dp"
            android:text="Sign In"
            style="@style/DefaultButtonText"
            android:background="@drawable/button_default_bg"
            local:MvxBind="Click LoginCommand" />
    </LinearLayout>
</RelativeLayout>

Do you guys have any suggestions to why I cannot get my icons to display or is there a easier way of validating editText that will show and message and icon?

Upvotes: 1

Views: 5880

Answers (1)

Mukesh Modhvadiya
Mukesh Modhvadiya

Reputation: 2178

If you want to just display the icons on validations then you can do it like this :

if (IsValidEmail(e.Text.ToString()))
{
    //change icon to green
    emailEditText.SetCompoundDrawablesWithIntrinsicBounds(0, 0, Resource.Drawable.correctIcon, 0);
}
else
{
    //change icon to red
    emailEditText.SetCompoundDrawablesWithIntrinsicBounds(0, 0, Resource.Drawable.errorIcon, 0);
}

Well, you should create a method for this so you can use it everywhere, or you can also override the base SetError method for this behaviour.

Upvotes: 3

Related Questions