How to use wrap_content with a maximum width?

Here you go, a layout that does exactly what you want.

<?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="wrap_content"
    android:clipChildren="false"
    android:paddingBottom="10dp"
    android:paddingRight="10dp"
    android:paddingTop="10dp">


    <RelativeLayout
        android:id="@+id/blank"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#aaaaaa">
        <LinearLayout
            android:id="@+id/message_container"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingRight="100dp">
            <TextView
                android:id="@+id/message"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Hello?"
                android:background="#00ff00" />
        </LinearLayout>

        <TextView
            android:id="@+id/time"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/message_container"
            android:layout_marginLeft="-100dp"
            android:text="12:30 PM"
            android:background="#ff0000" />
    </RelativeLayout>

</RelativeLayout>

Short message Short message

Long message Long message


I know this is a really old question, but it's a frustrating problem I've encountered several times now and the existing answers weren't quite what I was looking for. Some colleagues and I came up with the following:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:background="#FFFFFF">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#888888"
        android:padding="10dp"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="#00FF00"
            tools:text="Short message."/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_weight="0"
            android:background="#CCCCCC"
            tools:text="Yesterday,\n11:30pm"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:background="#888888"
        android:padding="10dp"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/message_text_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="#00FF00"
            tools:text="Super ultra mega awesome long message which is going to help us take over the world."/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_weight="0"
            android:background="#CCCCCC"
            tools:text="Yesterday,\n11:31pm"/>

    </LinearLayout>

</LinearLayout>

Which looks like this when rendered:

enter image description here

The magic seems to be the zero value for the weight of the text box on the right (in addition to the non-zero weight value of the text box on the left, which some of the other answers already have).

Honestly, I can't explain exactly why it works, but after having looked for a solution to this for so long I'm not questioning it! :)

As an aside, I like this approach because it doesn't require any explicit or minimum widths, any intermediate wrapper views, or the use of clipping settings, margins, padding, etc. to implement view overlay.


You can try the following arrangement of views and their widths:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:tools="http://schemas.android.com/tools"
              android:orientation="horizontal"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:padding="6dp"
        >
    <FrameLayout
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content">
        <TextView 
                android:layout_width="wrap_content" 
                android:layout_height="wrap_content"
                android:background="@android:color/holo_blue_bright"
                tools:text="Some long test is this which is support to wrap at the end of parent view"
                />
    </FrameLayout>
    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="6dp"
            tools:text="Yesterday,\n 11:30 PM"
            />
</LinearLayout>

What the author of this question really asks is, how to let the TextView expand to fit the message inside of it without overflowing the time TextView, and without leaving blank spaces.

Since you don't actually know the width of the whole screen, you can't tell your TextView to be 100dp less than it. What you should do is wrap your TextView in a container which will have the toLeftOf rule, with the TextView only wrapping it's contents. This way, the container will expand all the way up to the right (without overflowing the time TextView) but the TextView will only wrap it's text contents (so, it won't extend any blank spaces)

Code

Instead of

<TextView
        android:id="@+id/message_holder"
        android:layout_toLeftOf="@id/blank"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="@dimen/horizontalMargin"
        android:background="@drawable/message_corners"
        style="@style/white_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="alsdkjf; alsdkf" />

Use

<LinearLayout
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"       
 android:layout_toLeftOf="@id/blank"
 android:layout_alignParentLeft="true"
 android:layout_marginLeft="@dimen/horizontalMargin">

     <TextView
          android:id="@+id/message_holder"
          android:background="@drawable/message_corners"
          style="@style/white_text"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="alsdkjf; alsdkf" />
</LinearLayout>

By the way, your layout isn't very good. You should optimize it.