Shadow
Shadow

Reputation: 6899

How to align imageview, and two text at center of screen android?

Hi I have parent layout as LinearLayout and I need to place an imageview at center and after that two textview one after another at center of screen. At bottom of the screen is an icon.

I placed everything but imageview and two textview are not aligning at center of screen. Here is 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"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="@drawable/bg"
    android:orientation="vertical">

   //this image and two textview, i need to place at center of screen
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:layout_gravity="center"
    android:orientation="vertical">
    <ImageView
        android:id="@+id/seat_icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/on_logo"
        android:layout_marginBottom="@dimen/dp_size20"
        />

    <TextView
        android:id="@+id/seat_number"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Test1"
        android:textAllCaps="false"
        android:textColor="@color/white"
        android:textSize="16dp"
        android:gravity="center"/>
    <TextView
        android:id="@+id/another_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="@dimen/dp_size10"
        android:paddingBottom="@dimen/dp_size10"
        android:text="TEst 2"
        android:textAllCaps="false"
        android:textColor="@color/white"
        android:textSize="16dp"
        android:gravity="center"/>

</LinearLayout>
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="@dimen/dp_size10">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:weightSum="1"
            android:layout_alignParentBottom="true"
            android:layout_alignParentStart="true">

            <ImageView
                android:id="@+id/on_boarding_circle_arrow"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_centerInParent="true"
                android:background="@drawable/circle_arrow" />
</RelativeLayout>
    </RelativeLayout>
    </LinearLayout>

I really couldn't figure out what mistake I am doing in this design. ImageView, and two text are placing at top of screen and not in center of screen.

Upvotes: 1

Views: 1933

Answers (6)

Anil kumar
Anil kumar

Reputation: 1

cut the 'layout_gravity' and 'gravity' parameters from child LinearLayout and paste this to Parent LinearLayout

<LinearLayout

    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:layout_gravity="center"
    android:orientation="vertical">

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
       android:orientation="vertical">
            <ImageView
                 android:id="@+id/seat_icon"
                 android:layout_width="match_parent"
                 android:layout_height="wrap_content"
         />
 Your code....

 >

Upvotes: 0

Tushar Patil
Tushar Patil

Reputation: 77

I have changed some dimensions and images, correct those and you are good to go.

try this..

<?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:background="@drawable/bg">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:layout_centerInParent="true"
            android:orientation="vertical">
            <ImageView
                android:id="@+id/seat_icon"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@mipmap/ic_launcher"
                android:layout_marginBottom="20dp" />
            <TextView
                android:id="@+id/seat_number"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Test1"
                android:textAllCaps="false"
                android:textSize="16dp"
                android:gravity="center"/>
            <TextView
                android:id="@+id/another_text"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:paddingTop="@dimen/dp_size10"
                android:paddingBottom="@dimen/dp_size10"
                android:text="TEst 2"
                android:textAllCaps="false"
                android:textColor="@color/white"
                android:textSize="16dp"
                android:gravity="center"/>

        </LinearLayout>
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_marginBottom="@dimen/dp_size10">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:weightSum="1"
                android:layout_alignParentBottom="true"
                android:layout_alignParentStart="true">

                <ImageView
                    android:id="@+id/on_boarding_circle_arrow"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_centerInParent="true"
                    android:background="@mipmap/ic_launcher" />
            </RelativeLayout>
        </RelativeLayout>
    </RelativeLayout>
</LinearLayout>

Upvotes: 2

Arpan Sharma
Arpan Sharma

Reputation: 2162

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

    <LinearLayout
        android:layout_centerInParent="true"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_gravity="center_vertical"
        android:orientation="vertical">
        <ImageView
            android:id="@+id/seat_icon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="20dp"
            />

        <TextView
            android:id="@+id/seat_number"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Test1"
            android:textAllCaps="false"
            android:textColor="@color/Grey"
            android:textSize="16dp"
            android:gravity="center"/>
        <TextView
            android:id="@+id/another_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingTop="10dp"
            android:paddingBottom="10dp"
            android:text="TEst 2"
            android:textAllCaps="false"

            android:textSize="16dp"
            android:gravity="center"/>

    </LinearLayout>
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        >

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            >

            <ImageView
                android:id="@+id/on_boarding_circle_arrow"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_centerInParent="true"
                android:background="@drawable/acircle" />
        </RelativeLayout>
    </RelativeLayout>
</RelativeLayout>

The problem is that you are using linear layout so it is aligning the layouts one after another.So you need to use relative layout and it will work

Upvotes: 1

Akshay Panchal
Akshay Panchal

Reputation: 695

Just add android:gravity="center" in your root LinearLayout and remove last RelativeLayout and see

Upvotes: 0

Gidi Sprintzin
Gidi Sprintzin

Reputation: 465

The RelativeLayout at the bottom is the problem. And add android:gravity="center" to your parent LinearLayout

Upvotes: 0

Shanto George
Shanto George

Reputation: 994

Try this code

 <?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"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="@drawable/bg"
    android:orientation="vertical">

   //this image and two textview, i need to place at center of screen
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:layout_gravity="center"
    android:orientation="vertical">
    <ImageView
        android:id="@+id/seat_icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/on_logo"
        android:layout_marginBottom="@dimen/dp_size20"
        />

    <TextView
        android:id="@+id/seat_number"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Test1"
        android:textAllCaps="false"
        android:textColor="@color/white"
        android:textSize="16dp"
        android:gravity="center"/>
    <TextView
        android:id="@+id/another_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="@dimen/dp_size10"
        android:paddingBottom="@dimen/dp_size10"
        android:text="TEst 2"
        android:textAllCaps="false"
        android:textColor="@color/white"
        android:textSize="16dp"
        android:gravity="center"/>

</LinearLayout>
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="@dimen/dp_size10">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:weightSum="1"
            android:layout_alignParentBottom="true"
            android:layout_alignParentStart="true">

            <ImageView
                android:id="@+id/on_boarding_circle_arrow"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_centerInParent="true"
                android:background="@drawable/circle_arrow" />
</RelativeLayout>
    </RelativeLayout>
    </LinearLayout>

Upvotes: 0

Related Questions