Search code examples
javaandroidandroid-layoutandroid-viewandroid-framelayout

How to make a Square Frame Layout?


I tried making custom MySquareFrame class by extending android.widget.FrameLayout and override onMeasure mehtod by passing custom width and height.

public class MySquareFrame extends FrameLayout {

public MySquareFrame(Context context) {
    super(context);
}

public MySquareFrame(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public MySquareFrame(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
}

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public MySquareFrame(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    super(context, attrs, defStyleAttr, defStyleRes);
}

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

and used this in xml like this

<com.example.akash.view.MySquareFrame
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background3">
<ImageView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:src="@drawable/fuel_meter"
    android:rotation="120"
    />
<com.triggertrap.seekarc.SeekArc
    android:id="@+id/seekArc"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    seekarc:max="120"
    android:padding="70dp"
    seekarc:rotation="180"
    seekarc:startAngle="30"
    seekarc:sweepAngle="300"
    seekarc:touchInside="false"
    seekarc:clockwise="false"
    seekarc:thumb="@drawable/nob" />
</com.example.akash.view.MySquareFrame>

and what I get Landscape view

So I want MySquareFrame class to look Square in xml. Please help..

--------------------UPDATED-------------------------

DisplayMetrics displaymetrics = new DisplayMetrics();
    ((Activity) getContext()).getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
    int height = displaymetrics.heightPixels;
    int width = displaymetrics.widthPixels;
    int size = width > height ? height : width;
    setMeasuredDimension(size, size);

this helped me to get square frame according to screen dimension.


Solution

  • You can see implementation of SquareFrameLayout from android developers website:

        
    
        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            final int widthSize = MeasureSpec.getSize(widthMeasureSpec);
            final int heightSize = MeasureSpec.getSize(heightMeasureSpec);
    
            if (widthSize == 0 && heightSize == 0) {
                // If there are no constraints on size, let FrameLayout measure
                super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    
                // Now use the smallest of the measured dimensions for both dimensions
                final int minSize = Math.min(getMeasuredWidth(), getMeasuredHeight());
                setMeasuredDimension(minSize, minSize);
                return;
            }
    
            final int size;
            if (widthSize == 0 || heightSize == 0) {
                // If one of the dimensions has no restriction on size, set both dimensions to be the
                // on that does
                size = Math.max(widthSize, heightSize);
            } else {
                // Both dimensions have restrictions on size, set both dimensions to be the
                // smallest of the two
                size = Math.min(widthSize, heightSize);
            }
    
            final int newMeasureSpec = MeasureSpec.makeMeasureSpec(size, MeasureSpec.EXACTLY);
            super.onMeasure(newMeasureSpec, newMeasureSpec);
        }