Change gravity of image drawable in textview
Asked Answered
E

5

38

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

Exasperation answered 27/2, 2017 at 8:43 Comment(1)
An easy to use solution could be here github.com/imbeerus/compound-text-view. But it seems to have deprecated dependencies.Embellish
O
12
  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

Ointment answered 27/2, 2017 at 8:52 Comment(3)
gravity top not working in my case. and about padding i already have to assign some padding for other purpose.Exasperation
then go for the third ideaOintment
yes end up doing the third solution. i know that solution but i thought someone might have better solution for this. thanksExasperation
Z
9

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

Zampino answered 27/2, 2017 at 8:45 Comment(1)
You can change CheckBox drawable android:button="@drawable/your_drawable" android:gravity="top" android:paddingLeft="6dp"Mclellan
C
2

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.

Composite answered 27/2, 2017 at 8:53 Comment(0)
L
2
<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>
Logicize answered 27/2, 2017 at 8:58 Comment(3)
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 ?Exasperation
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.Logicize
Also you can create additional TextView for "Character" under main TextViewLogicize
S
1

Use checkbox instead of textview and disable click and focus of that view

<CheckBox
        android:gravity="top"
        android:button="@drawable/ic_right_managed"
        android:clickable="false"
        android:focusable="false"
        text="I look like a check box but beware I'm not"/>
Sagunto answered 29/6, 2021 at 14:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.