Search code examples
android-linearlayoutandroid-relativelayout

Unable to align image buttons inside relative and linear layout


I want to have 3 image buttons in first row and 3 image buttons in second row. I tried implementing it with a combination of relative and linear layout but, somehow I am ending up with 1 image button in each row, and total of 6 rows.

Can someone please help me in aligning the 3 image boxes in first row and 3 boxes in second row.

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="app.com.date.design.drawer.AddImagesFragment">

    <!-- TODO: Update blank fragment layout -->

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/layout1"
            android:baselineAligned="false"
            android:orientation="vertical">

            <ImageButton
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:srcCompat="@drawable/common_full_open_on_phone"
                android:id="@+id/image1" />

            <ImageButton
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:srcCompat="@drawable/common_full_open_on_phone"
                android:id="@+id/image2" />

            <ImageButton
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:srcCompat="@drawable/common_full_open_on_phone"
                android:id="@+id/image3" />

            <ImageButton
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:srcCompat="@drawable/common_full_open_on_phone"
                android:id="@+id/image4" />

            <ImageButton
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:srcCompat="@drawable/common_full_open_on_phone"
                android:id="@+id/image5" />

            <ImageButton
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:srcCompat="@drawable/common_full_open_on_phone"
                android:id="@+id/image6"
                android:layout_weight="0.05" />
        </LinearLayout>

    </RelativeLayout>

</FrameLayout>

Solution

  • Try This Code,

    activity_main.xml :

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/activity_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin">
    
        <LinearLayout
            android:id="@+id/lin1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:weightSum="3">
    
            <ImageButton
                android:id="@+id/image1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                app:srcCompat="@drawable/star2" />
    
            <ImageButton
                android:id="@+id/image2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                app:srcCompat="@drawable/star1" />
    
            <ImageButton
                android:id="@+id/image3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                app:srcCompat="@drawable/star3" />
    
        </LinearLayout>
    
        <LinearLayout
            android:id="@+id/lin2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/lin1"
            android:orientation="horizontal"
            android:weightSum="3">
    
            <ImageButton
                android:id="@+id/image4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                app:srcCompat="@drawable/star2" />
    
            <ImageButton
                android:id="@+id/image5"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                app:srcCompat="@drawable/star1" />
    
            <ImageButton
                android:id="@+id/image6"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                app:srcCompat="@drawable/star3" />
    
        </LinearLayout>
    
    
    </RelativeLayout>
    

    Hereby, i m attaching Screenshot,

    enter image description here

    Hope it will help you.