How to align the text with its compound drawables in TextView?
Asked Answered
H

2

6

I can't figure out how to set the alignment of text and its compound drawable (smaller than text) in a TextView. It seems the default alignment is center, but I need to align them by the edge.

PS: The android:gravity works great if compound drawable is larger than text, but not if smaller.

Haik answered 30/1, 2013 at 14:41 Comment(0)
M
1

It sounds like you're using an image with fixed dimensions for android:drawableLeft.

Try using a nine-patch or defining a drawable through an XML resource file. In this way your drawable's dimensions will stretch to align in the TextView the way that you want it to.

Malatya answered 30/1, 2013 at 14:53 Comment(2)
Good idea for nine-patch! But I'm still slightly confused about the second solution. Take "android:drawableLeft" for example, how to write the bridge XML drawable to align the text with the compound image by top?Haik
I haven't actually tried it, but you should be able to use <bitmap> in conjunction with android:gravity.Malatya
A
0

I had a similar problem in that I wanted to use setCompoundDrawables to set a top drawable for a floating hint, however TextView (EditText) sets the top drawable to center, irrespective of what its width is.

I am using a custom Drawable, and inside its draw method, I am grabbing the clipBounds, and translating on its top,left to get back to the TextView's 0,0 position.

The other way to do it easily is to getMatrix and mapPoints from 0,0 then take the negative of them and translate back to 0,0 that way.

Agio answered 22/6, 2014 at 19:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.