Search code examples
c#androidxamarinlayoutcenter

Xamarin C#: Trying to center button horizontally causes it to go off screen


Basically I'm trying to place a button using percentage of screen. Percentage size and percentage location. The button is placed at X: 0.5f and Y: 0.0f meaning it should be centered at the top of the screen.

Button button = new Button(Application.Context);
int height = Convert.ToInt32(Application.Context.Resources.DisplayMetrics.HeightPixels * heightPercent);
int width = Convert.ToInt32(Application.Context.Resources.DisplayMetrics.WidthPixels * widthPercent);

RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WrapContent, RelativeLayout.LayoutParams.WrapContent)
{
    LeftMargin = Convert.ToInt32(Application.Context.Resources.DisplayMetrics.WidthPixels * xPercent - (width / 2)),
    TopMargin = Convert.ToInt32(Application.Context.Resources.DisplayMetrics.HeightPixels * yPercent),
    BottomMargin = Convert.ToInt32(Application.Context.Resources.DisplayMetrics.HeightPixels * yPercent) + height,
    RightMargin = Convert.ToInt32(Application.Context.Resources.DisplayMetrics.WidthPixels * xPercent + (width / 2)),
    Width = width,
    Height = height
};


button.Tag = buttonID;
button.LayoutParameters = params1;

The code above causes the button to go off screen even though that makes no sense. If I change the margins to this;

LeftMargin = Convert.ToInt32(Application.Context.Resources.DisplayMetrics.WidthPixels * xPercent),
TopMargin = Convert.ToInt32(Application.Context.Resources.DisplayMetrics.HeightPixels * yPercent),
BottomMargin = Convert.ToInt32(Application.Context.Resources.DisplayMetrics.HeightPixels * yPercent) + height,
RightMargin = Convert.ToInt32(Application.Context.Resources.DisplayMetrics.WidthPixels * xPercent) + width,

The button will appear on screen but be slightly offset to the right. I'm trying to move it to the left by half of it's size but I have no idea why the layout freaks out. Any hints?

How the button gets added to the layout:

int MaxWidth = Resources.DisplayMetrics.WidthPixels;
int MaxHeight = Resources.DisplayMetrics.HeightPixels;

base.OnCreate(bundle);
RelativeLayout layout = new RelativeLayout(this);

layout.SetMinimumHeight(MaxHeight);
layout.SetMinimumWidth(MaxWidth);

<BUTTON GETS CREATED HERE>

layout.AddView(button);
SetContentView(layout);

Solution

  • Xamarin C#: Trying to center button horizontally causes it to go off screen

    You could use LayoutRules.CenterHorizontal to implement this feature, for example :

    RelativeLayout layout = new RelativeLayout(this);
    Button button = new Button(this);
    button.Text = "Center Button";
    
    RelativeLayout.LayoutParams parametros = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WrapContent, RelativeLayout.LayoutParams.WrapContent);
    parametros.AddRule(LayoutRules.CenterHorizontal);
    layout.AddView(button, parametros);
    
    SetContentView(layout);
    

    Effect.