Search code examples
androidandroid-layoutandroid-linearlayout

Is this layout possible in Android?


This layout seems simple but I can't find any way to do this solely in xml using LinearLayout or any other layout without changing properties dynamically. Here is what I want, if text is short then layout should be totally wrapped as shown in figure:

enter image description here

But in case TextView contains longer text then it should be expanded but it must leave space for Button like this:

enter image description here


Solution

  • After experimenting a lot of layout, the best that I can do is to use TableLayout with shrinkColumns

    <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:shrinkColumns="0" >
        <TableRow>
            <TextView
                android:id="@+id/text"
                android:ellipsize="end"
                android:singleLine="true"
                android:text="This is a very long text and it cannot be displayed wholly" />
    
            <Button
                android:id="@+id/button"
                android:text="Button" />
        </TableRow>
    </TableLayout>