1

Is there a way to set an End Icon Click Listener on a TextInputLayout in Xamarin?

I'm trying to do something similar to what people did in this post but in Xamarin.

  • Good question. I see that googling for xamarin equivalent of that doesn't return any useful xamarin answer. Hopefully someone knows a way to do this. – ToolmakerSteve Aug 10 '22 at 20:45

1 Answers1

0

I did a test on my side,and it works properly on my side.

please refer to the following code:

The .xml code:

<com.google.android.material.textfield.TextInputLayout
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:id="@+id/textInputLayout1"
    
     android:layout_marginHorizontal="20dp"
     app:helperText="Test"
     app:endIconMode="custom"
     app:endIconDrawable="@drawable/haixing"
    >

    <com.google.android.material.textfield.TextInputEditText
        android:id="@+id/textInputEditText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</com.google.android.material.textfield.TextInputLayout>

MainActivity.cs

TextInputLayout textInputLayout;

textInputLayout = FindViewById<TextInputLayout>(Resource.Id.textInputLayout1);           
textInputLayout.SetEndIconOnClickListener( new MyTestClick(this));

class MyTestClick.cs

    class MyTestClick : Java.Lang.Object, Android.Views.View.IOnClickListener
    {

        Context mContext;
        public MyTestClick(Context context) {

            mContext = context;
        }

        public void OnClick(View v)
        {
            Toast.MakeText(mContext, Resource.String.action_test, ToastLength.Long).Show();
        }
    }

Note: please update your android nuget to the latest version;

Jessie Zhang -MSFT
  • 9,830
  • 1
  • 7
  • 19
  • Thanks, that works. But how would you handle multiple clickable end icons in the same activity? – Hercules Trainer Aug 11 '22 at 08:10
  • It's the same way . You can use the same class `MyTestClick`. In order to distinguish different `SetEndIconOnClickListener` events of `textInputLayout` , you can add a parameter to the constructor of `MyTestClick`'. – Jessie Zhang -MSFT Aug 11 '22 at 08:34
  • Is there a way to do it dynamically, without that parameter? With your solution I would have to manually check that parameter in the OnClick function and execute the code associated with that parameter. The way people do this in Java is by creating an anonymous (like what the first answer in the post I linked did) OnClick method and putting the code for a specific end Icon in there. Is there a way to do something similar to that in Xamarin? – Hercules Trainer Aug 11 '22 at 08:49
  • Sorry for the delay response. For `View.IOnClickListener`,we cann't create an anonymous class in xamarin android as we do in native android. – Jessie Zhang -MSFT Aug 16 '22 at 03:42