Search code examples
androidandroid-layoutandroid-imageviewandroid-image

Image in chat bubble having unwanted margins


I am having this simple layout for chat bubble.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/bubble_layout_chat_parent"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/bubble_layout_chat"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/message_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/message_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@android:color/primary_text_light"
        android:layout_gravity="top"
        android:textSize="16sp"/>

    <TextView
        android:id="@+id/message_time"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#999"
        android:textSize="10sp"
        android:textStyle="italic"
        android:textColorHint="@color/grey"
        android:layout_gravity="bottom|end"/>
</LinearLayout>

However, when I set image it leaves unwanted margins above and below the image.

enter image description here

How can I remove margins and make the bubble to fit image?

(Note: What I observed is the margins appears only when image width is larger than height. For images having width much smaller than height no margins observed)


Solution

  • Thanks for everyone's comments. Setting android:adjustViewBounds="true" to ImageView fixed my problem. So my working ImageView now looks like:

        <ImageView
            android:id="@+id/message_image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"/>