Ghimire
Ghimire

Reputation: 958

Setting Image Exactly in Center

I have divided Screen in 4 horizontal and two vertical portion.I am using Linear Layout to display the ImageView and TextView.I am not able to set the imageView exactly in center of the Linear layout.

home_layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">


    <include layout="@layout/appbar_layout" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:background="#156C7F"
        android:orientation="horizontal">


        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginLeft="125dp"
            android:text="Branch Office : Dhankuta"
            android:textColor="#FFFFFF"
            android:textSize="10sp" />

        <View
            android:layout_width="1dp"
            android:layout_height="15dp"
            android:layout_gravity="center"
            android:layout_marginLeft="7dp"
            android:background="#FFFFFF" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginLeft="7dp"
            android:text="Tuesday - 4 April, 2017"
            android:textColor="#FFFFFF"
            android:textSize="10sp" />


    </LinearLayout>

    <!--  Main Linear Layout -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#DFDFDF"
        android:orientation="vertical">

        <!--   first Frame -->


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="horizontal">

            <!-- first row first column -->

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_marginBottom="5dp"
                android:layout_marginLeft="5dp"
                android:layout_marginTop="5dp"
                android:layout_weight="1"
                android:background="#ffffff"
                android:gravity="center"
                android:orientation="vertical">

                <ImageView
                    android:layout_width="70dp"
                    android:layout_height="70dp"
                    android:layout_gravity="center"
                    android:src="@mipmap/saving"></ImageView>

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:text="Saving Collection"
                    android:textColor="#494949"
                    android:textSize="13sp"
                    android:textStyle="bold" />


            </LinearLayout>


            <!-- first row second column -->

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_marginBottom="5dp"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="5dp"
                android:layout_marginTop="5dp"
                android:layout_weight="1"
                android:background="#ffffff"
                android:gravity="center"
                android:orientation="vertical">

                <ImageView  **i have made changes here**
                    android:layout_width="70dp"
                    android:layout_height="70dp"
                    android:layout_gravity="center"
                    android:src="@mipmap/loancollection"></ImageView>

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:text="Loan Collection"
                    android:textColor="#494949"
                    android:textSize="13sp"
                    android:textStyle="bold" />


            </LinearLayout>


        </LinearLayout>


        <!-- 2nd frame  -->

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="horizontal">

            <!-- Second row first column -->

            <LinearLayout       **i have made changes here**
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_marginBottom="5dp"
                android:layout_marginLeft="5dp"
                android:layout_weight="1"
                android:background="#ffffff"
                android:gravity="center"
                android:orientation="vertical">

                <ImageView
                    android:layout_width="70dp"
                    android:layout_height="70dp"
                    android:layout_gravity="center"

                    android:src="@mipmap/dailysaving"></ImageView>

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:text="Daily Saving"
                    android:textColor="#494949"
                    android:textSize="13sp"
                    android:textStyle="bold"
               />


            </LinearLayout>


            <!-- Second row Second column -->

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_marginBottom="5dp"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="5dp"

                android:layout_weight="1"
                android:background="#ffffff"
                android:orientation="vertical">

                <ImageView
                    android:layout_width="70dp"
                    android:layout_height="70dp"
                    android:layout_gravity="top|center"
                    android:layout_marginTop="10dp"
                    android:src="@mipmap/memberenrollment"></ImageView>

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginTop="5dp"
                    android:text="Member Enrollment"
                    android:textColor="#494949"
                    android:textSize="13sp"
                    android:textStyle="bold" />


            </LinearLayout>


        </LinearLayout>


        <!-- 3rd frame  -->

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="horizontal">

            <!-- Third row First column -->


            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_marginBottom="5dp"
                android:layout_marginLeft="5dp"

                android:layout_weight="1"
                android:background="#ffffff"
                android:orientation="vertical">

                <ImageView
                    android:layout_width="70dp"
                    android:layout_height="70dp"
                    android:layout_gravity="top|center"
                    android:layout_marginTop="10dp"
                    android:src="@mipmap/cashincashout"></ImageView>

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginTop="5dp"
                    android:text="Cash In-Out "
                    android:textColor="#494949"
                    android:textSize="13sp"
                    android:textStyle="bold" />


            </LinearLayout>
            <!-- Third row Second column -->
            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_marginBottom="5dp"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="5dp"

                android:layout_weight="1"
                android:background="#ffffff"
                android:orientation="vertical">

                <ImageView
                    android:layout_width="70dp"
                    android:layout_height="70dp"
                    android:layout_gravity="top|center"
                    android:layout_marginTop="10dp"
                    android:src="@mipmap/collectorledger"></ImageView>

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginTop="5dp"
                    android:text="Collector Ledger "
                    android:textColor="#494949"
                    android:textSize="13sp"
                    android:textStyle="bold" />


            </LinearLayout>


        </LinearLayout>


        <!-- 4th frame  -->


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="horizontal">


            <!-- Fourth row First column -->


            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_marginBottom="5dp"
                android:layout_marginLeft="5dp"

                android:layout_weight="1"
                android:background="#ffffff"
                android:orientation="vertical">

                <ImageView
                    android:layout_width="70dp"
                    android:layout_height="70dp"
                    android:layout_gravity="top|center"
                    android:layout_marginTop="10dp"
                    android:src="@mipmap/summary"></ImageView>

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginTop="5dp"
                    android:text="Summary"
                    android:textColor="#494949"
                    android:textSize="13sp"
                    android:textStyle="bold" />


            </LinearLayout>

            <!-- Fourth row Second column -->


            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_marginBottom="5dp"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="5dp"

                android:layout_weight="1"
                android:background="#ffffff"
                android:orientation="vertical">

                <ImageView
                    android:layout_width="70dp"
                    android:layout_height="70dp"
                    android:layout_gravity="top|center"
                    android:layout_marginTop="10dp"
                    android:src="@mipmap/officesync"></ImageView>

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginTop="5dp"
                    android:text="Office Sync"
                    android:textColor="#494949"
                    android:textSize="13sp"
                    android:textStyle="bold" />


            </LinearLayout>


        </LinearLayout>


        <!-- 4th frame end  -->


    </LinearLayout>   <!-- main frame end Layout -->


