Click event fires when scrolling on EditText to see all its content(Xamarin.Android)

When I scroll in an EditText to see its content when it's too long, its click event fires. How can I prevent that? The EditText is not editable.

Here is my activity:

    public class MainActivity : AppCompatActivity, View.IOnTouchListener
{
        EditText et;
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            SetContentView(Resource.Layout.activity_main);

            et = FindViewById<EditText>(Resource.Id.editText1);
            et.SetOnTouchListener(this);
            et.Click += delegate { this.OnClicking(); };
        }

        private void OnClicking()
        {
            //Show alert dialog
        }

        public bool OnTouch(View v, MotionEvent e)
        {
            v.OnTouchEvent(e);
            var imm = (Android.Views.InputMethods.InputMethodManager)v.Context.GetSystemService("input_method");
            imm?.HideSoftInputFromWindow(v.WindowToken, Android.Views.InputMethods.HideSoftInputFlags.None);
            return true;
        }

}

Here is my axml file:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:app="http://schemas.android.com/apk/res-auto"
                xmlns:tools="http://schemas.android.com/tools"
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="match_parent">
                <EditText
                        android:text="Very long text Very long text Very long text Very long text Very long text "
                        android:layout_width="300dp"
                        android:focusable="false"
                        android:clickable="true"
                        android:singleLine="true"
                        android:layout_height="wrap_content"
                        android:minWidth="25px"
                        android:minHeight="25px"
                        android:id="@+id/editText1" />
    </LinearLayout>

Best Answer

Answers

  • SvetoslavHlebarovSvetoslavHlebarov USMember ✭✭

    @yelinzh said:
    How about changing the 'Click' event to a 'LongClick' event or a 'double Click' event.It'll avoid triggering the click event when scrolling on EditText.

    Yeah, that may be considered as a valid solution but isn't there a way to remove the et.Click event and to make some checks in the OnTouch method if the touch was a fast single touch or a sliding.

Sign In or Register to comment.