Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 764
Oasis Feng Avatar asked Jan 30 '13 14:01

Oasis Feng


People also ask

How to set the drawables of a textview from code?

It’s almost as easy to set these drawables from code: textView.setCompoundDrawablePadding (…); With this idea in mind, we can create very interesting layouts. Let’s create a simple username and password form: Quite easy.

What is the default text alignment of text in textview?

The default text alignment of text in TextView is left. Note: When you specify text alignment, it takes effect only when the width of TextView widget is more than the width of text. In other words, the size of container should be greater than the size of content, or, if the text spans multiple lines.

What is third textview in widget?

Third TextView – Width of the widget wraps the content, which is the text. But as text spans multiple lines, the text is effectively displayed with center alignment.

How to center text in textview area?

You can use center_vertical | center_horizontal value to set the text at the center of the TextView area. HTML, HTML5. CSS, CSS3 Color Codes 2020 - Html


1 Answers

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.

like image 140
Paul Lammertsma Avatar answered Oct 02 '22 19:10

Paul Lammertsma