Simple way to do dynamic but square layout

Related searches

I'm using a GridView to display a bunch of views which are essentially LinearLayouts. I want the LinearLayouts to all be square, but I also want them to be dynamically sized--that is, there are two columns and I want the LinearLayouts to stretch depending on the size of the screen but remain square. Is there a way to do this through the xml layout or do I have to set the heights and widths programmatically?

A neat solution for square GridView items is to extend RelativeLayout or LinearLayout and override onMeasure like so:

public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, widthMeasureSpec);

Dynamic Square & Rectangular layouts in Android, we will see how to create a Dynamic Square & Rectangular Layouts Simply call the custom layout where ever applicable as shown below. Square & Rectangular Layouts. In this quick and easy tutorial, we will see how to create Dynamic Square & Rectangular layouts where width is same as height or height is twice the width. This is totally dynamic and adjust itself. This can be applied on any layouts including LinearLayout, RelativeLayout, FrameLayout, ImageView or any other views.

It might be late to answer but still, it will be helpful to the new visitors.

With the new ConstraintLayout introduced in Android Studio 2.3, it is now quite easy to build responsive layouts.

In a parent ConstraintLayout, to make any of its children view/layout dynamically square, add this attribute


w is to specify width-wise constraints and 1:1 ratio ensures square layout.

Building Dynamic UI Layouts in JavaFX, using some of the basic dynamic layout mechanisms in JavaFX. This ensures that the Ellipse will always stay on top of the Text but still allows the you how you can build a Reversi board square that dynamically responds to height and� Adjust : If you're laying out a large square or rectangle (with batterboards) by yourself with no one to read the diagonal from the tape as you adjust the string line at the batterboard to get it square (get the diagonal right), after you get it 'close' (eyeball it), measure the diagonal and enter it here to calculate how far to move the string line at the batterboard, to square it.

There's nothing in the xml that will let you link the width and height properties. Probably the easiest thing to do is to subclass LinearLayout and override onMeasure

@Override public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int width = MeasureSpec.getSize(widthMeasureSpec);
    int height = MeasureSpec.getSize(heightMeasureSpec);
    int size = width > height ? height : width;
    setMeasuredDimension(size, size);

I've used this to create views that are always square before. It should still work for a LinearLayout.

More info that will help doing this:

How to Constrain a Square View with Auto Layout in iOS, On the other hand, constraining simple views can require a lot of constraints. To tell Auto Layout how large the square should be, all I need to do is pin I am going to setup the constraints again, but I will use two different� Layout squares function as a Try Square, Miter Square, Protractor, Line Scriber and a Saw Guide. Personally, I use a layout square so often as a guide for simple cross-cuts that I don’t even realize I’m doing it anymore, it’s become so ingrained in the way I cut wood. The layout square ‘s 1/4-inch spaced notches make scribing lines a

I've done this way:

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int widthMode = MeasureSpec.getMode(widthMeasureSpec);
    int widthSize = MeasureSpec.getSize(widthMeasureSpec);
    int heightMode = MeasureSpec.getMode(heightMeasureSpec);
    int heightSize = MeasureSpec.getSize(heightMeasureSpec);

    int size;
    if(widthMode == MeasureSpec.EXACTLY && widthSize > 0){
        size = widthSize;
    else if(heightMode == MeasureSpec.EXACTLY && heightSize > 0){
        size = heightSize;
        size = widthSize < heightSize ? widthSize : heightSize;

    int finalMeasureSpec = MeasureSpec.makeMeasureSpec(size, MeasureSpec.EXACTLY);
    super.onMeasure(finalMeasureSpec, finalMeasureSpec);

With this implementation, your layout will be square, assuming the lower size between width and height. And it can even be set with dynamic values, like using weight inside a LinearLayout.

How to Use GridLayout (The Java™ Tutorials > Creating a GUI With , Otherwise, if you want to code by hand and do not want to use GroupLayout , then GridBagLayout A GridLayout object places components in a grid of cells. One, but not both, of rows and cols can be zero, which means that any number of� Custom Square LinearLayout. How? Simple way to do dynamic but square layout; Hope to find a solution to this, using onMeasure or any other function when extending the layout (so that even if some extends the Custom Layout, the Square property remains) Edit 1 : For further clarification, the expected result for 1st case is shown

We can do it with a very simple way - just call super.onMeasure() twice.

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    int width = getMeasuredWidth();
    int height = getMeasuredHeight();
    int squareLen = Math.min(width, height);

        MeasureSpec.makeMeasureSpec(squareLen, MeasureSpec.EXACTLY), 
        MeasureSpec.makeMeasureSpec(squareLen, MeasureSpec.EXACTLY));

By calling super.onMeasure() twice, this is less efficient in terms of the drawing process, but it is a simple way to fix layout issues that the other answers can cause.

Tables, Here is a simple three-row, three-column table described in HTML 4: shows how to make a column a fixed size, by using the fixed layout algorithm. The rectangle must be as far to the left as possible, but the part of the cell in the This allows dynamic effects to remove table rows or columns without forcing a re- layout of� The following instructable details 2 ways to find out if something is square, 1 way to draw an accurate perpendicular line, and 1 way to draw an accurate parallel line. These tricks involve virtually no math to do and are scalable to any dimensions from millimetres, to miles.

A quick way to started with this type of layout is with a HTML template. Phoebe is a clean, minimal Blogger template packed with lots of features like an automatic featured posts slider and Promo area (both are optional). 4 home page styles to choose from (slider + promo area, slider only, promo area only or classic layout).

Another way of keeping yourself inspired is checking out articles like this one, for example, where new and interesting website design ideas with cool layouts are featured. You should, in any case, ignore articles with a year-old design or more, as it is a waste of time checking concepts that you know already and you are currently using for

How to Check for Square . In other words, if you wanted to determine if an assembly was square using math, select the face that you want to check for square. Check one corner of the assembly with a framing or combination layout square to make sure that it is a right angle. Then, using a tape measure, measure along one edge from that right angle

  • This is using the measure spec rather than using setMeasuredDimension() so that is good. But it's not very flexible since it always takes the width.
  • As @DavidMerriman said above, if the view is wider than it is tall, this will cause the view to extend below the viewing area, cutting off part of the view.
  • This would be more flexible if it passed the value of Math.min(widthMeasureSpec, heightMeasureSpec) into both arguments of super.onMeasure()
  • This will work easily with activities but it won't work with an app widget.
  • this is a king answer
  • Top answer if you want to display an existing layout as a square !
  • Make sure to set "0dp" for layout width and height in child layout. Otherwise it won't work.
  • It also works for the view itself, I mean if you want a contrantLayout's child to be square, you can define the layout_constraintDimensionRatio attribute directly in the child view
  • Thank you very much for the solution; I've made if it helps a complete gist of my code using your solution:
  • Yeah, I ended up doing something similar. Thanks!