Nothing gets typed in EditText when EditText.RequestFocus() and EditText.SetError() gets called

AnkushSharma.5312AnkushSharma.5312 USMember ✭✭
edited April 2016 in Xamarin.Android

If focus changes from first EditText i.e mTxtFirstNameand if mTxtFirstName is empty then:-
1) The new focussed EditText will lose focus i.e EditText.ClearFocus()
2)mTxtFirstName will get focus again i.e mTxtFirstName.RequestFocus()
3)mTxtFirstName shows error i.e mTxtFirstName.SetError("Non Empty Field",null)
4)Cursor set to 0th position of mTxtFirstName i.e. mTxtFirstName.SetSelection(0)

All things happens but when i starts typing in mTxtFirstName the typing happens in previous focussed EditText Field.See Screenshot for more Info.How to make typing enable in mTxtFirstName only till it is non empty.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#DEDEDC"
    android:minWidth="300dp"
    android:minHeight="400dp"
    android:id="@+id/MyDialog_Layout">
    <EditText
        android:theme="@android:style/Theme.Holo.Light"
        android:background="@drawable/edit_text_style"
        android:layout_marginBottom="10dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="40dp"
        android:hint="First Name"
        android:paddingLeft="10dp"
        android:textColor="#000"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:id="@+id/txtFirstName" />
    <EditText
        android:theme="@android:style/Theme.Holo.Light"
        android:background="@drawable/edit_text_style"
        android_layout_below="@+id/txtFirstName"
        android:inputType="textEmailAddress"
        android:layout_marginBottom="10dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="110dp"
        android:hint="Email"
        android:paddingLeft="10dp"
        android:textColor="#000"
        android:layout_width="match_parent"
        android:id="@+id/txtEmailId"
        android:layout_height="40dp" />
    <EditText
        android:theme="@android:style/Theme.Holo.Light"
        android:background="@drawable/edit_text_style"
        android:layout_below="@+id/txtEmailId"
        android:inputType="textPassword"
        android:layout_marginBottom="10dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="20dp"
        android:hint="Password"
        android:paddingLeft="10dp"
        android:textColor="#000"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:id="@+id/txtPassword" />
    <EditText
        android:theme="@android:style/Theme.Holo.Light"
        android:background="@drawable/edit_text_style"
        android:layout_below="@+id/txtPassword"
        android:inputType="textPassword"
        android:layout_marginBottom="10dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="20dp"
        android:hint="Confirm Password"
        android:paddingLeft="10dp"
        android:textColor="#000"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:id="@+id/txtConfirmPassword" />
    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/txtPassword"
        android:layout_marginTop="50dp">
        <Button
            android:text="Sign Up"
            android:layout_below="@+id/txtConfirmPassword"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/btnDialogEmail"
            android:layout_weight="15"
            android:background="@drawable/ButtonSignUpStyle"
            android:layout_marginLeft="30dp"
            android:textSize="20dp"
            android:textColor="#fff"
            android:textStyle="bold"
            android:layout_marginBottom="50dp"
            android:layout_marginTop="30dp"
            android:paddingLeft="5dp"
            android:paddingBottom="5dp"
            android:paddingRight="5dp"
            android:height="70dp"
            android:paddingTop="2dp"
            android:layout_centerHorizontal="true" />
    </RelativeLayout>
</RelativeLayout>

Here is Code from .cs file

  public override void OnResume()
    {
        base.OnResume();

        //Code for Validation of FirstName EditText Field on FocusChange Event

        mTxtFirstName.FocusChange += (object sender, View.FocusChangeEventArgs e) =>

        {
            if (!e.HasFocus)
            {
                if (string.IsNullOrEmpty(mTxtFirstName.Text.Trim()))

                {
                    for (int j = 0; j < mViewGroup.ChildCount; j++)

                    {
                        var childViewNew = mViewGroup.GetChildAt(j);

                        if (childViewNew is EditText && childViewNew.HasFocus)
                        {
                            childViewNew.ClearFocus();
                            mTxtFirstName.RequestFocus();
                            mTxtFirstName.SetError("Non Empty Field", null);
                            mTxtFirstName.SetSelection(0);
                        }
                    }
                }
            }
        };


    }
Sign In or Register to comment.