Align android layout with equal margin

2.6k views Asked by At

I am adding a footer to my app which will contain three buttons with image. I want similar to below view. I have tried android:layout_gravity="right/left/center" but seems not working.

enter image description here

Below is my XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<!-- Root element should wrap to parent size. -->

<!-- Your view xml codes. -->

<!--Bottom bar layout should be in root element. Parent should be Relative layout so that we can always align to parent bottom-->
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:weightSum="4">

<LinearLayout
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:layout_gravity="left"
    android:orientation="vertical"
    >


    <ImageView android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/brands"

        />

    <TextView android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="brands"

        />


</LinearLayout>


<LinearLayout
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:orientation="vertical"
    android:layout_gravity="center_horizontal"
    >


    <ImageView android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/stores"

        />

    <TextView android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="brands"

        />


</LinearLayout>

<LinearLayout
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_weight="1"
    android:layout_gravity="right"
    >


    <ImageView android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/coupons"

        />

    <TextView android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="brands"

        />


</LinearLayout>
</LinearLayout>


</RelativeLayout>
6

There are 6 answers

1
Harshad Pansuriya On

Your parent LinearLayouthave android:weightSum="4" and your child LinearLayout all sum is 3.

so change your parent LinearLayout weightSum android:weightSum="4" to android:weightSum="3".

Update :

add this way

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<!-- Root element should wrap to parent size. -->

<!-- Your view xml codes. -->

<!--Bottom bar layout should be in root element. Parent should be Relative layout so that we can always align to parent bottom-->
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:weightSum="3">

<LinearLayout
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:orientation="vertical"
     >
    <ImageView android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/brands"
        android:layout_gravity="center_horizontal"
        />
    <TextView android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="brands"
        android:layout_gravity="center_horizontal"
        />
</LinearLayout>
<LinearLayout
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:orientation="vertical"
   >
    <ImageView android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/stores"
        android:layout_gravity="center_horizontal"
        />
    <TextView android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="brands"
        android:layout_gravity="center_horizontal"
        />
</LinearLayout>

<LinearLayout
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_weight="1">
 <ImageView android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/coupons"
        android:layout_gravity="center_horizontal"
        />
    <TextView android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="brands" 
        android:layout_gravity="center_horizontal"/>
      </LinearLayout>
   </LinearLayout>
</RelativeLayout>
0
Jinesh Francis On

Give equal weight to layouts

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:orientation="horizontal"
    android:layout_height="match_parent">
    <LinearLayout
        android:layout_width="0dp"
        android:layout_weight="1"
        android:gravity="center"
        android:orientation="vertical"
        android:layout_height="wrap_content">
        <ImageView
            android:layout_width="24dp"
            android:src="@android:drawable/ic_menu_rotate"
            android:layout_height="24dp" />
        <TextView
            android:layout_width="wrap_content"
            android:text="@string/description"
            android:layout_height="wrap_content" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="0dp"
        android:layout_weight="1"
        android:gravity="center"
        android:orientation="vertical"
        android:layout_height="wrap_content">
        <ImageView
            android:layout_width="24dp"
            android:src="@android:drawable/ic_menu_rotate"
            android:layout_height="24dp" />
        <TextView
            android:layout_width="wrap_content"
            android:text="@string/description"
            android:layout_height="wrap_content" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="0dp"
        android:layout_weight="1"
        android:gravity="center"
        android:orientation="vertical"
        android:layout_height="wrap_content">
        <ImageView
            android:layout_width="24dp"
            android:src="@android:drawable/ic_menu_rotate"
            android:layout_height="24dp" />
        <TextView
            android:layout_width="wrap_content"
            android:text="@string/description"
            android:layout_height="wrap_content" />
    </LinearLayout>
</LinearLayout>
4
Siddhesh Dighe On
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    >

<LinearLayout
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:orientation="vertical">


<ImageView android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/brands"
/>

  <TextView android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="brands"
/>

</LinearLayout>


<LinearLayout
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:orientation="vertical">


<ImageView android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/shops"
/>

  <TextView android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="shops"
/>

</LinearLayout>


<LinearLayout
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:orientation="vertical">


<ImageView android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/coupons"
/>

  <TextView android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="coupons"
/>

</LinearLayout>
</LinearLayout>

Try Something like this,

LinearLayout(Horizontal)
  - LinearLayout(Vertical)
        - ImageView (for the image)
        - Textview (for title)
  - LinearLayout(Vertical)
        - ImageView (for the image)
        - Textview (for title)
  - LinearLayout(Vertical)
        - ImageView (for the image)
        - Textview (for title)
1
Appy Shah On

Please try this

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:weightSum="3">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:gravity="center"
            android:orientation="vertical"
            android:padding="5dp">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/brands" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="brands" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:gravity="center"
            android:orientation="vertical"
            android:padding="5dp">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/stores" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Stores" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:gravity="center"
            android:orientation="vertical"
            android:padding="5dp">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/coupons" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Coupons" />
        </LinearLayout>
    </LinearLayout>


</RelativeLayout>
0
Uma Achanta On

change all inner layouts

android:layout_gravity="left"

android:layout_margin="10dp"//as required

and change

android:weightSum="3"

in parent layout

1
Sneha Sarkar On
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- Root element should wrap to parent size. -->

<!-- Your view xml codes. -->

<!--Bottom bar layout should be in root element. Parent should be Relative layout so that we can always align to parent bottom-->
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:weightSum="3">

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:orientation="vertical">


        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/brands" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="brands" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:orientation="vertical">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/stores" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="brands" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:orientation="vertical">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/coupons" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="brands" />

    </LinearLayout>
</LinearLayout>

Try this. I think it will solve your problem.