</LinearLayout>

I have made changes on the first row first column and on the 1st row 2nd column

**I want the imageView to lie exactly in Center of the Linear Layout and then the Text will Adjust on the remaining portion.**How can this issue be solved?

Upvotes: 0

Views: 76

Answers (5)

rafsanahmad007
rafsanahmad007

Reputation: 23881

If you want to use Linearlayout to match your requirement use below code:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="150dp"
    android:layout_marginBottom="5dp"
    android:layout_marginLeft="10dp"
    android:layout_marginTop="20dp"
    android:layout_weight="1"
    android:orientation="horizontal"
    android:weightSum="3">

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="@drawable/bottom_border_help"
        android:orientation="vertical">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginRight="10dp"
            android:layout_weight="1"
            android:gravity="center"
            android:padding="15dp"
            android:scaleType="fitCenter"
            android:src="@mipmap/ic_launcher" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="HELLO"
            android:textColor="@color/color_black" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="@drawable/bottom_border_help"
        android:orientation="vertical">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginRight="10dp"
            android:layout_weight="1"
            android:gravity="center"
            android:padding="15dp"
            android:scaleType="fitCenter"
            android:src="@mipmap/ic_launcher" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="HELLO"
            android:textColor="@color/color_black" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="@drawable/bottom_border_help"
        android:orientation="vertical">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginRight="10dp"
            android:layout_weight="1"
            android:gravity="center"
            android:padding="15dp"
            android:scaleType="fitCenter"
            android:src="@mipmap/ic_launcher" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="HELLO"
            android:textColor="@color/color_black" />
    </LinearLayout>


</LinearLayout>

Output

enter image description here

Upvotes: 1

MinnuKaAnae
MinnuKaAnae

Reputation: 1646

