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.