0

I have an activity, containing a layout. I have a pool of 10 imagebuttons set up and hidden (invisible) in this layout. I am trying to load the image to these imagebuttons programmatically, and then set their location on the screen.

I have found this previous discussion: How to set margin of ImageView using code, not xml

This, however, does not work. I am getting tiny imagebuttons as a result, despite setting the height and width parameters. Here is my code:

RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(btnGP1.getLayoutParams());
            lp1.leftMargin = P1leftValue;
            lp1.topMargin = P1topValue;
            lp1.height = 80;
            lp1.width = 80;
            btnGP1.setLayoutParams(lp1);

Something about this is not right, but I am not having any luck finding a more accurate resource. Any ideas?

Update:

I have edited my code to this:

            RelativeLayout.LayoutParams lp1 = (RelativeLayout.LayoutParams)btnGP1.getLayoutParams();
            lp1.leftMargin = P1leftValue;
            lp1.topMargin = P1topValue;
            lp1.height = 280;
            lp1.width = 280;
            btnGP1.setLayoutParams(lp1);

And it appears to be almost working. The only thing that remains now is that the functions above only appear to be allowing me to enter pixel sizes. I need to be able to enter pixel densities instead as not all screen sizes are the same.

My new question is this: how do I go about this? Is there a simple answer here? If I try 280dp or "280dp" the compiler will not allow it.

Community
  • 1
  • 1
Bisclavret
  • 1,327
  • 9
  • 37
  • 65
  • It may be the height of the layout is that much only. That is the reason may be the height is not shown increased – Terril Thomas May 05 '15 at 14:25
  • @Terril: Thanks for the response. How would I change the height of the layout? The layout in the xml file is set to wrap_content, as is the width. – Bisclavret May 06 '15 at 03:02
  • Try to explicitly define the width and height by adding something like layout_width= 220dp and layout_height = 200dp and check the result – Terril Thomas May 06 '15 at 07:38
  • @Terril: Thanks for the response. See my updated question where I explain that you cannot explicitly assign dp values through code. I don't know why, because this seems the only way you would want to do it, but it is what it is. You can only assign pixel values through code. Unless, someone knows another way. This is my question ;) – Bisclavret May 06 '15 at 10:17

1 Answers1

0

The answer to this was found in another thread. This was my final code:

final float SCALE = getResources().getDisplayMetrics().density;
RelativeLayout.LayoutParams LP = new RelativeLayout.LayoutParams(btnGP15.getLayoutParams());
                LP.leftMargin = (int) (P1leftValue * SCALE + 0.5f);
                LP.topMargin = (int) (P1topValue * SCALE + 0.5f);
                LP.height = (int) (tileSize * SCALE + 0.5f);
                LP.width = (int) (tileSize * SCALE + 0.5f);

Where btnGP15 is my image, P1leftValue and P1topValue store the dp values for my required location, so 100 and 150 for example. tileSize stores the dp value for the size of my images, 70 in my case.

Working perfectly ;)

Bisclavret
  • 1,327
  • 9
  • 37
  • 65