user2923535 user2923535 - 21 days ago 12
Android Question

Vertically Center a TextView to the right of a FrameLayout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:id="@+id/container"
android:background="?android:attr/selectableItemBackground"
android:layout_height="wrap_content">
<FrameLayout
android:layout_width="wrap_content"
android:id="@+id/frame"
android:layout_marginLeft="16dp"
android:layout_marginTop="18dp"
android:layout_marginBottom="10dp"
android:layout_height="wrap_content">
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:minHeight="5dip"
android:minWidth="5dip"
android:maxWidth="20dip"
android:maxHeight="20dip"
android:id="@+id/progress_bar"
android:visibility="visible"/>

<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/cover"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
/>
<TextView
android:id="@+id/changeinscore"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0.00"
android:shadowColor="#000000"
android:shadowDx="1"
android:shadowDy="1"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:shadowRadius="7"
android:textSize="12sp"
android:textColor="@color/background"
android:fontFamily="sans-serif-condensed"/>
</RelativeLayout>

</FrameLayout>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/middle"
android:layout_toRightOf="@+id/frame"
android:layout_centerVertical="true"
android:text="TextView"

/>

</RelativeLayout>


I want to vertically center the middle TextView to the right of the FrameLayout, as the changeinscore TextView is inside the FrameLayout, if that makes sense. Currently it's off center because the centerVertical is centering it in the entire RelativeLayout, not specifically to the FrameLayout, if I understand it correctly. Basically I want the middle textview to be empty but to actually anchor other textviews so I can have 2 textviews slightly offcenter to the framelayout/imageview.

If I follow what San says, here is the result, but not exactly what I want
enter image description here

San San
Answer

I finally got what you wanted and here is my sample of it.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:id="@+id/container"
    android:background="?android:attr/selectableItemBackground"
    android:layout_height="wrap_content">

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/frame"
        android:layout_margin="10dp"
        android:gravity="center">


        <ImageView
            android:id="@+id/cover"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/im"
            />

        <TextView
            android:id="@+id/changeinscore"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="0.00"
            android:shadowColor="#000000"
            android:shadowDx="1"
            android:shadowDy="1"
            android:visibility="gone"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:shadowRadius="7"
            android:textSize="12sp"
            android:layout_margin="5dp"
            android:textColor="@color/background"
            android:fontFamily="sans-serif-condensed"/>

        <ProgressBar
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:minHeight="5dip"
            android:minWidth="5dip"
            android:maxWidth="20dip"
            android:maxHeight="20dip"
            android:layout_centerInParent="true"
            android:id="@+id/progress_bar"
            android:visibility="visible"/>

    </RelativeLayout>


    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_centerHorizontal="true"
        android:layout_alignTop="@+id/frame"
        android:layout_alignBottom="@+id/frame"
        android:layout_toRightOf="@id/frame"
        android:gravity="center"
        android:weightSum="3"
        android:orientation="vertical">


        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Aldnoah.Zero"
            android:layout_weight="1"
            android:ellipsize="end"
            android:maxLines="1"
            android:paddingLeft="5dp"
            android:gravity="bottom"
            android:fontFamily="sans-serif-slab"
            android:textSize="16sp"
            />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:id="@+id/middle"
            android:text="TextView"
            android:layout_weight="1"
            android:paddingLeft="5dp"
            android:textSize="14sp"
            />

        <TextView
            android:id="@+id/score"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Bottom Text"
            android:paddingLeft="5dp"
            android:textSize="14sp"
            android:gravity="top"
            android:fontFamily="sans-serif-condensed"/>

    </LinearLayout>



</RelativeLayout>

And the layout looks like this