Bonucci
Bonucci

Reputation: 51

How to set vertical LinearLayout layout_gravity to center_horizontal programatically? (Xamarin.Android)

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

Upvotes: 0

Views: 408

Answers (1)

JoeTomks
JoeTomks

Reputation: 3276

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>

Upvotes: 1

Related Questions