Set actual text padding in TextView with compound drawables
Asked Answered
E

3

23

I have a ListView populated with an ArrayAdapter. For items I use just a single TextView layout. I want some of the rows to have compound drawables set.

Question: is there a way to set padding for the actual text that is contained in TextView so that the compound drawables don't get the padding too? Other solution would be to lock the width of text. Do I need to add ImageViews to my layout?

Exocarp answered 17/9, 2012 at 8:23 Comment(7)
I'm curious about this too, did you figure out a solution?Aura
Nah. I just added some ImageViews and that was my solution. I even got a Tumbleweed badge for this question so I guess it's impossible.Exocarp
Hah, funny. I do see a way to put padding on the image, but not padding on the text.Aura
Which way is that? I think setCompoundDrawables method gives you that option please let me know your solution.Exocarp
txtView.setCompoundDrawablePadding (int pad); or your can set the bounds on your image and then use setCompoundDrawablesWithIntrinsicBounds. I'm playing with them now to see how they work out!Aura
Well, why don't you try setting a positive padding to the whole TextView and then set a negative padding to the drawable. That would make the text shifted and image on it's own place. I don't have access to my computer ATM so I can't test it.Exocarp
That does seem like it would work, I guess you've answered your own question!Aura
E
12

I'm posting this as an answer so that someone, who comes across this post finds the answer.

There's no direct way to set a padding only to text, but you can set a positive padding to the whole TextView and set a negative padding for the drawable.

Exocarp answered 5/10, 2012 at 15:33 Comment(0)
P
50

Quite simple:

android:drawablePadding="5dp"

It will automatically use the padding according to the direction.

Pontiac answered 19/6, 2013 at 2:36 Comment(2)
Thank you for your answer. I already solved my problem then and I don't have source to try out your method.Exocarp
And just for reference the method is setCompoundDrawablePaddingJane
E
12

I'm posting this as an answer so that someone, who comes across this post finds the answer.

There's no direct way to set a padding only to text, but you can set a positive padding to the whole TextView and set a negative padding for the drawable.

Exocarp answered 5/10, 2012 at 15:33 Comment(0)
M
9

Improving on Gio's sugestion, which is probably for a dated API/SDK...

TL:DR - just apply android:drawablePadding to increase distance between image and text.

Explanation: android:drawablePadding now applies padding between CompoundDrawable and original View, in this case the TextView containing the text;

android:padding and modifiers now apply to the whole group, including all compounds.

Doing the negative-positive hack doesn't seem to work no more.

So depending on the drawable position (drawableTop/Left/...), the padding will apply in the opposite direction of it, right next to the drawable, in other words between the two elements. For instance, Applying drawablePadding="10dp" to a left placed drawable would have a similar effect as setting an individual ImageView to the left of the TextView with paddingRight="10dp" (at least padding-wise).

Megan answered 24/10, 2013 at 0:59 Comment(1)
I guess it was for a dated SDK, I also wanted to have some legacy support. At that time I couldn't think of a better solution than I posted.Exocarp

© 2022 - 2024 — McMap. All rights reserved.