Button auto width

SSB 71 Reputation points


How can i set the width of a Xamarin Forms button to auto-fit the container it is in?

as opposed to setting an actual number?


A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
4,819 questions
{count} votes

1 answer

Sort by: Most helpful
  1. JessieZhang-MSFT 7,516 Reputation points


    Welcome to our Microsoft Q&A platform!

    You can try to set propeties HorizontalOptions and VerticalOptions of this button to FillAndExpand .

    And set the parent's Padding to 0.

    You can refer to the following code:

        <StackLayout   BackgroundColor="Yellow" WidthRequest="300" HeightRequest="50" HorizontalOptions="Center" VerticalOptions="Center" Padding="5">  
            <Button Text="Example"   BackgroundColor="Pink" HorizontalOptions="FillAndExpand"  VerticalOptions="CenterAndExpand"/>  

    Note: As a test, I assign the WidthRequest and HeightRequest to an actual number, and if we want the inner button to auto-fit the container , we can just set using the following code:

     HorizontalOptions="FillAndExpand"  VerticalOptions="CenterAndExpand"  

    Best Regards,

    Jessie Zhang

    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.