maui: [regression/8.0.0-preview.4.8333] [Android] Setting the background property of AppCompatEditText in a handler mapping does not work
Description
In a .NET MAUI project using the latest .NET 8 RC, setting the background property of a AppCompatEditText doesn’t seem to work.
Using this code:
Microsoft.Maui.Handlers.EntryHandler.Mapper.AppendToMapping("TestEntry", (handler, view) =>
{
handler.PlatformView.Background = null;
handler.PlatformView.SetBackgroundColor(Android.Graphics.Color.Pink);
handler.PlatformView.SetTextColor(Android.Graphics.Color.Aqua);
});
Has the following results:
❌ Figure: PlatformView background properties not applied, but other mappings are applied
✅ Figure: The same code in .NET 7
Repro is linked below. Repo with working version in .NET 7: https://github.com/matt-goldman/Net7AndroidHandlers
Steps to Reproduce
- Create a .NET MAUI app in .NET 8
- Create a handler mapping to set the background of the
PlatformViewforEntry - Add an
Entryto your UI - Run the app, observe bug 🐞
Link to public reproduction project repository
https://github.com/matt-goldman/Net8AndroidHandlers
Version with bug
8.0.0-preview.4.8333
Is this a regression from previous behavior?
Yes, this used to work in .NET MAUI
Last version that worked well
8.0.0-preview.3.8149
Affected platforms
Android
Affected platform versions
Android - all versions
Did you find any workaround?
I could not find a workaround
Relevant log output
No response
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 8
- Comments: 34 (12 by maintainers)
Hello @samhouts, I prepared sample to show that Background property of AppCompatEditText does not apply changes. In the project you can find CustomEntryHandler.cs which change Background to round violet border and usage of CustomEntry.cs in MainPage.xaml.
https://github.com/vitalbit/AndroidEntryBackgroundChangeSample/
master branch - provides .NET 7 project, which works fine.
net8-migration branch - provides .NET 8 project, with background issue.
The workaround mentioned above provides possibility to remove underline for Android, meanwhile we still can not fully control Background natively for Android, that’s why I believe this bug should still be in Open state.
Hello @matt-goldman , it appears this is not a .NET MAUI 8/7 issue. On this post found in StackOverflow, the Android developers shared that to change the edit text line color we must use BackgroundTint, and we should not use BackgroundColor.
Honestly, I was like you, I have been using the background color to remove the line color on Android in .NET MAUI 7, but after reviewing I was able to remove the bottom line of the edit text on android in my .NET MAUI 8 projects, by using the following code line on my handler:
handler.PlatformView.BackgroundTintList = Android.Content.Res.ColorStateList.ValueOf(Colors.Transparent.ToAndroid());I updated my article on my blog, reflecting this change: So, based on the findings, I believe this is not a bug of .NET MAUI and instead we were setting wrong the code to hide the Entry form control border in Android. I just wanted to share my grain of salt @jsuarezruiz .
I hope this helps other developers!
Reference of the official Android documentation: https://developer.android.com/reference/androidx/core/view/TintableBackgroundView
Thanks in advance.
cc: @XamlTest @borrmann @ieuangriffiths97
You should be able to override the behaviour using the generics (note the use of
BorderedEntryinstead ofIEntry:With this XAML where once is a normal entry and the other is a custom one, the mapper only applies to the custom one - without requiring a new handler and new handler registration. We basically register the mapper as a smaller unit.
This all looks very complicated with the append mapper and/or custom handler. It appears you want to replace the background? We have a feature for that since net6:
This results in:
Any update from the MAUI team on when this will be fixed? This is stopping me from using .NET 8 Android in production. .NET 7 end of life is in May.
I have tested it in version 8.06 and still have this issue
@vhugogarcia, @matt-goldman
I believe there is still handler.PlatformView.Background setter, that is required to be fixed. Meanwhile we have ability to remove line for entry with Colors.Transparent approach, we still not able to update Background with any native Android.Graphics.Drawables.Drawable in .NET8.
@borrmann I expect the team will fix this pretty quickly as it will affect everyone. And probably the controls vendors too.
Sorry, on vacation, not near a dev env but if you setup the correct handler type it works for editor in iOS and Android, don’t override ConnectHandler.
https://learn.microsoft.com/en-us/dotnet/maui/user-interface/handlers/?view=net-maui-8.0#view-handlers. Use the approach recommended by @mattleibow
@pmahend1 I use this:
I never thought of consuming the API that way, your 100% right about needing more documentation on mappers, I’m guessing some of these 3.5K open issues are this type of thing, a misunderstanding of the mappers API. That’s working very nicely, thanks.
@mackayn I was able to apply it for sub-classed control Here is how I did it.
MauiProgram.cs
Created BorderedEntryHandler in Platforms/Android, namespace has to be same as of MauiProgram
I can confirm this is still happening in 8.0.70
Still dont working at 8.0.40
Nice catch @vhugogarcia! Thanks for this, and the write-up too!