'How to make icon the same size as text in Android?

I have TextView with some text and ImageView with some icon. I aligned them by baseline:

android:baselineAlignBottom="true"
app:layout_constraintBaseline_toBaselineOf="@+id/penny"

TextView has some text size, but I can't guess the height of ImageView so that the text (not TextView) and the icon are the same height, they are always slightly different. How I can fix it?enter image description here

<androidx.constraintlayout.widget.ConstraintLayout ... >
<TextView
    android:id="@+id/text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="16sp"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintStart_toStartOf="parent" />

<ImageView
    android:id="@+id/icon"
    android:layout_width="wrap_content"
    android:layout_height="12dp"
    android:baselineAlignBottom="true"
    app:layout_constraintBaseline_toBaselineOf="@+id/text"
    app:layout_constraintStart_toEndOf="@+id/text"
    android:src="@drawable/icon" />
</androidx.constraintlayout.widget.ConstraintLayout>


Solution 1:[1]

You can set top and bottom of image view to top and bottom of textView respectively with height of imageView as 0dp and scaleType as fitXY.

Solution 2:[2]

You can try this one it might be work for you.

Make Linear layout horizontal with height as wrap_content, Take Textview with your required text size. Take image view with height as match_parent and width as wrap_content.

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Demo Text"
            android:textColor="@color/black"
            android:textSize="@dimen/_30sdp" />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:src="@drawable/ic_app_logo" />

    </LinearLayout>

And for ConstraintLayout layout yu can try this as below.

set android:scaleType="fitXY"

<androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/tvDemoText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Demo Text"
            android:textColor="@color/black"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            android:textSize="@dimen/_35sdp" />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:scaleType="fitXY"
            app:layout_constraintTop_toTopOf="@id/tvDemoText"
            app:layout_constraintBottom_toBottomOf="@id/tvDemoText"
            app:layout_constraintStart_toEndOf="@id/tvDemoText"
            android:src="@drawable/material_drawer_ico_menu_down" />

    </androidx.constraintlayout.widget.ConstraintLayout>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Anmol
Solution 2