Note that there are some explanatory texts on larger screens.

plurals
  1. POSizing a custom view based upon the screen size
    text
    copied!<p>I've been trying to find a solution to this, but perhaps I'm not understanding how the "onMeasure()" method works so well in Android!</p> <p>This is the first time I've made custom views "dynamic" in Android, so I learned that you need to override the "onMeasure()" method in order to resize your view depending upon how large the screen is. I would like the my view to be half the width of the screen, plus a little extra (this number is arbitrary, as long as its relatively small, between 3 and 5), so I wrote this code for the onMeasure method:</p> <pre><code>@Override public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int halfWidth = (MeasureSpec.getSize(widthMeasureSpec) / 2) - 5; this.setMeasuredDimension((int) halfWidth, (int) halfWidth); } </code></pre> <p>I'm sure this a terrible way of going about things because everywhere I've found online has so much more code than I do while trying to accomplish something similar!</p> <p>When I place these views next to each other in the XML, this first view is correctly sized (it is indeed half the size of the width minus a little more), but the second view is much smaller, probably half the size of the width of the last view minus a little more! (I would post a picture of it but unfortunately I don't have enough reputation to do so...)</p> <p>What <em>should</em> occur is the two boxes should be the same size, no matter where in the XML I place them. They should all be one-half the size of the width of the screen, minus a little extra.</p> <p>I know it has something to do with how I'm using "setMeasuredDimension()", but I don't know what it is. I really hope someone could clarify things a bit for me! Thanks! :)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload