Adding an icon alongside text in an Android layout by nesting an ImageView and TextView inside a LinearLayout inflates unnecessary View objects, degrading layout render performance. Android TextView supports compound drawables, allowing you to embed vector icons directly inside the TextView container.
XML Layout Implementation (app:drawableStartCompat)
<!-- Single TextView with left vector icon & padding -->
<TextView
android:id="@+id/emailTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="support@lynxbee.com"
android:textSize="16sp"
android:textColor="#1e293b"
<!-- Embed Vector Icon on Start/Left side -->
app:drawableStartCompat="@drawable/ic_email"
app:drawableTint="#2563eb"
android:drawablePadding="12dp"
android:gravity="center_vertical" />Programmatic Kotlin Implementation
val textView = findViewById<TextView>(R.id.emailTextView)
// Programmatically set start (left), top, end (right), and bottom drawables
textView.setCompoundDrawablesWithIntrinsicBounds(
R.drawable.ic_email, // Start
0, // Top
0, // End
0 // Bottom
)
Comments and corrections