Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minimum and Maximum sizes for square layout #53

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package com.example.android.activityscenetransitionbasic;

import android.content.Context;
import android.content.res.TypedArray;
import android.os.Build;
import android.util.AttributeSet;
import android.widget.FrameLayout;

Expand All @@ -25,6 +27,8 @@
*/
public class SquareFrameLayout extends FrameLayout {

private int mMaxWidth, mMaxHeight;

public SquareFrameLayout(Context context) {
this(context, null);
}
Expand All @@ -35,6 +39,24 @@ public SquareFrameLayout(Context context, AttributeSet attrs) {

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

TypedArray a = context.getTheme().obtainStyledAttributes(
attrs, R.styleable.SquareFrameLayout, 0, 0
);
try {
mMaxWidth = a.getDimensionPixelSize(R.styleable.SquareFrameLayout_maxWidth, Integer.MAX_VALUE);
mMaxHeight = a.getDimensionPixelSize(R.styleable.SquareFrameLayout_maxHeight, Integer.MAX_VALUE);
} finally {
a.recycle();
}
}

public int getMaximumWidth() {
return mMaxWidth;
}

public int getMaximumHeight() {
return mMaxHeight;
}

@Override
Expand All @@ -52,7 +74,7 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
return;
}

final int size;
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
Expand All @@ -63,6 +85,17 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
size = Math.min(widthSize, heightSize);
}

// bound the size by the view's min and max dimensions
final int maxSize = Math.min(getMaximumWidth(), getMaximumHeight());
if (size > maxSize) {
size = maxSize;
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
final int minSize = Math.max(getMinimumWidth(), getMinimumHeight());
if (size < minSize) {
size = minSize;
}
}

final int newMeasureSpec = MeasureSpec.makeMeasureSpec(size, MeasureSpec.EXACTLY);
super.onMeasure(newMeasureSpec, newMeasureSpec);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
-->

<ScrollView 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">

Expand All @@ -26,7 +27,9 @@

<com.example.android.activityscenetransitionbasic.SquareFrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
android:layout_height="wrap_content"
android:minHeight="100dp"
app:maxHeight="400dp">

<ImageView
android:id="@+id/imageview_header"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="SquareFrameLayout">
<attr name="maxWidth" format="dimension" />
<attr name="maxHeight" format="dimension" />
</declare-styleable>
</resources>