Center TextView horizontally in parent as long there is space with xml layouting

66 views Asked by At

I want to center a title within a view as long it's possible and the space is not limited by surrounding buttons. Additionally, if the space is getting limited by the surrounding buttons, it should break into "maxLines".

To make it easy to understand, take a look at the following visualization: enter image description here

Is this possible with xml layouting? Probably with TableLayout somehow?

Current xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="?android:attr/actionBarSize"
                android:layout_gravity="fill_horizontal"
                android:orientation="vertical"
                android:background="@android:color/transparent"
                android:paddingLeft="5dp"
                android:paddingRight="5dp">

    <ImageButton
        android:id="@+id/backBtn"
        android:paddingLeft="5dp"
        android:paddingRight="5dp"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:background="?android:selectableItemBackground"
        android:src="@drawable/ic_action_navigation_back_item"
        android:layout_width="wrap_content"
        android:layout_height="match_parent" />

    <ImageButton
        android:id="@+id/menuBtn"
        android:paddingLeft="5dp"
        android:paddingRight="5dp"
        android:layout_centerVertical="true"
        android:layout_toRightOf="@id/backBtn"
        android:background="?android:selectableItemBackground"
        android:src="@drawable/icon_menu"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:scaleType="fitCenter" />

    <ImageButton
        android:id="@+id/shareBtn"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:background="?android:selectableItemBackground"
        android:src="@drawable/player_button_share"
        android:layout_width="wrap_content"
        android:layout_height="match_parent" />

    <TextView
        android:id="@+id/titleTV"
        android:tag="title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:textAlignment="center"
        android:gravity="center"
        android:text="short text"
        android:textColor="@android:color/white"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:maxLines="2" />

</RelativeLayout>
0

There are 0 answers