Sizing a custom view based upon the screen size

android, android-layout, drawing, java

Solution

I actually accomplished this today through a little bit of experimentation based upon what Tim said. I moved away from "onMeasure()" and only wrote this code for it:

@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
    // Call the super class.
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    // Set the dimensions of this view.
    this.setMeasuredDimension((int) width, (int) width);
}

Width is a global float. Where does width come from? I derived width from this method that I wrote in the view to resize:

public void initSize(float size)
{
    width = size;
    this.invalidate();
}

I call "invalidate()" to redraw the view. Something has to call this method though, so I do so in the activity. First, I get the size of the screen in the Activity, not the View, using this code:

Display thisDisplay = this.getWindowManager().getDefaultDisplay();
Point desiredSize = new Point();
display.getSize(desiredSize);
int width = size.x;

You used to be able to call a method called "getWidth()" on "thisDisplay" but this method is now depreciated, so you should use "getSize()" instead. Finally, right after you initialize the constructor, call the "initSize()" method:

nameBox = (InfoBoxView) findViewById(R.id.nameBox);
    nameBox.initSize(width);

Now, all the views will be the same size. I don't know if this is the best way to do this, but it works for now and I figured I'd post it here so everyone knew. :)

Problem

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! 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: ``` @Override public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int halfWidth = (MeasureSpec.getSize(widthMeasureSpec) / 2) - 5; this.setMeasuredDimension((int) halfWidth, (int) halfWidth); } ``` 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! 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...) What should 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. 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! :)

Original source

Related problems