Solution 1 :
I don’t think there is a easy way to add cursor color to MAUI styles.
To change entry cursor color You could use EntryHandler and write in it android native code.
app.xaml.cs:
public partial class App : Application
{
public App()
{
InitializeComponent();
Microsoft.Maui.Handlers.EntryHandler.Mapper.AppendToMapping("CursorColor", (handler, view) =>
{
#if __ANDROID__
handler.PlatformView.TextCursorDrawable.SetTint(Colors.Green.ToAndroid());
#endif
});
MainPage = new AppShell();
}
}
Other solution would be to create custom Entry:
CursorEntry.cs:
public class CursorEntry : Entry
{
public static BindableProperty CursorColorProperty = BindableProperty.Create(
nameof(CursorColor), typeof(Color), typeof(CursorEntry), Colors.Black);
public Color CursorColor
{
get => (Color)GetValue(CursorColorProperty);
set => SetValue(CursorColorProperty, value);
}
public CursorEntry()
{
}
}
CursorEntryHandler.cs:
namespace mauicursor
{
#if ANDROID
public sealed partial class CursorEntryHandler : EntryHandler
{
public CursorEntryHandler()
{
Mapper.AppendToMapping("CursorEntryCustomization", MapCursorEntry);
}
private void MapCursorEntry(IEntryHandler entryHandler, IEntry entry)
{
if (entry is CursorEntry cursorEntry && entryHandler is CursorEntryHandler cursorEntryHandler)
{
SetCursorColor(cursorEntry);
}
}
}
#else
public partial class CursorEntryHandler : EntryHandler
{
}
#endif
}
in Platforms Android create CursorEntryHandler.android.cs:
namespace mauicursor
{
public partial class CursorEntryHandler
{
AppCompatEditText _nativeEntry;
protected override AppCompatEditText CreatePlatformView()
{
_nativeEntry = new AppCompatEditText(Context);
return _nativeEntry;
}
internal void SetCursorColor(CursorEntry entry)
{
_nativeEntry.TextCursorDrawable.SetTint(entry.CursorColor.ToAndroid());
}
}
}
add handler in MauiProgram.cs
.ConfigureMauiHandlers(handlers =>
{
handlers.AddHandler(typeof(CursorEntry), typeof(CursorEntryHandler));
});
use in any view:
xmlns:views="clr-namespace:mauicursor"
<views:CursorEntry
Text="Cursor color"
FontSize="18"
Placeholder="Green"
HorizontalOptions="Center"
CursorColor="Yellow"/>
But to use dark and light mode you would probably have to programaticaly detect if it is dark or light mode and recreate views. Maybe there are better ways for this part like somehow setting themes.
Solution 2 :
You can add the code below to app.xaml.cs file, and the TextCursorDrawable
can not be used on the device which Android API version is below 29.
public partial class App : Application
{
public App()
{
InitializeComponent();
MainPage = new AppShell();
Microsoft.Maui.Handlers.EntryHandler.Mapper.AppendToMapping("MyCustomization", (handler, view) =>
{
#if ANDROID
handler.PlatformView.TextCursorDrawable.SetTint(Colors.Red.ToPlatform());
#elif IOS || MACCATALYST
#elif WINDOWS
#endif
});
}
}
Problem :
I can’t find how to change the cursor colour of an entry field on MAUI for Android.
I’m writing an app supporting dark theme and I don’t want the entry box to have a light background. Unfortunately, the default cursor colour is dark purple, which has insufficient contrast and makes it very hard to see.
I’d be happy if it were the same colour as the text or if I could set it independently.
Any suggestions?
Comments
Comment posted by teegee
Thanks for the samples. Didn’t think it was going to be that hard – shouldn’t be. I’ll try the first one and if that doesn’t work with theme changes I’ll just make my entry colour so the cursor works for both dark and light.
Comment posted by teegee
Also, shouldn’t #2 just work with AppThemeBinding? I’ll try