maghfirzakir
maghfirzakir

Reputation: 848

centering layout android same height

I can't upload image via imgur, so I will post link via drive:

https://drive.google.com/file/d/0B9Ho1o8QjLVaLWsyNmxnQTV1alU/view?usp=sharing

here's the xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <LinearLayout
            android:id="@+id/lay"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <ImageView
                android:id="@+id/imgView"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:src="@mipmap/ic_launcher"
                android:layout_weight="1"/>

            <ImageView
                android:id="@+id/imgView2"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:src="@mipmap/ic_launcher"
                android:layout_weight="1"/>
        </LinearLayout>
        <LinearLayout
            android:id="@+id/lay2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/lay"
            android:orientation="horizontal">

            <ImageView
                android:id="@+id/imgView3"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:src="@mipmap/ic_launcher"
                android:layout_weight="1"/>

            <ImageView
                android:id="@+id/imgView4"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:src="@mipmap/ic_launcher"
                android:layout_weight="1"/>
        </LinearLayout>

    </LinearLayout>

    <Button
        android:id="@+id/buttonLoadPicture"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Load Picture"
        android:layout_centerInParent="true"
        android:layout_alignParentBottom="true"/>
</RelativeLayout>

what I need is to make the layout center, like this:

https://drive.google.com/file/d/0B9Ho1o8QjLVaWDVjWk02ckVWeEU/view?usp=sharing

please help, thanks in advance!

Upvotes: 0

Views: 58

Answers (2)

Saeed Zhiany
Saeed Zhiany

Reputation: 2141

you must add weightSum property to LinearLayout and set 0dp for android:layout_width for image views, like this:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:weightSum="2">
        <LinearLayout
            android:id="@+id/lay"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:weightSum="2"
            android:orientation="horizontal">

            <ImageView
                android:id="@+id/imgView"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:src="@mipmap/ic_launcher"
                android:layout_weight="1"/>

            <ImageView
                android:id="@+id/imgView2"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:src="@mipmap/ic_launcher"
                android:layout_weight="1"/>
        </LinearLayout>
        <LinearLayout
            android:id="@+id/lay2"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_below="@+id/lay"
            android:orientation="horizontal"
            android:layout_weight="1"
            android:weightSum="2">

            <ImageView
                android:id="@+id/imgView3"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:src="@mipmap/ic_launcher"
                android:layout_weight="1"/>

            <ImageView
                android:id="@+id/imgView4"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:src="@mipmap/ic_launcher"
                android:layout_weight="1"/>
        </LinearLayout>

    </LinearLayout>

    <Button
        android:id="@+id/buttonLoadPicture"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Load Picture"
        android:layout_centerInParent="true"
        android:layout_alignParentBottom="true"/>
</RelativeLayout>

Upvotes: 1

Suhyeon Lee
Suhyeon Lee

Reputation: 569

You should use weight to your vertical layouts too.

Try this code.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <LinearLayout
            android:id="@+id/lay"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:orientation="horizontal">

            <ImageView
                android:id="@+id/imgView"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:src="@mipmap/ic_launcher"
                android:layout_weight="1"/>

            <ImageView
                android:id="@+id/imgView2"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:src="@mipmap/ic_launcher"
                android:layout_weight="1"/>
        </LinearLayout>
        <LinearLayout
            android:id="@+id/lay2"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_below="@+id/lay"
            android:orientation="horizontal"
            android:layout_weight="1">

            <ImageView
                android:id="@+id/imgView3"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:src="@mipmap/ic_launcher"
                android:layout_weight="1"/>

            <ImageView
                android:id="@+id/imgView4"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:src="@mipmap/ic_launcher"
                android:layout_weight="1"/>
        </LinearLayout>

    </LinearLayout>

    <Button
        android:id="@+id/buttonLoadPicture"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Load Picture"
        android:layout_centerInParent="true"
        android:layout_alignParentBottom="true"/>
</RelativeLayout>

Upvotes: 1

Related Questions