[android] RelativeLayout center vertical

I want to make a list row layout. This layout has a imageview in the most left, a textview right next to the imageview, and a imageview in the most right. I want all of them are center vertical.

<RelativeLayout
    android:layout_width="fill_parent" android:layout_height="100dp"
    android:gravity="center_vertical"
    >
    <ImageView 
        android:id="@+id/icon"
        android:layout_width="50dp" android:layout_height="50dp"
        android:layout_gravity="center_vertical" />
    <TextView 
        android:id="@+id/func_text" android:layout_toRightOf="@id/icon"
        android:layout_width="wrap_content" android:layout_height="100dp"
        android:layout_gravity="center_vertical" />
    <ImageView 
        android:layout_width="50dp" android:layout_height="50dp"
        android:layout_alignParentRight="true"
        android:layout_gravity="center_vertical"
        android:src="@drawable/arrow" />
</RelativeLayout>

I also tried to add android:layout_centerVertical="true" to the textview, but the result is the textview align bottom with the two imageview. I tried this in android 4.2 emulator. Anybody could help me about this?

This question is related to android center android-relativelayout

The answer is


Try aligning top and bottom of text view to one of the icon, this will make text view sharing same height as them, then set gravity to center_vertical to make the text inside text view center vertically.

<TextView 
        android:id="@+id/func_text" android:layout_toRightOf="@id/icon"
        android:layout_alignTop="@id/icon" android:layout_alignBottom="@id/icon"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:gravity="center_vertical" />

This maybe because the textview is too high. Change android:layout_height of the textview to wrap_content or use

android:gravity="center_vertical"

use

 android:layout_centerVertical="true"

For me, I had to remove

<item name="android:gravity">center_vertical</item>

from RelativeLayout, so children's configuration would work:

<item name="android:layout_centerVertical">true</item>

If View's height/width = wrap_content

use:

android:layout_centerHorizontal="true"
android:layout_centerVertical="true"

If View's height/width = match_parent

use:

android:gravity="center_vertical|center_horizontal"

Adding both android:layout_centerInParent and android:layout_centerVertical work for me to center ImageView both vertical and horizontal:

<ImageView
    ..
    android:layout_centerInParent="true"
    android:layout_centerVertical="true"
    />

       <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentStart="true"
                android:layout_centerInParent="true"
                android:layout_gravity="center_vertical"
                android:layout_marginTop="@dimen/main_spacing_extra_big"
                android:orientation="horizontal">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"

                    android:fontFamily="@font/noto_kufi_regular"
                    android:text="@string/renew_license_municipality"
                    android:textColor="@color/sixth_text"
                    android:textSize="@dimen/main_text" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:fontFamily="@font/noto_kufi_regular"
                    android:text="@{RenewLicenseBasicInfoFragmentVM.tvMunicipality}"
                    android:textColor="@color/sixth_text"
                    android:textSize="@dimen/main_text" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentEnd="true"
                android:layout_centerInParent="true"
                android:layout_gravity="center_vertical"
                android:layout_marginTop="@dimen/main_spacing_extra_big"
                android:orientation="horizontal">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="end"
                    android:fontFamily="@font/noto_kufi_regular"
                    android:text="@string/renew_license_license_number"
                    android:textColor="@color/sixth_text"
                    android:textSize="@dimen/main_text" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="end"
                    android:fontFamily="@font/noto_kufi_regular"
                    android:text="@{RenewLicenseBasicInfoFragmentVM.tvLicenseNum}"
                    android:textColor="@color/sixth_text"
                    android:textSize="@dimen/main_text" />
            </LinearLayout>`enter code here`

        </RelativeLayout>

        <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_marginHorizontal="5dp"
        android:layout_marginBottom="5dp"
        android:background="@drawable/default_button">

        <ImageView
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:layout_alignParentLeft="true"
            android:layout_centerInParent="true"
            android:layout_marginStart="15dp"
            android:src="@drawable/google" />

        <Button
            android:id="@+id/btnGmailLogin"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@null"
            android:paddingHorizontal="15dp"
            android:text="@string/gmail_login_button_text"
            android:textAllCaps="false"
            android:textColor="@color/black" />
    </RelativeLayout>

This is working for me.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/rell_main_bg"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#096d74" > 

<ImageView 
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true"
    android:src="@drawable/img_logo_large"
    android:contentDescription="@null" />

</RelativeLayout>

Examples related to android

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How to implement a simple scenario the OO way My eclipse won't open, i download the bundle pack it keeps saying error log getting " (1) no such column: _id10 " error java doesn't run if structure inside of onclick listener Cannot retrieve string(s) from preferences (settings) strange error in my Animation Drawable how to put image in a bundle and pass it to another activity FragmentActivity to Fragment A failure occurred while executing com.android.build.gradle.internal.tasks

Examples related to center

Center Plot title in ggplot2 How to make a <div> or <a href="#"> to align center How to center an unordered list? Bootstrap 3 modal vertical position center How to align title at center of ActionBar in default theme(Theme.Holo.Light) How to center absolute div horizontally using CSS? Resize to fit image in div, and center horizontally and vertically Center fixed div with dynamic width (CSS) RelativeLayout center vertical Center button under form in bootstrap

Examples related to android-relativelayout

How to switch from the default ConstraintLayout to RelativeLayout in Android Studio Differences between ConstraintLayout and RelativeLayout Moving from one activity to another Activity in Android RelativeLayout center vertical Android Relative Layout Align Center Android: show/hide a view using an animation How to set RelativeLayout layout params in code not in xml? How to create a RelativeLayout programmatically with two buttons one on top of the other? Percentage width in a RelativeLayout What are the differences between LinearLayout, RelativeLayout, and AbsoluteLayout?