Search code examples
androidandroid-layoutandroid-viewtextview

Space between textview and drawable icon


I'm facing problems with the position of an icon. I've set that icon as drawableLeft of a TextView, this is working fine but, if I set the TextView's gravity to center_horizontal, then a padding space appears between the icon and the text. The code is as follows:

<TextView
    style="@style/BaseText.White.Bold"
    android:gravity="center_horizontal"
    android:drawableStart="@drawable/ic_text"
    android:drawableLeft="@drawable/ic_text"
    android:text="@string/text"
    android:layout_marginBottom="@dimen/margin_small"
    android:layout_marginRight="@dimen/margin_big"
    android:layout_marginLeft="@dimen/margin_big"
    android:padding="0dp"
    android:drawablePadding="0dp"/>

What do I have to do to ensure that the icon is shown at the text's left side but horizontally centered? I've make two screenshots to show you the difference when adding or removing the icon's gravity. When the textview's gravity is not set, the result is as follows: enter image description here

But, when the gravity is added, then a padding appears between the text and the icon: enter image description here Thanks in advance


Solution

  • Try to give android:gravity="center" to Text View. Also add android:layout_width="wrap_content" and android:layout_height="wrap_content" properties in Text View.

    Refer this.

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:layout_marginBottom="5dp"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:drawableLeft="@mipmap/ic_launcher"
            android:drawablePadding="0dp"
            android:drawableStart="@mipmap/ic_launcher"
            android:gravity="center"
            android:textSize="22sp"
            android:padding="0dp"
            android:text="TestingTestingTestingTestingTestingTestingTestingTestingTesting" />
    
    </RelativeLayout>
    

    This is what it showing.

    enter image description here