Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vertically Center TextView in RelativeLayout

In the image below, i want the textview in red to be vertically centered, I would like to keep using just RelativeLayout not any LinearLayouts, I think i can figure out how to do this with LinearLayout but do not want to use these.

This is all based off of the examples i found here android devleper's blog - Android Layout Tricks #1

alt text

Here is my xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="?android:attr/listPreferredItemHeight"
    android:padding="6dip">

    <ImageView android:id="@+id/icon" android:layout_width="wrap_content"
        android:layout_height="fill_parent" 
        android:layout_alignParentTop="true"
        android:layout_alignParentBottom="true" 
        android:layout_alignParentRight="true"
        android:src="@drawable/chevron" />



    <TextView android:id="@+id/rightText" android:layout_width="55px"
        android:layout_height="fill_parent" android:layout_toLeftOf="@id/icon"
        android:layout_alignParentTop="true"
        android:layout_alignParentBottom="true"
        android:textSize="14dip"
        android:text="$15.99" />


    <TextView android:id="@+id/secondLine" android:layout_width="wrap_content"
        android:layout_height="26dip" android:layout_toLeftOf="@id/rightText"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true" 
        android:text="Simple application that shows how to use RelativeLayout" />

    <TextView android:layout_width="fill_parent" android:id="@+id/firstLine"
        android:layout_height="wrap_content" android:layout_toLeftOf="@id/rightText"
        android:layout_alignParentLeft="true" android:layout_alignParentTop="true"
        android:layout_above="@id/secondLine"
        android:layout_alignWithParentIfMissing="true" android:gravity="center_vertical"
        android:singleLine="true"
        android:ellipsize="end"
        android:maxLines="1"
        android:textSize="13dip"
        android:text="My Application that has a really long title  asdfsdf asdf sdf asdf" />

</RelativeLayout>
like image 796
slim Avatar asked Jul 09 '10 19:07

slim


2 Answers

You just need to set the Gravity of the TextView.

like image 147
CaseyB Avatar answered Oct 22 '22 16:10

CaseyB


It may be help full for others, if above answer does not work. Just set the below attribute in textView

 android:layout_centerVertical="true"
like image 4
Tara Avatar answered Oct 22 '22 16:10

Tara