How Remove DatePicker underline in .NET MAUI

Bhuwan 881 Reputation points
2024-03-04T07:11:22.7533333+00:00

In .NET MAUI, I am using a DatePicker to display a date on a page. However, on Android, it is showing an underline below it. I want to hide this underline. I have written the following code to achieve this, but it's not working. Could you please help me with how to remove the underline?

using Android.Graphics.Drawables;

using Microsoft.Maui.Controls.Platform;

using Microsoft.Maui.Handlers;

using Microsoft.Maui.Platform;

namespace Dev.Platforms.Android.Handlers

{

/// <summary>

/// DevDatePickerHandler

/// </summary>

public class DevDatePickerHandler : DatePickerHandler

{

protected override void ConnectHandler(MauiDatePicker platformView)

{

base.ConnectHandler(platformView);

GradientDrawable gd = new GradientDrawable();

gd.SetColor(global::Android.Graphics.Color.Transparent);

platformView.SetBackground(gd);

}

}

}

in MauiProgram. cs

        `ConfigureMauiHandlers((handlers) =>`
````            {`

`#if ANDROID`

`               handlers.AddHandler(typeof(DatePicker), typeof(Platforms.Android.Handlers.DevDatePickerHandler));`

`#endif`

 });
Developer technologies | .NET | .NET MAUI
0 comments No comments
{count} vote

Accepted answer
  1. Anonymous
    2024-03-05T02:29:41.0533333+00:00

    Hello,

    You can use DatePickerHandler to hide the underline with following code directly in the CreateMauiApp method of MauiProgram. cs

      Microsoft.Maui.Handlers.DatePickerHandler.Mapper.AppendToMapping("MyCustomization", (handler, view) =>
            {
    
    
    #if ANDROID
                handler.PlatformView.BackgroundTintList =
    Android.Content.Res.ColorStateList.ValueOf(Android.Graphics.Color.Transparent);
    #endif
    
    
           });
    

    Best Regards,

    Leon Lu


    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 to enable e-mail notifications if you want to receive the related email notification for this thread.

    4 people found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.