Skip to content

[Android] Added Platform specific ripple color#23599

Merged
rmarinho merged 6 commits intodotnet:net10.0from
kubaflo:platform-specific-ripple-color
Apr 29, 2025
Merged

[Android] Added Platform specific ripple color#23599
rmarinho merged 6 commits intodotnet:net10.0from
kubaflo:platform-specific-ripple-color

Conversation

@kubaflo
Copy link
Contributor

@kubaflo kubaflo commented Jul 13, 2024

Description of Change

Right now the only way to set a ripple color for buttons is to use custom mappers or platform specific code. But, for image buttons there is currently no way to do anything.

Public API Changes

<Button 
   xmlns:android="clr-namespace:Microsoft.Maui.Controls.PlatformConfiguration.AndroidSpecific;assembly=Microsoft.Maui.Controls"
   android:Button.RippleColor="Green"/>
<ImageButton 
   xmlns:android="clr-namespace:Microsoft.Maui.Controls.PlatformConfiguration.AndroidSpecific;assembly=Microsoft.Maui.Controls"
   android:ImageButton.RippleColor="Green"/>

Fixes #22805

Screen.Recording.2024-07-13.at.19.17.11.mov

Notes

I will remove the pragma warning disable RS0016 if this PR makes sense

@dotnet-policy-service dotnet-policy-service bot added the community ✨ Community Contribution label Jul 13, 2024
@kubaflo kubaflo force-pushed the platform-specific-ripple-color branch from cd2974d to d908f2c Compare July 13, 2024 17:44
@kubaflo kubaflo changed the title [Android] Added Platform specific ripple color [net9.0][Enhancement][Android] Added Platform specific ripple color Jul 14, 2024
@kubaflo kubaflo marked this pull request as ready for review September 7, 2024 10:58
@kubaflo kubaflo requested a review from a team as a code owner September 7, 2024 10:58
@kubaflo kubaflo changed the base branch from net9.0 to main October 18, 2024 23:08
@azure-pipelines

This comment was marked as outdated.

@jsuarezruiz

This comment was marked as off-topic.

@jfversluis
Copy link
Member

@kubaflo could you rebase this one please? Thanks!

@kubaflo kubaflo force-pushed the platform-specific-ripple-color branch from 2afa9af to 24c62fb Compare December 6, 2024 18:59
@azure-pipelines
Copy link

Azure Pipelines successfully started running 3 pipeline(s).

Copy link
Member

@mattleibow mattleibow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unless someone does not like this feature, I think it is great.

I added a few comments, but it seems like all the bits are there. This will need to all be internal and then made public in net10 or in a SR if we are allowed to.

Alos, no idea how to test this because it is an OS animation... Msybe a device test that sets the ripple then background and/or border (and in reverse) and just read the derawable.

@mattleibow
Copy link
Member

I wonder if this should be targeted at net10.0 instead?

@kubaflo
Copy link
Contributor Author

kubaflo commented Feb 2, 2025

I wonder if this should be targeted at net10.0 instead?

Hmmm that's a good question

@kubaflo kubaflo force-pushed the platform-specific-ripple-color branch from 24c62fb to 402d27c Compare February 3, 2025 11:29
@kubaflo
Copy link
Contributor Author

kubaflo commented Feb 3, 2025

@mattleibow I think I've resolved comments :)

@kubaflo kubaflo force-pushed the platform-specific-ripple-color branch from 402d27c to ee9df74 Compare February 3, 2025 11:45
@kubaflo kubaflo changed the title [net9.0][Enhancement][Android] Added Platform specific ripple color [Android] Added Platform specific ripple color Mar 12, 2025
@kubaflo kubaflo force-pushed the platform-specific-ripple-color branch from ee9df74 to d2a109e Compare March 12, 2025 14:16
@kubaflo kubaflo self-assigned this Mar 12, 2025
@azure-pipelines

This comment was marked as off-topic.

@jfversluis jfversluis self-assigned this Apr 7, 2025
@jfversluis jfversluis added this to the .NET 10.0-preview4 milestone Apr 7, 2025
@kubaflo kubaflo force-pushed the platform-specific-ripple-color branch from d2a109e to 9837377 Compare April 7, 2025 16:16
@kubaflo kubaflo force-pushed the platform-specific-ripple-color branch from c4f77f4 to be91cea Compare April 11, 2025 00:26
@azure-pipelines
Copy link

Azure Pipelines successfully started running 3 pipeline(s).

Copy link
Contributor

@jsuarezruiz jsuarezruiz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could include some tests in the DeviceTest project just using the Platform Specific and checking the RippleDrawable color?

@kubaflo
Copy link
Contributor Author

kubaflo commented Apr 14, 2025

