Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] How to set cursor colour of entry on MAUI for Android

Posted on November 11, 2022 By

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?

READ  [FIXED] android - Rotate R.drawable in kotlin
Powered by Inline Related Posts

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

Android Tags:android, darkmode, maui

Post navigation

Previous Post: [FIXED] android – LottieAnimationView doesn’t show in my_layout.xml
Next Post: [FIXED] android – Firestore conditional array query

Related Posts

[FIXED] java – How to use a single Firestore instance throughout an android app? Android
[FIXED] How to switch between two custom JSON mapStyles in google map Android SDK Android
[FIXED] java – Use “RETR” FTP command with Apache Common’s FTPClient Android
[FIXED] android – Getting error on google play console even setting Exported to true In AndroidManifest File Android
[FIXED] Firebase Firestore OR query (Android Studio) Android
[FIXED] firebase cloud messaging – Android 10 Incoming call notification like whats app when we are in another app Android

Archives

  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • What are the main features of Islamic education?
  • Is the Jeep 4xe worth it?
  • How does the ringer work on cast iron?
  • What is the biggest size interior door?
  • Is blue raspberry an original Jolly Rancher flavor?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme