Custom SquareView - children not behaving with match_parent || wrap_content

215
July 29, 2017, at 1:05 PM

So I needed a view to be always square and I wrote the following custom class

public class SquareView extends LinearLayoutCompat
{
    public SquareView(Context context)
    {
        super(context);
    }
    public SquareView(Context context, AttributeSet attrs)
    {
        super(context, attrs);
    }
    public SquareView(Context context, AttributeSet attrs, int defStyleAttr)
    {
        super(context, attrs, defStyleAttr);
    }
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
    {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int width  = getMeasuredWidth();
        int height = getMeasuredHeight();
        if (width > height)
        {
            setMeasuredDimension(width, width);
        }
        else
        {
            setMeasuredDimension(height, height);
        }
    }
}

However, the children of the view that are using match_parent and wrap_content are no longer behaving correctly

How do I fix this?

Answer 1

This is an answer to your larger problem, not a way to fix your custom view.

ConstraintLayout supports sizing children to have a certain aspect ratio. If you want a square LinearLayout, you can just put it inside a ConstraintLayout and enforce a 1:1 aspect ratio.

https://developer.android.com/training/constraint-layout/index.html#adjust-the-view-size

Set size as a ratio

You can set the view size to a ratio such as 16:9 if at least one of the view dimensions is set to "match constraints" (0dp). To enable the ratio, click Toggle Aspect Ratio Constraint (callout 1 in figure 10), and then enter the width:height ratio in the input that appears.

If both the width and height are set to match constraints, you can click Toggle Aspect Ratio Constraint to select which dimension is based on a ratio of the other. The view inspector indicates which is set as a ratio by connecting the corresponding edges with a solid line.

For example, if you set both sides to "match constraints", click Toggle Aspect Ratio Constraint twice to set the width be a ratio of the height. Now the entire size is dictated by the height of the view (which can be defined in any way) as shown in figure 11.

READ ALSO
how do i open external URL out of my app?

how do i open external URL out of my app?

it is still running within my appCan someone help me ? how do i open external URL out of my app?

196
Old android version updated with encrypted database but it gives no column found

Old android version updated with encrypted database but it gives no column found

I have android android 70 on play store and local server with encrypted database

154
Is there anyway to find out what type of View a View is?

Is there anyway to find out what type of View a View is?

So if I were to have a LinearLayout and had several children Views inside of it, say like a couple of Buttons a TextView and a CheckBox, using the LinearLayout's getChildAt(x) I would then get an unspecified ViewTo note, I'm not using an xml in this so it's all done programatically

126
Getting LiveData updates in multiple processes

Getting LiveData updates in multiple processes

In my Android application I use the Room API to persist data that comes from the networkThe app is split into several processes, with one of them responsible for syncing the data (implementing SyncAdapter)

199