Joshua Rogers
Joshua Rogers

Reputation: 3568

EditText capturing events on Android

I have the following peace of code on a Xamarin Android app

        _editText = new EditText(context);
        _editText.Text = textData.Text;         
        _editText.Gravity = GravityFlags.Top;
        _editText.Clickable = true;
        _editText.Enabled = false;
        _editText.Focusable = true;
        _editText.FocusableInTouchMode = false;
        _editText.SetBackgroundResource(Color.Transparent);

        AddView(_editText);

The EditText widget gets added inside a framelayout I want that when the EditText is not editable or disabled, the events to propagate to the parent layout, a RelativeLayout in this case. If I click on the EditBox the Touch event of the parent RelativeLayout never gets called If I change the EditBox to a TextView, the event reaches the RelativeLayout's touch event.

How can I set the EditText to disabled but still allow events to propagate to the parent? Even though this is Xamarin.Android the solution is probably the same in Kotlin or Java.

Thanks!

Upvotes: 1

Views: 143

Answers (1)

Leon Lu
Leon Lu

Reputation: 9274

If you set _editText.Enabled = false this touch event will not executed. If you want to achieve the not editable, disable result and need touch event, you can use following code.

  EditText _editText = new EditText(Context);
            _editText.Text =" textData.Text";
            _editText.Gravity = GravityFlags.Top;
           //make the color like EditText is disabled
            _editText.SetTextColor(Color.Argb(80,88,88,88));
            _editText.Clickable = true;
            _editText.Enabled = true;
           //cannot get the focus, cursor cannot be seen, text cannot be selected
            _editText.Focusable = false;
            _editText.SetCursorVisible( false);
            _editText.SetTextIsSelectable(false);

        
            _editText.SetBackgroundResource(Color.Transparent);

If I add a click lisener, it executed as normal.

enter image description here

Upvotes: 1

Related Questions