dotnet / dotnet/android-libraries

Unable to change width of Android's 'PayButton'

Open
#1,138 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
C#
Stars
317
Forks
73
Avg merge
1d 8h
Merged PRs (30d)
17

Description

### Android framework version

net9.0-android

### Affected platform version

.NET 9.0.103, VS 2022 17.12.5, Xamarin.GooglePlayServices.Wallet 119.4.0.4

### Description

I'm attempting to inject the native Google Pay button into our MAUI system using Handlers with a custom control. Albeit I got it working functionally, the width of the button doesn't respect the parent's width.

I have tried multiple means of changing the width.

From changing `HorizontalOptions` on the control itself:
```xml

```
To modifying the handler's return to include `LayoutParameters` to specifically match the parent layout:
```csharp
var payButton = new PayButton(context)
{
LayoutParameters = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.MatchParent,
ViewGroup.LayoutParams.WrapContent)
{
Gravity = GravityFlags.CenterHorizontal
}
};
```
I've even looked at using `Android.Views.View.Post` to attempt to realign these after rendering:
```csharp
payButton.Post(() =>
{
// Ensure the parent layout is measured
var parent = payButton.Parent as ViewGroup;
if (parent != null && parent.MeasuredWidth > 0)
{
payButton.LayoutParameters.Width = parent.MeasuredWidth;
}
else
{
payButton.LayoutParameters.Width = ViewGroup.LayoutParams.MatchParent;
}
payButton.RequestLayout();
});
```

This still doesn't work, and no matter what I do it always sets the PayButton's width to 300. Attached a minimal example in reproduction steps (this example is using just the `LayoutParameters` method).

### Steps to Reproduce

1. Download ZIP file attached
2. Extract
3. Run on Android (iOS has not been implemented).
[MauiDigitalWallets.zip](https://github.com/user-attachments/files/20084651/MauiDigitalWallets.zip)

### Did you find any workaround?

_No response_

### Relevant log output

```shell

```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.