Skip to content

Instantly share code, notes, and snippets.

@xingrz
Last active October 12, 2021 21:25
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save xingrz/c95cdedf57f45f60dd28 to your computer and use it in GitHub Desktop.
Save xingrz/c95cdedf57f45f60dd28 to your computer and use it in GitHub Desktop.
An ImageView that always square, matching parent's width
import android.content.Context;
import android.util.AttributeSet;
import android.widget.ImageView;
public class SquareImageView extends ImageView {
public SquareImageView(Context context) {
super(context);
}
public SquareImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public SquareImageView(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();
setMeasuredDimension(width, width);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment