LinearLayout in Square Form
Asked Answered
A

3

7

is it possible to have a LinearLayout inside a LinearLayout with equal height and width dynamically? i don't want to specify the values, just that the height is the same size of the possible width.

thx

Amosamount answered 27/4, 2011 at 11:3 Comment(0)
M
16

I have the same problem and i couldn't find a way to solve this using only xml. So i wrote custom layout and reference it from xml.

public class SquareLayout extends LinearLayout {

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, widthMeasureSpec);
            // or you can use this if you want the square to use height as it basis
            // super.onMeasure(heightMeasureSpec, heightMeasureSpec); 
    }
}

and reference it in xml like this

<your.package.SqureLayout .....
</your.package.SquareLayout>

If there is easiest solution i'll be glad to know it.

Mcginnis answered 27/4, 2011 at 11:11 Comment(3)
Are there any cases in which this easiest solution will not work, and will require more complex solutions such as the answer by Alex North, or the Android implementation of [SquareFrameLayout] (androidxref.com/5.1.1_r6/xref/development/samples/browseable/…) ?Mythology
@Mojo Risin, there is an easier solution now. Check out SquareLayout : https://mcmap.net/q/1414260/-linearlayout-in-square-formKeenan
And it also provides auto adjustment depending on which of the two dimensions (height/width) is smallerKeenan
U
2

Expanding Mojo's answer a bit to handle either height or width being the constrained dimension according to context:

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int width = MeasureSpec.getSize(widthMeasureSpec);
    int height = MeasureSpec.getSize(heightMeasureSpec);
    int size = Math.min(width, height);
    // Call super with adjusted spec
    super.onMeasure(MeasureSpec.makeMeasureSpec(size, MeasureSpec.EXACTLY),
            MeasureSpec.makeMeasureSpec(size, MeasureSpec.EXACTLY));
}
Unnumbered answered 21/10, 2013 at 14:40 Comment(1)
When is this required, and Mojo Risin's solution is not enough?Mythology
K
0

Check out SquareLayout, an Android Library which provides a wrapper class for different Layouts, rendering them Squared dimensioned without losing any core functionalities.

The dimensions are calculated just before the Layout is rendered, hence there is no re-rendering or anything as such to adjust once the View is obtained.

To use the Library, add this to your build.gradle:

repositories {
    maven {
        url "https://maven.google.com"
    }
}

dependencies {
    compile 'com.github.kaushikthedeveloper:squarelayout:0.0.3'
}

Your XML will look like :

<!-- Inner Linear Layout -->   
<com.kaushikthedeveloper.squarelayout.SquareLinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
/>
Keenan answered 10/9, 2017 at 16:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.