Search code examples
androidandroid-layoutviewandroid-linearlayout

position icons within LinearLayout


I've an app where I have 2 icons within a Linearlayout. How to position the icons next to each other centrally?

enter image description here

<LinearLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     android:orientation="horizontal" >

     <Button
         android:id="@+id/buttonsignin"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_gravity="center"
         android:background="@drawable/signinbuttonmenu" />

     <Button
         android:id="@+id/buttongetrota"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_gravity="center"
         android:background="@drawable/rotabuttonmenu"/>

</LinearLayout>

Solution

  • Adding the attribute android:gravity with the value of center_horizontal (android:gravity="center_horizontal") to the parent LinearLayout will do it. Changing the center_horizontal to just center will set it right in the center both Vertically as well as Horizontally.

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center_horizontal"
        android:orientation="horizontal" >
    
        <Button
            android:id="@+id/buttonsignin"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:background="@drawable/signinbuttonmenu" />
    
        <Button
            android:id="@+id/buttongetrota"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:background="@drawable/rotabuttonmenu" />
    </LinearLayout>