1

I have a layout with two children one after another, like this:

|<view1><text1>                      |

<view1> may change it's width. So, I want it increases while both views stay on the screen:

|<really_wide_view1><text1>          |

But when there is no more space at right, it stops:

|<reeeeeeeeeeeally_wide_vi...><text1>|

Is there easy way to do this?

Now I try to use this layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#FF00aa00" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="loooooooooooooooooooooooooooooooooooong text" />
    </LinearLayout>

    <TextView
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:background="#FFaa0000"
        android:minWidth="30dp"
        android:singleLine="true"
        android:text="test" />

</LinearLayout>

But result is the same: result

Jin35
  • 8,602
  • 3
  • 32
  • 52

3 Answers3

1

I am able to solve your problem using the following code (a modified version of yours):

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="#FF00aa00" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong text" 
            />
    </LinearLayout>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_weight="0"
        android:background="#FFaa0000"
        android:text="testtest" />
</LinearLayout>

Here is a screenshot of the same: Wrap Test

Let me know if the above piece of code solves the problem.

Arnab
  • 736
  • 5
  • 14
  • This solves the problem with long text, but now it works wrong for short texts. I have found solution with TableView, I'll post it in five minutes – Jin35 Jun 18 '12 at 19:10
0

Try to give both views inside the RelativeLayout and make the views layout width and as a wrap_content and give orientation as horizontal. I think it will solve your problem.

Akilan
  • 1,707
  • 1
  • 16
  • 30
  • This doesn't work. If there isn't relations between views - it covers one another. If I use `toRightOf` for second view - it goes away from screen – Jin35 Jun 18 '12 at 18:24
0

Here is solution with TableLayout, but it looks dirty.

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:shrinkColumns="0" >

    <TableRow>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
        </LinearLayout>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </TableRow>

</TableLayout>

It works for any length of both text views.

Jin35
  • 8,602
  • 3
  • 32
  • 52
  • I was going to post an answer but you manged the figure out the `TableLayout` solution. I've answer a similar question here http://stackoverflow.com/questions/9448113/android-layout-align-to-right-without-wrapping-and-reserving-space/9448231#9448231 . Basically there is no need to add the `LinearLayout`, you just have to add `android:shrinkColumns="0" android:stretchColumns="1"` to your `TableLayout`. – user Jun 18 '12 at 19:20
  • `LinearLayout` goes here like container, there will be some complex view inside. – Jin35 Jun 18 '12 at 19:41