LinearLayout Instead of RelativeLayout

<!-- first row first column -->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginBottom="5dp"
            android:layout_marginLeft="5dp"
            android:layout_marginTop="5dp"
            android:layout_weight="1"
            android:background="#ffffff"
            android:orientation="vertical"
            android:weightSum="2"
            android:gravity="center">

            <ImageView
                android:id="@+id/sjkashjkasd"
                android:layout_width="70dp"
                android:layout_height="70dp"
                android:src="@drawable/conference_room"
                android:layout_weight="1.9"/>

            <TextView
                android:id="@+id/asdlkjl"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="Saving Collection"
                android:textColor="#494949"
                android:textSize="13sp"
                android:textStyle="bold"
                android:gravity="center"
                android:layout_weight="0.1"/>
        </LinearLayout>

Upvotes: 1

MinnuKaAnae
MinnuKaAnae

Reputation: 1646

enter image description here

UseBelowCode

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="30dp"
    android:background="#156C7F"
    android:orientation="horizontal">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginLeft="125dp"
        android:text="Branch Office : Dhankuta"
        android:textColor="#FFFFFF"
        android:textSize="10sp" />

    <View
        android:layout_width="1dp"
        android:layout_height="15dp"
        android:layout_gravity="center"
        android:layout_marginLeft="7dp"
        android:background="#FFFFFF" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginLeft="7dp"
        android:text="Tuesday - 4 April, 2017"
        android:textColor="#FFFFFF"
        android:textSize="10sp" />
</LinearLayout>

