26

I've added image Drawable start in a TextView . Problem is i cant control the gravity of that Drawable in TextView

What i need to achieve What i need to achieve

What i have achieved so far

What i have achieved so far

This is my TextView

        <TextView
            android:id="@+id/tv_8_digit_check"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:drawablePadding="@dimen/dimen_4"
            android:drawableStart="@drawable/ic_validate"
            android:text="@string/at_least_8_characters_txt"
            android:textColor="@color/white_trans"
            android:textSize="12sp" />

Any suggestion on how can i set gravity of that Drawable to top/start? Thanks

Usman Ghauri
  • 815
  • 1
  • 7
  • 23

4 Answers4

6

Use checkbox instead of textview Drawable, and add custom drawable where you can use this image

Mrinmoy
  • 1,342
  • 2
  • 15
  • 27
Jinal Patel
  • 631
  • 4
  • 15
  • 4
    You can change CheckBox drawable `android:button="@drawable/your_drawable" android:gravity="top" android:paddingLeft="6dp"` – Ultimo_m Jun 04 '18 at 13:53
6
  1. Try using android:gravity="top"

    if it didnt work then go with negative margin like this

  2. android:drawablePadding="-20sp"

  3. Another alternative way is to take an ImageView beside TextView inside LinearLayout so you can apply gravity

Manohar Reddy
  • 15,894
  • 7
  • 77
  • 109
0

I suggest you put image to imageview. Put the imageview and the textview in a Linearlayout and set gravity to top. Reduce the size of text and image too.

K.Sopheak
  • 22,440
  • 4
  • 26
  • 73
0
<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:gravity="top">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="@dimen/dimen_4"
        android:src="@drawable/ic_validate"/>
    <TextView
        android:id="@+id/tv_8_digit_check"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/at_least_8_characters_txt"
        android:textColor="@color/white_trans"
        android:textSize="12sp" />

</LinearLayout>
  • i know this solution would work. but the problem is nesting layouts. and view optimization if it was for adding another view in the layout why android built compound drawable to be set in the same TextView ? – Usman Ghauri Feb 27 '17 at 09:18
  • Usually when text is short compound drawable is good idea, but when you want something custom it can't help. You can put android:maxLines="1" and reduce text size, but it can cause a problem on small screens. – Davit Avetisyan Feb 27 '17 at 09:25
  • Also you can create additional TextView for "Character" under main TextView – Davit Avetisyan Feb 27 '17 at 09:27