Shadow Shadow - 6 days ago 5
Android Question

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.

Answer

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>
Comments