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.