Forum Xamarin.Forms

Background color of Picker Menu, DatePicker and Alert

CrudarCrudar Member ✭✭

Hi. I have been trying to change the background color of Picker Menu, DatePicker and Alert and they don't have a property to set the color. I have 2 themes (dark, light) and I change the themes dynamically. One way to change the background color of those elements is to set the theme through DependencyService on Android but this doesn't work exactly as I would like to. You can set the theme at start but when you want to change it later you have to recreate the main activity. Is there a way to change colors of those elements from Xamarin.Forms ?

Answers

  • LeonLuLeonLu Member, Xamarin Team Xamurai
    edited February 22

    One way to change the background color of those elements is to set the theme through DependencyService on Android >but this doesn't work exactly

    No, If Xamarin.Forms do not provide the change background code, you should use Xamarin.Forms Custom Renderers to change the color for your control.

    For Android, if you enable dark mode forcefully with following code in OnCreate() of MainActivity.cs, the background color of Picker Menu, DatePicker and Alert will be changed by Android OS.

      AppCompatDelegate.DefaultNightMode = AppCompatDelegate.ModeNightYes;
    
    

    Xamarin forums are migrating to a new home on Microsoft Q&A!
    We invite you to post new questions in the Xamarin forums’ new home on Microsoft Q&A!
    For more information, please refer to this sticky post.

  • CrudarCrudar Member ✭✭

    No, If Xamarin.Forms do not provide the change background code, you should use Xamarin.Forms Custom Renderers to change the color for your control.

    How exactly ? Is it possible to only change the color in the custom renderer or I have to create dialog with grid and gesture recognizer ? (If it is possible)

  • LeonLuLeonLu Member, Xamarin Team Xamurai

    Is it possible to only change the color in the custom renderer?

    No, you need to write a DatePickerDialog, because DatePicker combine with Edittext and DatePickerDialog in android, you need to create a new DatePickerDialog then set the backgroundcolor. Here is a custom renderer for TimePickerRenderer, there are similar.

    https://stackoverflow.com/questions/32314484/xamarin-forms-timepicker-isvisible-property-vs-custom-renderer

Sign In or Register to comment.