0

I'm trying to make the TextView containing the text "This is some long text" to float in the centre relative to the image on the left, but mine currently looks like:

enter image description here

This TextView can take up to 2 lines of text, so if it does have 2 lines, I hope to make it restore to the standard alignment like:

enter image description here

Here's my layout now:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="match_parent"
          android:layout_height="match_parent">
<ImageView
        android:id="@+id/user_left"
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:scaleType="centerCrop"
        android:src="@drawable/the_pirates"
        android:layout_margin="5dp"
        />

<TextView android:id="@+id/relationship_caption"
          android:layout_width="100dp"
          android:layout_height="wrap_content"
          android:textSize="8dp"
          android:text="This is some long text"
          android:layout_toRightOf="@id/user_left"
          android:layout_margin="5dp"/>

</RelativeLayout>    

I've tried setting gravity to center, but that didn't seem to have helped.\

Thanks!

Kar
  • 6,063
  • 7
  • 53
  • 82
  • @ASP setting `gravity` to `center_vertical` doesn't achieve it, as I'm trying to get the text to float center with respect to the image on the left. – Kar Mar 01 '14 at 07:35
  • set `centerVertical="true"` in TextView – Ankit Popli Mar 01 '14 at 07:37
  • try putting your imageview and textview in a horizontal LinearLayout and then work on it. – ASP Mar 01 '14 at 07:41
  • @ASP Piyush's solution of setting alignTop. But it seems alignBottom is also needed. – Kar Mar 01 '14 at 07:45
  • @ASP That solution doesn't work for me. What I get is an image in the corner, but the next being in the middle of the screen (rather than middle of w.r.t the image). – Kar Mar 01 '14 at 07:56
  • did u try android:layout_gravity="left" for your textview. – ASP Mar 01 '14 at 08:00
  • @ASP That might mean I'll need to wrap the TextView in another layout, right? That seems a bit complex – Kar Mar 01 '14 at 08:05

2 Answers2

2

Use this one..

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >

<ImageView
    android:id="@+id/user_left"
    android:layout_width="20dp"
    android:layout_height="20dp"
    android:layout_margin="5dp"
    android:scaleType="centerCrop"
    android:src="@drawable/the_pirates" />

<TextView
    android:id="@+id/relationship_caption"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/user_left"
    android:layout_toRightOf="@+id/user_left"
    android:maxLines="2"
    android:text="This is some long text"
    android:textSize="15dp" />

</RelativeLayout>
Piyush
  • 18,895
  • 5
  • 32
  • 63
0

here i have done what you want

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="20dp" >//here you can give your size

        <ImageView
            android:id="@+id/user_left"
            android:layout_width="120dp"//here you can put your size as you want
            android:layout_height="match_parent"
            android:scaleType="fitXY"
            android:src="@drawable/ic_launcher" />

        <TextView
            android:id="@+id/relationship_caption"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_toRightOf="@+id/user_left"
            android:maxLines="2"
            android:text="This is some long text"
            android:textSize="15sp" />
    </RelativeLayout>

</RelativeLayout>
Nitin
  • 258
  • 2
  • 8