2

I have the following requirement in Android:

Given a horizontal LinearLayout, I have five buttons in it. All the buttons should have equal width and height. The height of each button is same as their parent LinearLayout and spacing between them should remain constant. The height of LinearLayout is not constant and depends on form factor and other layouts sizing. Therefore, I can not assign fixed with/height to each button.

While I can easily achieve that very easily in iOS with the help of constraints, I am not sure how to achieve this in Android at design time. Is there some way to achieve this or is it possible programatically only?

NoDataDumpNoContribution
  • 10,591
  • 9
  • 64
  • 104
Aarkan
  • 3,811
  • 6
  • 40
  • 54

3 Answers3

0

The height of each button is same as their parent LinearLayout

Set the height to match_parent

For your width, you'll have to calculate the screen size programmatically and set the widths accordingly. See this question.

Community
  • 1
  • 1
adao7000
  • 3,632
  • 2
  • 28
  • 37
0

Try this . As you have a horizontal LinearLayout with 5 buttons, for equal spacing of all buttons, you must first allocate the space to each button of of the total space available like this..

<LinearLayout
            android:weightSum="5"
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
             <Button
                    android:layout_weight="1"
                    android:layout_width="0dp"
                    android:layout_height="allocate it programatically"
                    />

This button shown above has to be for all 5 buttons u have. The idea allocating weight sum of 5 to LinearLayout and then dividing it as 1 to each button. Note the width here for the button should be 0dp.

Then dynamically change your button Height and Width per your requirement

Prabhuraj
  • 928
  • 2
  • 8
  • 14
  • I meant spacing (gap) between buttons to be same, not just their width. So the idea is to have a constant spacing and then remaining width should be assigned equally to 5 buttons. – Aarkan Jul 17 '15 at 15:47
0

Unfortunately no typical layout in Android seems to have the concept of this kind of layout constraint (square like elements).

It's the layout that ultimately determines the sizes of their children. Therefore if you want that you have to write your own layout implementing this constraint.

For this extend ViewGroup and in onLayout enforce that width of the children or a specific children equals height. You could even invent your own LayoutParams for this task. See the documentation of ViewGroupfor a general example. The exact implementation very much depends on which other requirements you have for your layout.

NoDataDumpNoContribution
  • 10,591
  • 9
  • 64
  • 104