locked
How to change button location in Relative Layout in Xamarin Forms RRS feed

  • Question

  • User395357 posted

    So I've been stuck on this for a while and i can't seem to figure it out.

    I'm using Relative Layout in Xamarin Forms and I want to change the location of the button on a clicked event in the C# File

    Here's the Xaml file with the button ```

    Tuesday, June 23, 2020 10:40 PM

Answers

  • User382871 posted

    im trying to change the XConstraint and XConstraint from the C# file and i have no clue how to do it. To change the position of a view in the RelativeLayout programmatically, you could use the RelativeLayout.SetYConstraint and RelativeLayout.SetXConstraint method to reset the values.

    Check the code: ``` private void Answer_Clicked(object sender, EventArgs e) { RelativeLayout.SetYConstraint(Answer, Constraint.RelativeToParent((parent) => { return (parent.Height * 0.5) - 20; }));

    RelativeLayout.SetXConstraint(Answer, Constraint.RelativeToParent((parent) =>
    {
        return (parent.Width * 0.5) - 5;
    }));
    

    } ```

    Tutorials: https://docs.microsoft.com/en-us/dotnet/api/xamarin.forms.relativelayout.setxconstraint?view=xamarin-forms https://docs.microsoft.com/en-us/dotnet/api/xamarin.forms.relativelayout.setyconstraint?view=xamarin-forms

    Similar issue: https://forums.xamarin.com/discussion/22675/set-relativelayout-constraint-expression-from-code

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Wednesday, June 24, 2020 6:26 AM

All replies

  • User382871 posted

    im trying to change the XConstraint and XConstraint from the C# file and i have no clue how to do it. To change the position of a view in the RelativeLayout programmatically, you could use the RelativeLayout.SetYConstraint and RelativeLayout.SetXConstraint method to reset the values.

    Check the code: ``` private void Answer_Clicked(object sender, EventArgs e) { RelativeLayout.SetYConstraint(Answer, Constraint.RelativeToParent((parent) => { return (parent.Height * 0.5) - 20; }));

    RelativeLayout.SetXConstraint(Answer, Constraint.RelativeToParent((parent) =>
    {
        return (parent.Width * 0.5) - 5;
    }));
    

    } ```

    Tutorials: https://docs.microsoft.com/en-us/dotnet/api/xamarin.forms.relativelayout.setxconstraint?view=xamarin-forms https://docs.microsoft.com/en-us/dotnet/api/xamarin.forms.relativelayout.setyconstraint?view=xamarin-forms

    Similar issue: https://forums.xamarin.com/discussion/22675/set-relativelayout-constraint-expression-from-code

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Wednesday, June 24, 2020 6:26 AM
  • User395357 posted

    Thank you so much Jarvan! I've been stressing with this issue for a while. I really appreciate that, you made my day!

    Wednesday, June 24, 2020 3:46 PM