Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LinearLayout in Square Form

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

like image 762
Maffo Avatar asked Apr 27 '11 11:04

Maffo


People also ask

What is LinearLayout?

LinearLayout is a view group that aligns all children in a single direction, vertically or horizontally. You can specify the layout direction with the android:orientation attribute. Note: For better performance and tooling support, you should instead build your layout with ConstraintLayout.

What is gravity in linear layout?

XML attributes Drawable to use as a vertical divider between buttons. android:gravity. Specifies how an object should position its content, on both the X and Y axes, within its own bounds.

How do you make a linear layout border?

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml. In the above code we have taken one text view with background as border so we need to create a file in drawable as boarder.


1 Answers

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.

like image 127
Mojo Risin Avatar answered Oct 07 '22 08:10

Mojo Risin