Looking to remove underline from Android Entry and Pickers in .NET Maui

Isspro-eng 40 Reputation points
2024-04-08T18:00:56.82+00:00

I need to remove the underline in my .NET Maui application for entry and picker handlers. In addition to this, I need to set the background color to a custom color that I have. I have found ways to set the background color to transparent to remove the underline, but this does not allow me to then change the background color I have seen the following code to remove the underline, but it overrides any BackgroundColor setting on my pages Xaml.

protected override MauiApp CreateMauiApp()
    {
        // Remove Entry control underline
        Microsoft.Maui.Handlers.EntryHandler.Mapper.AppendToMapping("NoUnderline", (h, v) =>
        {
            h.PlatformView.BackgroundTintList =
                Android.Content.Res.ColorStateList.ValueOf(Colors.Transparent.ToAndroid());
        });

        return MauiProgram.CreateMauiApp();
    }
.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
2,890 questions
{count} votes

Accepted answer
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 35,576 Reputation points Microsoft Vendor
    2024-04-09T02:45:02.5433333+00:00

    Hello,

    If you need to remove the underline and change the background color at the same time, just replace Transparent with the color you need.

    Microsoft.Maui.Handlers.EntryHandler.Mapper.AppendToMapping("NoUnderline", (h, v) =>
    {
    	h.PlatformView.BackgroundTintList =
    	Android.Content.Res.ColorStateList.ValueOf(Colors.Red.ToAndroid());
    });
    

    For one specific page, I need it transparent. For the rest, I need it Red. How would I implement this?

    To achieve this requirement, you need to write this method in the construction method of the corresponding page, not in the CreateMauiApp method.

    For example, you have two pages, one called MainPage and one called NewPage1. You need to make MainPage transparent and set the background color to red in NewPage1.

    MainPage.cs:

    public MainPage(){
    	InitializeComponent();
    	Microsoft.Maui.Handlers.EntryHandler.Mapper.AppendToMapping("NoUnderline", (h, v) =>{	h.PlatformView.BackgroundTintList =	Android.Content.Res.ColorStateList.ValueOf(Colors.Transparent.ToAndroid());
    }
    

    NewPage1.cs:

    csharp
    public NewPage1(){
    
    	InitializeComponent();
    	Microsoft.Maui.Handlers.EntryHandler.Mapper.AppendToMapping("NoUnderline", (h, v) =>{	h.PlatformView.BackgroundTintList =	Android.Content.Res.ColorStateList.ValueOf(Colors.Red.ToAndroid());
    }
    

    Best Regards, Alec Liu.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our [documentation][1] to enable e-mail notifications if you want to receive the related email notification for this thread.

    
    

0 additional answers

Sort by: Most helpful