Android Question How to center 3 side by side views irrespective of screen width?

Mashiane

Expert
Licensed User
Longtime User
Hi

I have 3 imageviews, view1, view2 and view3 inside a panel. I'd like these to be centered on the panel irrespective of the screen size, the panel is anchored for horizontal both. how can I achieve this?

I have depicted these below..

Thanks.
 

Attachments

  • centerviews.png
    centerviews.png
    6.3 KB · Views: 199

canalrun

Well-Known Member
Licensed User
Longtime User
dim spc as int = (panel.width - (imgv1.width + imgv2.width + imgv3.width)) / 4

imgv1.left = spc
imgv2.left = imgv1.left + imgv1.width + spc
imgv3.left = imgv2.left + imgv2.width + spc

Warning this is not tested, but I do similar positioning all the time.
You can do something similar to position the top of each imageview.

Update:
Whoops, I just noticed you have three panels to center horizontally.
Substitute panelN for imgvN above and activity.width for panel.width.


Barry.
 
Upvote 0

Mashiane

Expert
Licensed User
Longtime User
dim spc as int = (panel.width - (imgv1.width + imgv2.width + imgv3.width)) / 4

imgv1.left = spc
imgv2.left = imgv1.left + imgv1.width + spc
imgv3.left = imgv2.left + imgv2.width + spc

Warning this is not tested, but I do similar positioning all the time.
You can do something similar to position the top of each imageview.

Update:
Whoops, I just noticed you have three panels to center horizontally.
Substitute panelN for imgvN above and activity.width for panel.width.


Barry.
Thanks Barry, let me try it.
 
Upvote 0

Mashiane

Expert
Licensed User
Longtime User
dim spc as int = (panel.width - (imgv1.width + imgv2.width + imgv3.width)) / 4

imgv1.left = spc
imgv2.left = imgv1.left + imgv1.width + spc
imgv3.left = imgv2.left + imgv2.width + spc

Warning this is not tested, but I do similar positioning all the time.
You can do something similar to position the top of each imageview.

Update:
Whoops, I just noticed you have three panels to center horizontally.
Substitute panelN for imgvN above and activity.width for panel.width.


Barry.
Thanks a lot, it worked!! Perfect!
 
Upvote 0
Top