Oasis Feng
Oasis Feng

Reputation: 7520

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

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.

Upvotes: 6

Views: 5144

Answers (2)

Peterm
Peterm

Reputation: 1

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.

Upvotes: 0

Paul Lammertsma
Paul Lammertsma

Reputation: 38292

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.

Upvotes: 1

Related Questions