<!--  Main Linear Layout -->
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#DFDFDF"
    android:orientation="vertical">

    <!--   first Frame -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="horizontal">

        <!-- first row first column -->
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginBottom="5dp"
            android:layout_marginLeft="5dp"
            android:layout_marginTop="5dp"
            android:layout_weight="1"
            android:background="#ffffff"
            android:orientation="vertical">

            <ImageView
                android:layout_width="70dp"
                android:layout_height="70dp"
                android:src="@drawable/conference_room"
                android:layout_centerInParent="true"/>

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="Saving Collection"
                android:textColor="#494949"
                android:textSize="13sp"
                android:textStyle="bold" 
                android:gravity="center"
                android:layout_alignParentBottom="true"/>
        </RelativeLayout>

        <!-- first row second column -->
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginBottom="5dp"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_marginTop="5dp"
            android:layout_weight="1"
            android:background="#ffffff"
            android:gravity="center"
            android:orientation="vertical">

            <ImageView
                android:layout_width="70dp"
                android:layout_height="70dp"
                android:src="@drawable/conference_room"
                android:layout_centerInParent="true"/>

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="Loan Collection"
                android:textColor="#494949"
                android:textSize="13sp"
                android:textStyle="bold"
                android:gravity="center"
                android:layout_alignParentBottom="true"/>
        </RelativeLayout>
    </LinearLayout>


    <!-- 2nd frame  -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="horizontal">

        <!-- Second row first column -->
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginBottom="5dp"
            android:layout_marginLeft="5dp"
            android:layout_weight="1"
            android:background="#ffffff"
            android:gravity="center"
            android:orientation="vertical">

            <ImageView
                android:layout_width="70dp"
                android:layout_height="70dp"
                android:src="@drawable/conference_room"
                android:layout_centerInParent="true"/>


            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="Daily Saving"
                android:textColor="#494949"
                android:textSize="13sp"
                android:textStyle="bold"
                android:gravity="center"
                android:layout_alignParentBottom="true"/>
        </RelativeLayout>

        <!-- Second row Second column -->
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginBottom="5dp"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_weight="1"
            android:background="#ffffff"
            android:orientation="vertical">

            <ImageView
                android:layout_width="70dp"
                android:layout_height="70dp"
                android:src="@drawable/conference_room"
                android:layout_centerInParent="true"/>

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginTop="5dp"
                android:text="Member Enrollment"
                android:textColor="#494949"
                android:textSize="13sp"
                android:textStyle="bold"
                android:gravity="center"
                android:layout_alignParentBottom="true"/>
            </RelativeLayout>
        </LinearLayout>

    <!-- 3rd frame  -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="horizontal">

        <!-- Third row First column -->
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginBottom="5dp"
            android:layout_marginLeft="5dp"
            android:layout_weight="1"
            android:background="#ffffff"
            android:orientation="vertical">

            <ImageView
                android:layout_width="70dp"
                android:layout_height="70dp"
                android:src="@drawable/conference_room"
                android:layout_centerInParent="true"/>


            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginTop="5dp"
                android:text="Cash In-Out "
                android:textColor="#494949"
                android:textSize="13sp"
                android:textStyle="bold"
                android:gravity="center"
                android:layout_alignParentBottom="true"/>

        </RelativeLayout>

        <!-- Third row Second column -->
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginBottom="5dp"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_weight="1"
            android:background="#ffffff"
            android:orientation="vertical">

            <ImageView
                android:layout_width="70dp"
                android:layout_height="70dp"
                android:src="@drawable/conference_room"
                android:layout_centerInParent="true"/>


            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginTop="5dp"
                android:text="Collector Ledger "
                android:textColor="#494949"
                android:textSize="13sp"
                android:textStyle="bold"
                android:gravity="center"
                android:layout_alignParentBottom="true"/>
            </RelativeLayout>
        </LinearLayout>

        <!-- 4th frame  -->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="horizontal">

            <!-- Fourth row First column -->
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginBottom="5dp"
                android:layout_marginLeft="5dp"
                android:layout_weight="1"
                android:background="#ffffff"
                android:orientation="vertical">

                <ImageView
                    android:layout_width="70dp"
                    android:layout_height="70dp"
                    android:src="@drawable/conference_room"
                    android:layout_centerInParent="true"/>


                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginTop="5dp"
                    android:text="Summary"
                    android:textColor="#494949"
                    android:textSize="13sp"
                    android:textStyle="bold"
                    android:gravity="center"
                    android:layout_alignParentBottom="true"/>
            </RelativeLayout>

            <!-- Fourth row Second column -->
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginBottom="5dp"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="5dp"
                android:layout_weight="1"
                android:background="#ffffff"
                android:orientation="vertical">

                <ImageView
                    android:layout_width="70dp"
                    android:layout_height="70dp"
                    android:src="@drawable/conference_room"
                    android:layout_centerInParent="true"/>


                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginTop="5dp"
                    android:text="Office Sync"
                    android:textColor="#494949"
                    android:textSize="13sp"
                    android:textStyle="bold"
                    android:gravity="center"
                    android:layout_alignParentBottom="true"/>
            </RelativeLayout>
        </LinearLayout>
        <!-- 4th frame end  -->
</LinearLayout>   <!-- main frame end Layout -->

Upvotes: 1

MinnuKaAnae
MinnuKaAnae

Reputation: 1646

add this line in LinearLayout

android:layout_width="match_parent"
android:gravity="center"

Upvotes: 0

Gordon developer
Gordon developer

Reputation: 417

You can set the gravity of the Linear Layout that is holding your views to centre, this way

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:background="#156C7F"
        android:gravity="center"
        android:orientation="horizontal">


        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginLeft="125dp"
            android:text="Branch Office : Dhankuta"
            android:textColor="#FFFFFF"
            android:textSize="10sp" />

        <View
            android:layout_width="1dp"
            android:layout_height="15dp"
            android:layout_gravity="center"
            android:layout_marginLeft="7dp"
            android:background="#FFFFFF" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginLeft="7dp"
            android:text="Tuesday - 4 April, 2017"
            android:textColor="#FFFFFF"
            android:textSize="10sp" />


    </LinearLayout> 

This simply means that any child elements inside of your linearLayout should be defaultly placed at the exact center of the layout. When you define the gravity for the linearlayout there will be no need to define elements layout_gravity, it will be automatically set to center.

Upvotes: 0

Related Questions