How can I change the cursor color for AuroComplete the have focused event on the MAUI

1 Answer 141 Views
AutoComplete
Hakob
Top achievements
Rank 2
Iron
Hakob asked on 14 Jun 2023, 09:43 PM
Do I have a solution to change the cursor color for AuroComplete 

1 Answer, 1 is accepted

Sort by
0
Lance | Senior Manager Technical Support
Telerik team
answered on 15 Jun 2023, 08:20 PM

Hello Hakob,

This is similar to my answer to your other forum post about changing cursor color for RadEntry, because inside the Autocomplete is a RadEntry. So, my answer to you here is going to be very similar to my answer to the RadEntry question.

Here's a link to my other thread's answer, but to summarize, you use the HandlerChanged lifecycle event to access the native controls:

public class MyCustomTelerikAutoComplete : Telerik.Maui.Controls.RadAutoComplete
{
    protected override void OnHandlerChanged()
    {
        // 1. Run the default handler logic first
        base.OnHandlerChanged();
        
#if ANDROID
        // 2. Change the cursor
        if (this?.Handler?.PlatformView is Telerik.Maui.Border.BorderViewGroup borderViewGroup)
        {
            var textInputs = borderViewGroup.GetChildrenOfType<TextInputLayout>();

            var textInput = textInputs.FirstOrDefault();

            if (textInput is { EditText.TextCursorDrawable: not null })
            {
                textInput.EditText.TextCursorDrawable.SetColorFilter(new PorterDuffColorFilter(Android.Graphics.Color.Red, PorterDuff.Mode.Darken));
            }
        }
        
#elif __IOS__

#endif
    }
}

Here's what the above code looks like at runtime:

Empowering Self-Discovery

Before I send this, I wanted to share a pro-tip with you. When you are not sure about what the data type is you're working with, you can always put a temporary line of code and run the app real quick:

That would have gotten you the Telerik.Maui.Border.BorderViewGroup for the RadAutoComplete Handler (because the autocomplete is a bunch of inner controls)... then you can look inside the view group for the EditText.

However, what if you want to also writ this for iOS/MacCatalyst? Using my trick, you would have seen the following in your output:

Viola! How you write the rest of the code is up to you, there are a wide variety of way to work with .NET iOS APIs. Here's just one example

if (this.Handler?.PlatformView is Telerik.Maui.Border.TKBorderView borderView)
{
            var textField = borderView.FindDescendantView<UITextField>();
}

Conclusion

Here's Android and iOS combined in the same implementation:

public class MyCustomTelerikAutoComplete : Telerik.Maui.Controls.RadAutoComplete
{
    protected override void OnHandlerChanged()
    {
        // 1. Run the default handler logic first
        base.OnHandlerChanged();

        // 2. Change the cursor
#if ANDROID
        if (this.Handler?.PlatformView is Telerik.Maui.Border.BorderViewGroup borderViewGroup)
        {
            var textInputs = borderViewGroup.GetChildrenOfType<TextInputLayout>();

            var textInput = textInputs.FirstOrDefault();

            if (textInput is { EditText.TextCursorDrawable: not null })
            {
                textInput.EditText.TextCursorDrawable.SetColorFilter(new PorterDuffColorFilter(Android.Graphics.Color.Red, PorterDuff.Mode.Darken));
            }
        }
#elif __IOS__
        if (this.Handler?.PlatformView is Telerik.Maui.Border.TKBorderView borderView)
        {
            // Find the UITextField inside the AutoComplete's layout
            var textField = borderView.FindDescendantView<UITextField>();
            
            // Do your customizations
            if(textField != null)
                textField.TintColor = UIColor.Red;
        }
#endif
    }
}

Regards,
Lance | Manager Technical Support
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Hakob
Top achievements
Rank 2
Iron
commented on 16 Jun 2023, 05:08 AM

Thank you, so helpful.
Tags
AutoComplete
Asked by
Hakob
Top achievements
Rank 2
Iron
Answers by
Lance | Senior Manager Technical Support
Telerik team
Share this question
or