title: How do I specify the height of a view relative to its width in Android Studio?
body: <h3>Problem</h3>
  <p>I have a button with the following XML:</p>
  <pre><code>&lt;Button
      android:id=&quot;@+id/firstNumber&quot;
      android:layout_width=&quot;0dp&quot;
      android:layout_height=&quot;wrap_content&quot;
      android:layout_marginLeft=&quot;16dp&quot;
      android:layout_marginStart=&quot;16dp&quot;
      android:layout_marginTop=&quot;16dp&quot;
      android:text=&quot;@string/firstNumber&quot;
      app:layout_constraintEnd_toStartOf=&quot;@+id/secondNumber&quot;
      app:layout_constraintHorizontal_bias=&quot;0.5&quot;
      app:layout_constraintHorizontal_chainStyle=&quot;spread_inside&quot;
      app:layout_constraintStart_toStartOf=&quot;parent&quot;
      app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;
  </code></pre>
  <p>Currently, the button's <code>layout_width</code> is set to <code>match_constraint</code> (which is the reason why it shows as &quot;0dp,&quot; I think).</p>
  <p>I want to make the button a square, where its width would be equal to its height. Here are the solutions that I have found so far:</p>
  <h3>Solution 1:</h3>
  <p>I could do something similar to <a href="https://stackoverflow.com/a/24928215/8624603">this answer</a>, where I would change the XML <code>layout_width</code> and <code>layout_height</code> values to a specific value <code>@dimen/box_size</code>:</p>
  <pre><code>android:layout_width=&quot;@dimen/box_size&quot;
  android:layout_height=&quot;@dimen/box_size&quot;
  </code></pre>
  <p>However, this would not be ideal as it is basically hard-coding the button width, which would not adjust well for different screen sizes (as opposed to using <code>match_constraint</code>).</p>
  <h3>Solution 2:</h3>
  <p>I could write some Java code to do it (<a href="https://stackoverflow.com/a/28846185/8624603">source</a>):</p>
  <pre><code>public class SquareButton extends Button {
  
      public SquareButton(Context context) {
          super(context);
      }
  
      public SquareButton(Context context, AttributeSet attrs) {
          super(context, attrs);
      }
  
      @Override
      protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
          super.onMeasure(widthMeasureSpec, heightMeasureSpec);
          setMeasuredDimension(getMeasuredWidth(), getMeasuredWidth());
      }
  }
  </code></pre>
  <p>This would work, but I'd rather do this within XML. Is there a way?</p>