Search code examples
androidnine-patch

Resize 9 patch image with respect to content


I need my 9-patch image to wrap the content. If there is a little text, it should be really small to wrap that text. And if there is an image insetead of text it should wrap it too. Here is my 9-patch png:

9-patch

But here it what it looks like with content:

Message

This is the code in xml file:

<RelativeLayout
    android:id="@+id/message_text_layout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@id/other_user_layout"
    android:layout_toLeftOf="@+id/user_layout"
    android:layout_below="@id/message_date"
    >
    <TextView
        android:id="@+id/message_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/white"
        android:layout_marginTop="@dimen/margin"
        />
    <ImageView
        android:id="@+id/message_picture"
        android:layout_width="wrap_content"
        android:layout_height="100dp"
        android:layout_centerInParent="true"
        android:scaleType="fitCenter"/>
</RelativeLayout>

I'm setting the background to this relative layout programmatically. How can I make this message layout to exactly fit the content and not stretching all over the screen.


Solution

  • The problem was in lines:

    android:layout_toRightOf="@id/other_user_layout"
    android:layout_toLeftOf="@+id/user_layout"
    

    I am setting one of this programmatically depending on the message author.