Search code examples
androidandroid-relativelayoutellipsis

In android ellipsize text view between buttons


I've looked at many posts on similar subject but couldn't find the answer to this problem... I use a RelativeLayout to have a button on the left, a button on the right and text in the middle. If the text is too long I use ellipsize (see XML below). The problem is that when the text is long it also cuts the BEGINNING of the code (see image). I can fix that by using android:gravity="center_vertical" instead of "center", but then when the text is short it becomes left-justified. I want it centered. Any hint ?

<RelativeLayout
    android:layout_height="60dp"
    android:layout_width="match_parent" >
    <ImageButton
        android:id="@+id/leftbutton"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_alignParentLeft="true"
        android:layout_gravity="center"
        android:src="@drawable/back_chevron_icon"
        android:background="@color/transparent" />
    <ImageButton
        android:id="@+id/rightbutton"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_alignParentRight="true"
        android:layout_gravity="center"
        android:src="@drawable/back_chevron_icon"
        android:background="@color/transparent" />
    <TextView
        android:id="@+id/sometext"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:gravity="center"
        android:layout_toRightOf="@id/leftbutton"
        android:layout_toLeftOf="@id/rightbutton"
        android:maxLines="1"
        android:ellipsize="end"
        android:textSize="22dp"
        android:text="Long Long Long Long Long Long Long Long Text" />
</RelativeLayout>

with android:gravity:"center_vertical

What I want is shown in the 2 pictures below. I can achieve that only by changing the gravity from center to center_vertical and vice versa.

long text ok

shor text ok


Solution

  • It turns out that the code in the initial post WORKS ! It is the Design simulator in Android Studio that actually does not work and shows the (wrong) text layout as in my initial picture. Thank you @#*%! Android Studio developers... I wasted several hours of my time thanks to you...