Changed button text alignment after button clicked

leighleigh NZMember

Hello,

I am not sure why the button change text alignment after button clicked if the button is in a grid and there is a label and grid in StackLayout for android.

Here is a test case of the source code.

using Xamarin.Forms;

namespace ButtonTextAlignment
{
public class ContentPageGrid : ContentPage
{
public ContentPageGrid()
{
var layout = new StackLayout
{
Orientation = StackOrientation.Vertical,
Padding = 20
};

        var labelUsDollor = new Label
        {
            Text = "label",
            TextColor = Color.White,
            BackgroundColor = Color.Blue
        };
        layout.Children.Add(labelUsDollor);

        var grid = new Grid
        {
            RowSpacing = 10
        };

        var button = new Button
        {
            Text = "Click me to test",
            TextColor = Color.White,
            BackgroundColor = Color.Blue,
            WidthRequest = 400
        };

        button.Clicked += (sender, e) =>
        {
            labelUsDollor.Text = "button text change alignment";
        };

        grid.Children.Add(button, 0, 0);

        layout.Children.Add(grid);
        Content = layout;
    }
}

}

Regards
Leigh

Posts

  • AndrewBilykAndrewBilyk USMember

    I'm having the same issue. Can someone please assist us?

  • RyanHatfieldOldRyanHatfieldOld USMember, Insider ✭✭✭

    They did a TON of bug fixes, is this still an issue after the latest xamarin forms update? make sure to open nuget and update to the latest with each project

  • leighleigh NZMember
    edited July 2014

    I am using Xamarin.Forms 1.1.1.6206 and still have the problem

  • RyanHatfieldOldRyanHatfieldOld USMember, Insider ✭✭✭

    I do think you found a bug. And this really isn't a fix, but it works if you make the grid the only element in the stack layout.

    Take out the line:

    layout.Children.Add(labelUsDollor);
    

    and add

    grid.Children.Add(labelUsDollor, 0, 0);
    

    and change

    grid.Children.Add(button, 0, 0);
    

    to

    grid.Children.Add(button, 0, 1);
    

    hopefully that works for ya

    (full modified code)

    public class ContentPageGrid : ContentPage
    {
        public ContentPageGrid()
        {
            var layout = new StackLayout
            {
                Orientation = StackOrientation.Vertical,
                Padding = 20
            };
    
            var labelUsDollor = new Label
            {
                Text = "label",
                TextColor = Color.White,
                BackgroundColor = Color.Blue
            };
    
            //took this out
            //layout.Children.Add(labelUsDollor);
    
            var grid = new Grid
            {
                RowSpacing = 10
            };
    
            var button = new Button
            {
                Text = "Click me to test",
                TextColor = Color.White,
                BackgroundColor = Color.Blue,
                WidthRequest = 400
            };
    
            button.Clicked += (sender, e) =>
            {
                labelUsDollor.Text = "button text change alignment";
            };
    
            //add this
            grid.Children.Add(labelUsDollor, 0, 0);
            //replace this
            //grid.Children.Add(button, 0, 0);
            //with this
            grid.Children.Add(button, 0, 1);
    
            layout.Children.Add(grid);
    
            Content = layout;
        }
    
    }
    
  • leighleigh NZMember

    Hi Ryan,

    Thanks

    I am using the method you posted for the last two weeks. :-)

    Regards
    Leigh

  • PhilipParkerPhilipParker USMember

    I've posted a fix for this button layout problem in the following thread:

    http://forums.xamarin.com/discussion/20608/fix-for-button-layout-bug-on-android

    Hope it helps

Sign In or Register to comment.