Search code examples
androidandroid-layoutandroid-widget

Centering the TextView's text vertically in relation to its drawableLeft image


I have this component:

<TextView
    android:id="@+id/item_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:drawableLeft="@drawable/icon" />

The image in the drawableLeft attribute is bigger than the font of the text, so I want the text centered vertically in relation to the image. How can I get this effect?


Solution

  • You want "gravity" note this is not to be confused with layout gravity, layout gravity moves the entire textview, while gravity moves the text within the text view.

    <TextView
        android:id="@+id/item_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:drawableStart="@drawable/icon"
        android:gravity="center_vertical" />