Alan2
Alan2

Reputation: 24572

How can I set the padding on a Xamarin Forms Android button?

I tried to set the padding:

public class GP10Button : Button
{
    // Grid Small Button
    public GP10Button()
    {
        Padding = new Thickness(10, 0, 10, 0);
    }
}

but it seems to be ignored. Then I tried to follow these instructions:

https://learn.microsoft.com/en-us/xamarin/xamarin-forms/platform/android/button-padding-shadow

And it seems like it's not possible for me to set the default off.

Has anyone found a way to implement padding on the Android button that actually works. I found a lot of suggestions out there but none seem to work.

Upvotes: 0

Views: 953

Answers (2)

Nikhil
Nikhil

Reputation: 3387

You can manipulate the space available to the button. You can use a Grid and have as many columns needed and then in the code behind file access the GridColumns by setting x:name and use the below code in the constructor of the Page. This may help.

    if (Device.RuntimePlatform == Device.Android)
    {
        GColumn1.Width = new GridLength(1, GridUnitType.Star);
        GColumn2.Width = new GridLength(1, GridUnitType.Star);
        GColumn3.Width = new GridLength(1, GridUnitType.Star);
        GColumn4.Width = new GridLength(1, GridUnitType.Star);
        GColumn5.Width = new GridLength(1, GridUnitType.Star);
    }

Upvotes: 1

Leo Zhu
Leo Zhu

Reputation: 14956

Whether the padding is not visible because your button is too wide,but it actually works,you could try to reduce the wide and hava a look.

class GP10Button:Button
{

    public GP10Button()
    {
        switch (Device.RuntimePlatform)
        {
            case Device.iOS:
                break;
            case Device.Android:
                Padding = new Thickness(10, 0, 10, 0);
                break;
        }

    }
}

in xaml:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:local="clr-namespace:App18"
         x:Class="App18.Page20">
  <ContentPage.Content>
    <StackLayout>
        <local:GP10Button Text="Welcome" HorizontalOptions="Start" WidthRequest="60" HeightRequest="50"
      />
        <Button Text="Welcome" HorizontalOptions="Start" WidthRequest="60" HeightRequest="50"/>
    </StackLayout>
  </ContentPage.Content>
</ContentPage>

the effect :

enter image description here

Upvotes: 0

Related Questions