Jin35
Jin35

Reputation: 8612

Layout views with dynamic width

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

Upvotes: 1

Views: 920

Answers (3)

Jin35
Jin35

Reputation: 8612

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.

Upvotes: 0

Arnab
Arnab

Reputation: 726

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.

Upvotes: 1

Akilan
Akilan

Reputation: 1717

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.

Upvotes: 0

Related Questions