1

EDIT: Answer found through Emil's comments. Code ended up like this:

FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
               FrameLayout.LayoutParams.WRAP_CONTENT, 
               FrameLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(left, top, right, bottom);
view.setLayoutParams(params);

Thanks all :D

I'm currently writing an Android application and I have a dynamically created tree which displays ImageView elements within it. However, to arrange these images I've used padding left to line them up correctly, dynamically changing how the padding is calculated so that everything turns out evenly. However when I do this, the padding covers up some of the images to the left, and even though you can see the images behind this padding, when you click on that image, it will evaluate the image who's padding is being used (the furthest left element). I'm basically wondering if there is a way to programatically set something that does what padding does to images but isn't clickable? Margins would make sense, but you cannot set margins on ImageViews.

The tree set up is like this:

-----A-----
--B--C--D--
-E-F----G--

Imagine every letter is an ImageView, dynamically placed with padding. But if I click on B or C, D's code will be evaluated because it's left padding covers B and C. The same goes for E or F, click those, and G will be evaluated. I cannot figure out a different way to place these ImageViews. Any help is greatly appreciated.

Jake Gascho
  • 106
  • 1
  • 8

1 Answers1

5

Consider replacing your padding with layout_margin property. that way the area that separates the padded view is not considered as part of the view but ratter as part of the layout it sits in.

So use

android:layout_margin="10dp"

or:

android:layout_marginLeft="10dp"

for your needs.

Emil Adz
  • 40,709
  • 36
  • 140
  • 187
  • I wish I could do that... The ImageViews are all created programatically and then added to a frame view. But because it isn't declared in the XML at all, I cannot set the margins in that way... – Jake Gascho Feb 28 '13 at 23:53
  • 1
    you can head here: http://stackoverflow.com/questions/4472429/change-the-right-margin-of-a-view-programmatically to see how to achieve the same result programaticlly. – Emil Adz Feb 28 '13 at 23:56
  • Thank you Emil, this was very helpful and eventually it worked for me :) – Jake Gascho Mar 01 '13 at 00:14