Could include some tests in the DeviceTest project just using the Platform Specific and checking the RippleDrawable color?

I'm not quite sure how to do it

@jsuarezruiz
Copy link
Contributor

Could include some tests in the DeviceTest project just using the Platform Specific and checking the RippleDrawable color?

I'm not quite sure how to do it

You need to add a test in this class

Something like:

[Fact]
[Description("The RippleColor property of a Button Platform Specific should match with native RippleDrawable Color")]
public async Task RippleColorPlatformSpecific()

Define the button with the added Platform Specific:

var expected = Colors.Red;

var button = new Button();
button.On<Controls.PlatformConfiguration.Android>().SetRippleColor(expected);

And then, create a method to get the RippleDrawable Color, to compare it using an Assert.Equal. To get the color, will require to use reflection:

int? GetRippleDrawableColor(Android.Views.View button)
{
	if (button.Background is RippleDrawable rippleDrawable)
	{
		var constantState = rippleDrawable.GetConstantState();

		if (constantState is not null)
		{
			var colorField = constantState.GetType().GetField("mColor", BindingFlags.NonPublic | BindingFlags.Instance);

			if (colorField is not null)
			{
				var colorStateList = colorField.GetValue(constantState) as ColorStateList;
				return colorStateList?.DefaultColor;
			}
		}
	}

	return null;
}

Finally, just launch the test runner App on an Android device our emulator and run the test.

Ok, I let you explore to add it, but just let me know if need help or if want myself adding some tests.

@kubaflo kubaflo force-pushed the platform-specific-ripple-color branch from be91cea to ebae55e Compare April 16, 2025 18:17
@kubaflo
Copy link
Contributor Author

kubaflo commented Apr 16, 2025

@jsuarezruiz I've added a test, but I had to modify your solution a bit because this always returned null:
var colorField = constantState.GetType().GetField("mColor", BindingFlags.NonPublic | BindingFlags.Instance);

rmarinho
rmarinho previously approved these changes Apr 21, 2025
Copy link
Contributor

@jsuarezruiz jsuarezruiz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Requires some small changes to fix the build:

C:\a\_work\1\s\src\Core\src\Platform\Android\ImageButtonExtensions.cs(87,43): error CS0234: The type or namespace name 'Graphics' does not exist in the namespace 'Microsoft.Android' (are you missing an assembly reference?) [C:\a\_work\1\s\src\Core\src\Core.csproj::TargetFramework=net10.0-android36.0]
C:\a\_work\1\s\src\Core\src\Platform\Android\ImageButtonExtensions.cs(91,22): error CS0234: The type or namespace name 'Content' does not exist in the namespace 'Microsoft.Android' (are you missing an assembly reference?) [C:\a\_work\1\s\src\Core\src\Core.csproj::TargetFramework=net10.0-android36.0]
C:\a\_work\1\s\src\Core\src\Platform\Android\ButtonExtensions.cs(94,43): error CS0234: The type or namespace name 'Graphics' does not exist in the namespace 'Microsoft.Android' (are you missing an assembly reference?) [C:\a\_work\1\s\src\Core\src\Core.csproj::TargetFramework=net10.0-android36.0]
C:\a\_work\1\s\src\Core\src\Platform\Android\ButtonExtensions.cs(98,22): error CS0234: The type or namespace name 'Content' does not exist in the namespace 'Microsoft.Android' (are you missing an assembly reference?) [C:\a\_work\1\s\src\Core\src\Core.csproj::TargetFramework=net10.0-android36.0]
    4 Error(s)

@rmarinho
Copy link
Member

/azp run

@dotnet dotnet deleted a comment from azure-pipelines bot Apr 22, 2025
@azure-pipelines
Copy link

Azure Pipelines successfully started running 3 pipeline(s).

@dotnet dotnet deleted a comment from azure-pipelines bot Apr 22, 2025
@dotnet dotnet deleted a comment from azure-pipelines bot Apr 22, 2025
@rmarinho rmarinho requested a review from jsuarezruiz April 23, 2025 10:23
Copy link
Contributor

@jsuarezruiz jsuarezruiz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, just added a comment about a detail in the added device test.

if (platformButton.Background is global::Android.Graphics.Drawables.RippleDrawable rippleDrawable)
{
var constantState = rippleDrawable.GetConstantState();
if (constantState is not null)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here can just check constantState:

Assert.NotNull(constantState);

@rmarinho rmarinho merged commit aa62228 into dotnet:net10.0 Apr 29, 2025
127 of 129 checks passed
@github-actions github-actions bot locked and limited conversation to collaborators May 30, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-controls-button Button, ImageButton community ✨ Community Contribution proposal/open

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add a platform specific for ripple color

5 participants