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?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Gio
  • 1,954
  • 3
  • 22
  • 42
  • I'm curious about this too, did you figure out a solution? – Alan Moore Sep 30 '12 at 14:45
  • 2
    Nah. I just added some `ImageView`s and that was my solution. I even got a `Tumbleweed` badge for this question so I guess it's impossible. – Gio Sep 30 '12 at 17:59
  • 1
    Hah, funny. I do see a way to put padding on the image, but not padding on the text. – Alan Moore Sep 30 '12 at 21:05
  • Which way is that? I think `setCompoundDrawables` method gives you that option please let me know your solution. – Gio Sep 30 '12 at 21:47
  • 2
    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! – Alan Moore Sep 30 '12 at 23:48
  • 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. – Gio Oct 01 '12 at 07:47
  • That does seem like it would work, I guess you've answered your own question! – Alan Moore Oct 01 '12 at 13:21

3 Answers3

49

Quite simple:

android:drawablePadding="5dp"

It will automatically use the padding according to the direction.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Ornithopter
  • 2,020
  • 2
  • 17
  • 16
  • Thank you for your answer. I already solved my problem then and I don't have source to try out your method. – Gio Jun 19 '13 at 06:32
  • 8
    And just for reference the method is `setCompoundDrawablePadding` – Niklas Jun 02 '15 at 14:49
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.

JJD
  • 50,076
  • 60
  • 203
  • 339
Gio
  • 1,954
  • 3
  • 22
  • 42
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).

leRobot
  • 1,497
  • 1
  • 18
  • 30
  • 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. – Gio Oct 25 '13 at 06:17