0

I'd like to center the following LinearLayout to the center of his parent programatically:

<LinearLayout
    android:id="@+id/SignButtonsLinearLayout"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <homes.shared.components.customviews.HomesButton
        android:id="@+id/CheckListPDFClosureButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="3dip"
        android:drawableLeft="@drawable/CheckListPDFClosure"
        android:background="@drawable/MainBlueButton"
        style="@style/WhiteDeliveryNoteMenuBold"
        android:text="@string/CheckListPDFClosure" />
   <homes.shared.components.customviews.HomesButton
        android:id="@+id/SignatureAndCloseButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:drawableLeft="@drawable/SignatureAndClosure"
        android:background="@drawable/MainBlueButton"
        style="@style/WhiteDeliveryNoteMenuBold"
        android:text="@string/SignatureAndClose" /> 
</LinearLayout> 

I have instanced the LinearLayout in code like this:

LinearLayout ll = this.FindViewById<LinearLayout>(Resource.Id.SignButtonsLinearLayout);

but the object doesn't have LayoutGravity property

Bonucci
  • 51
  • 7

1 Answers1

1

You can do it quick hand like this:

LinearLayout ll = this.FindViewById<LinearLayout>(Resource.Id.SignButtonsLinearLayout);
ll.SetGravity(GravityFlags.Center);

Or you can do it this way too:

LinearLayout ll = this.FindViewById<LinearLayout>(Resource.Id.SignButtonsLinearLayout);
LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams)lin.LayoutParameters;
layoutParams.Gravity = GravityFlags.Center;
ll.LayoutParameters = layoutParams;

Some more info can be found in this SO reply here as well.

EDIT:

Apologies, to adjust the layout gravity which sets the gravity of the View or Layout relative to its parent then you should just be able to add this attribute to your axml layout file. android:layout_gravity="center" like this:

<LinearLayout
    android:id="@+id/SignButtonsLinearLayout"
    android:layout_gravity="center"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

The other option is to stack layouts, so you could wrap your linear layout in a relative layout like this and use center in parent.

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:focusableInTouchMode="true"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
        <LinearLayout
        android:id="@+id/SignButtonsLinearLayout"
        android:layout_centerInParent="true"
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    </LinearLayout>
</RelativeLayout>
JoeTomks
  • 3,243
  • 1
  • 18
  • 42
  • But `ll.SetGravity(GravityFlags.Center)` makes the elements within the `LinearLayout` to be centered, and what I want is to center the `LinearLayout` which is in his parent. It is made with the attribute `layout_gravity` in the `.axml` file. – Bonucci Jun 06 '19 at 12:43
  • @Bonucci then you adjust the gravity of whatever the top level container is that contains the control / element that you want to center. Are you saying you want to center 'SignButtonsLinearLayout' inside the parent view? – JoeTomks Jun 06 '19 at 13:04
  • Yes. Is it possible if the parent of `SignButtonsLinearLayout` is a `LinearLayout` to center only its `SignButtonsLinearLayout` child? – Bonucci Jun 06 '19 at 13:14
  • @Bonucci just edited my answer, you can set the layout gravity through a property of the linear layout in your axml file. – JoeTomks Jun 06 '19 at 13:40
  • I want to do it programatically. Is it possible when its parent is a Linear Layout? – Bonucci Jun 06 '19 at 13:49
  • @Bonucci not easily, because you need the layout params of the viewgroup that your layout file gets rendered into. Can I ask why you need to do it programatically? – JoeTomks Jun 06 '19 at 14:06
  • @Bonucci saying that, adjusting the layout params of your linear layout as I show above should work as at that point Gravity refers to the linear layout not the children. – JoeTomks Jun 06 '19 at 14:11