Skip to content

Commit f026374

Browse files
author
KB Bot
committed
Added new kb article change-autocomplete-cursor-color-dotnet-maui
1 parent 73933f6 commit f026374

File tree

1 file changed

+118
-0
lines changed

1 file changed

+118
-0
lines changed
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
---
2+
title: Changing AutoComplete Cursor Color on Android and iOS
3+
description: Learn how to change the cursor color of the AutoComplete control on Android and on iOS to match the application's input field styling.
4+
type: how-to
5+
page_title: Adjusting Cursor Color for AutoComplete Control in UI for .NET MAUI
6+
meta_title: Adjusting Cursor Color for AutoComplete Control in UI for .NET MAUI
7+
slug: change-autocomplete-cursor-color-dotnet-maui
8+
tags: autocomplete, ui for .net maui, cursor color, android, radtextinput, handlerchanged, native customization
9+
res_type: kb
10+
---
11+
12+
## Environment
13+
14+
| Version | Product | Author |
15+
| --- | --- | ---- |
16+
| 8.0.0 and above | Telerik UI for .NET MAUI AutoComplete | [Dobrinka Yordanova](https://www.telerik.com/blogs/author/dobrinka-yordanova) |
17+
18+
## Description
19+
20+
I am trying to change the cursor color of the [UI for .NET MAUI AutoComplete](https://www.telerik.com/maui-ui/documentation/controls/autocomplete/overview) control on Android and on iOS to match the color of other input fields in the application. However, the available documentation for customizing the cursor is for versions 5.2.0 to 7.0.0 and is incompatible with the current version. Since version 8.0.0, the control uses `RadTextInput` internally instead of `RadEntry`, which requires different logic for cursor customization.
21+
22+
This knowledge base article also answers the following questions:
23+
- How to update AutoComplete cursor color on mobile?
24+
- How to handle breaking changes for cursor customization in AutoComplete after version 8.0.0?
25+
- How to apply native logic to change cursor color in AutoComplete control?
26+
27+
## Solution
28+
29+
To modify the cursor color in the AutoComplete control, follow these steps:
30+
31+
1. Handle the `Loaded` event of the AutoComplete control to locate the internal `RadTextInput` control and apply the necessary native logic.
32+
2. Use the `Handler` property of `RadTextInput` to update the cursor color for Android and iOS.
33+
34+
```csharp
35+
private void autoComplete_Loaded(object sender, EventArgs e)
36+
{
37+
var textInput = ChildOfType<RadTextInput>(this.autoComplete);
38+
if (textInput != null)
39+
{
40+
var handler = textInput.Handler;
41+
if (handler == null)
42+
{
43+
textInput.HandlerChanged += this.OnTextInputHandlerChanged;
44+
}
45+
else
46+
{
47+
this.UpdateNativeElement(handler);
48+
}
49+
}
50+
}
51+
52+
private void OnTextInputHandlerChanged(object sender, EventArgs e)
53+
{
54+
var textInput = (RadTextInput)sender;
55+
this.UpdateNativeElement(textInput.Handler);
56+
textInput.HandlerChanged -= this.OnTextInputHandlerChanged;
57+
}
58+
59+
private void UpdateNativeElement(IViewHandler handler)
60+
{
61+
var nativeEntry = handler.PlatformView as Telerik.Maui.Platform.RadMauiTextInput;
62+
if (nativeEntry != null)
63+
{
64+
#if ANDROID
65+
nativeEntry.TextCursorDrawable?.SetColorFilter(new Android.Graphics.PorterDuffColorFilter(Android.Graphics.Color.Red, Android.Graphics.PorterDuff.Mode.Darken));
66+
#elif __IOS__
67+
nativeEntry.TintColor = UIKit.UIColor.Red;
68+
#endif
69+
}
70+
}
71+
72+
internal static T ChildOfType<T>(View visualElement) where T : View
73+
{
74+
if (visualElement == null)
75+
{
76+
return null;
77+
}
78+
79+
foreach (var item in VisualTreeElementExtensions.GetVisualTreeDescendants(visualElement))
80+
{
81+
if (item is T targetElement)
82+
{
83+
return targetElement;
84+
}
85+
}
86+
87+
return null;
88+
}
89+
```
90+
91+
3. Add sample AutoComplete definition in XAML:
92+
93+
```xaml
94+
<VerticalStackLayout>
95+
<telerik:RadAutoComplete x:Name="autoComplete"
96+
Loaded="autoComplete_Loaded"
97+
Placeholder="Search here..." />
98+
</VerticalStackLayout>
99+
```
100+
101+
4. Add sample data for the AutoComplete source:
102+
103+
```csharp
104+
this.autoComplete.ItemsSource = new List<string>()
105+
{
106+
"Freda Curtis",
107+
"Jeffery Francis",
108+
"Eva Lawson",
109+
"Emmett Santos",
110+
"Theresa Bryan",
111+
"Jenny Fuller",
112+
};
113+
```
114+
115+
## See Also
116+
- [Breaking Changes in UI for .NET MAUI 8.0.0](https://www.telerik.com/maui-ui/documentation/upgrade/breaking-changes/8-0-0)
117+
- [Feature Request: Change Cursor Color in AutoComplete](https://feedback.telerik.com/maui/1597050-autocomplete-provide-an-option-to-change-the-color-of-the-caret-cursor)
118+
- [UI for .NET MAUI AutoComplete Overview](https://www.telerik.com/maui-ui/components/autocomplete/overview)

0 commit comments

Comments
 (0)