How to change the size of background 9patch image according to the size of child textview

272 views Asked by At

I am making a chat bubble. I made the bubble using 9patch image. But I want to resize the bubble according to the size of textview.. How to do it ?

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    >

    <LinearLayout
android:background="@drawable/senderbubble2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:paddingBottom="10dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true">

        <TextView
            android:id="@+id/txt_msg"
            android:text="hello world"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="5dp"
            android:textColor="#000000" />

    </LinearLayout>

</RelativeLayout>

The above is my xml code of chat bubble...

1

There are 1 answers

0
Starlord On

You can get the height & width of textView, by calling below methods on textview reference, below method returns int value, you can use it to resize image.

textView.getMeasuredHeight();
textView.getMeasuredWidth();

If you want the height/width of text in textView you can try

Rect bounds = new Rect();

textView.getPaint().getTextBounds(textView.getText().toString(), 0, textView.getText().length(), bounds)

see documentation for more details https://developer.android.com/reference/android/view/View.html