Android Question Distibute buttons? or anything!

aidymp

Well-Known Member
Licensed User
Longtime User
Hi,
LAYOUTS on android are a real problem for me! I know you can use CODE! to do your layouts, but I am mathematically challenged! I have x number of buttons/images/any control that I position on a layout, and on every device they are wrong!

For example I have 4 buttons horizontally, placed all on the same level, but spaced by hand!
I also have 3 Images, in the same format underneath. But has someone got a sub, or even a formula I can use to place them all evenly, and keep them looking good, including some scaling?

Currently my app looks GREAT! on my device, but on any other device, its just a mess!

I know this is due to my poor maths skills, but im sure someone, has a sub that just works out the spacing! or has some tips for a newbie!

Thanks

Aidy
 
Last edited:

canalrun

Well-Known Member
Licensed User
Longtime User
Hello,
I do all my layouts by positioning the elements in code.

I started doing this before there were Designer Scripts. Maybe those replace a lot of what I am doing.

The code below is from one of my apps

B4X:
  Dim spc As Int
  spc = (pnBtn.Width - (4 * bnInp.Width)) / 5

  bnInp.Left = spc
  bnClr.Left = bnInp.Left + bnInp.Width + spc
  bnSav.Left = bnClr.Left + bnClr.Width + spc
  bnRcl.Left = bnSav.Left + bnSav.Width + spc

  edMemo.SetLayout(0, pnTitle.Height + pnMemo.Height * 0.01, _
    pnMemo.Width, pnMemo.Height - (pnTitle.Height + pnBtn.Height + pnMemo.Height * 0.02))
  edMemo.Text = ""

  Dim cd As ColorDrawable

  cd.Initialize2(ClrBGnd1, 5, 1, ClrBrdr1)
  edMemo.Background = cd

I usually have panels that hold a group of buttons. Spc calculates even spacing between four buttons positioned within that panel.
edMemo is an EditText that is positioned 1% from the top of its parent panel, pnMemo, and fills 98% of the panel minus a couple other panel heights.
I then set the background and border colors.

I hope this gives some ideas. It's certainly not the prettiest thing in the world – I don't know of a universal solution. I usually have separate subroutines with a whole lot of code similar to the above for both landscape and portrait layouts.

Barry.
 
Upvote 0

aidymp

Well-Known Member
Licensed User
Longtime User
I would highly recommend learning the Designer and Designer Scripts, this will solve most of your headaches

SO the designer can evenly space buttons that will display correctly on any device?
 
Upvote 0

JonPM

Well-Known Member
Licensed User
Longtime User
Upvote 0

aidymp

Well-Known Member
Licensed User
Longtime User
Thank you! yourself and others really make this community what it is! USEFUL!

Thanks again

Aidy
 
Upvote 0

aidymp

Well-Known Member
Licensed User
Longtime User
Upvote 0
Top