Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This is good practice, to make square image? Side of the square container is equal to its width.

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, widthMeasureSpec);
    }
}
share|improve this question
up vote 1 down vote accepted

If your project needs it - it's good. I did the same thing some time ago and you must remember that sometimes widthMeasureSpec or heightMeasureSpec can be very small, so I suggest that you check those values, choose the bigger one and then call super.onMeasure().

@Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
      if (widthMeasureSpec < 1) {
          super.onMeasure(heightMeasureSpec, heightMeasureSpec);
      } else {
          super.onMeasure(widthMeasureSpec, widthMeasureSpec);
      }
    }
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.