Search code examples
androidandroid-layoutalignmentandroid-linearlayout

Linear Layout Horizontal, Gravity Problems


I've placed three buttons as below,

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="8dp"
        android:layout_marginBottom="8dp"
        android:layout_gravity="center">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Standard"
            android:id="@+id/standard_map"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="satellite"
            android:id="@+id/satellite"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hybrid"
            android:id="@+id/hybrid"/>
    </LinearLayout>

    <fragment
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:name="com.google.android.gms.maps.MapFragment"
        map:cameraTargetLat="13.0827"
        map:cameraTargetLng="80.2707"
        map:cameraBearing="112.5"
        map:cameraTilt="65"
        map:cameraZoom="20"/>

</LinearLayout>

The output yield is as below: image explaining the problem

The orientation of the inner Linear Layout is horizontal and that's perfectly fine. I need the buttons to be aligned in centre (Vertically centred). Even when I use android:gravity="centre", it aligns the buttons horizontally centred which is of no use. I need the buttons to be vertically centred in an Horizontal Linear Layout.


Solution

  • As far as I understand your Question, you want to make middle button in center, so I have done some changes, please update your code.

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:map="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
    
            <Button
                android:id="@+id/standard_map"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Standard" 
                android:layout_weight="1"/>
    
            <Button
                android:id="@+id/satellite"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="satellite"
                android:layout_weight="1"
                />
    
            <Button
                android:id="@+id/hybrid"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Hybrid"
                android:layout_weight="1"/>
        </LinearLayout>
    
        <fragment
            android:id="@+id/map"
            android:name="com.google.android.gms.maps.MapFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            map:cameraBearing="112.5"
            map:cameraTargetLat="13.0827"
            map:cameraTargetLng="80.2707"
            map:cameraTilt="65"
            map:cameraZoom="20" />
    
    </LinearLayout>
    

    Please let me know if any problem with this.