Square layout not working in landscape

300 views Asked by At

Why is my square view not working in landscape mode?

It has following onMeasure method:

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int width = MeasureSpec.getSize(widthMeasureSpec);
    int height = MeasureSpec.getSize(heightMeasureSpec);
    size = Math.min(width, height);
    setMeasuredDimension(size, size);
}

Here you see the background of my view (in gray) and that my view is not square. Why?

enter image description here

And here's my xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="@dimen/md_dialog_frame_margin">

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="center_horizontal">

        <com.my.package.GridPreviewView
            android:id="@+id/grid"
            android:background="#cccccc"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_above="@+id/npCols"
            android:layout_alignParentTop="true"
            android:layout_marginBottom="8dp"
            android:layout_marginLeft="8dp"
            android:layout_toRightOf="@+id/npRows" />

        <com.shawnlin.numberpicker.NumberPicker
            android:id="@+id/npRows"
            android:layout_width="48dp"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/grid"
            android:layout_alignTop="@+id/grid"
            android:paddingRight="8dp"
            app:np_dividerColor="?attr/colorAccent"
            app:np_orientation="vertical"
            app:np_selectedTextColor="?attr/colorAccent"
            app:np_selectedTextSize="22sp"
            app:np_textSize="18sp"
            app:np_width="48dp" />

        <com.shawnlin.numberpicker.NumberPicker
            android:id="@+id/npCols"
            android:layout_width="wrap_content"
            android:layout_height="48dp"
            android:layout_alignLeft="@+id/grid"
            android:layout_alignParentBottom="true"
            android:layout_alignRight="@+id/grid"
            android:paddingTop="8dp"
            app:np_dividerColor="?attr/colorAccent"
            app:np_height="48dp"
            app:np_orientation="horizontal"
            app:np_selectedTextColor="?attr/colorAccent"
            app:np_selectedTextSize="22sp"
            app:np_textSize="18sp" />

    </RelativeLayout>
</LinearLayout>
1

There are 1 answers

0
Kaushik NP On

Am not exactly sure what the problem may be with the onMeasure() technique you are using, but 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. This will work in every and all scenarios (including view orientation change)

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'
}

Note : The XML preview may be broken for the moment, but it works perfectly fine when running the app.