9

I call getHeight and getWidth in a custom view in the onDraw() method. getHeight() returns 1,073,742,549 and getWidth() returns 1,073,742,304. However, when I look at the display metrics for the screen's height and width, I get 800 and 480 respectively. What is getHeight and getWidth returning?

I'm getting the dimensions of my view so I can choose the dimensions of a bitmap that'll go in the view.

Thanks.

Sam R.
  • 16,027
  • 12
  • 69
  • 122
Jays
  • 145
  • 3
  • 8
  • I'm new on Android, but I have similar experience. It has to do something with the Android version, since the same project works fine on 4.1, but has this strange behavior on 2.3.6 Maybe some compatibility settings are missing. –  May 25 '13 at 19:49
  • Are you perhaps also overriding onMeasure in your custom view and calling setMeasuredDimensions with the value of a MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY). I believe the values you are getting would be returned from makeMeasureSpec with MeasureSpec.EXACTLY and the dimensions you have specified. – John Bowers Nov 17 '15 at 20:26

4 Answers4

11

Doesn't explicitly solve you're problem, but curiously:

Width:
1,073,742,304 == 0x400001E0
          480 == 0x000001E0

Height:
1,073,742,549 == 0x400002D5
          725 == 0x000002D5

Any chance this custom view is roughly 725 x 480? I have no idea where that extra 0x40000000 came from, but it looks like it could be almost right except for 1 bit :)

Tim
  • 35,413
  • 11
  • 95
  • 121
  • 2
    Thanks. That's very weird. So I'm anding the width and height against 0xbfffffff to get the right dimensions, which works. – Jays Apr 24 '12 at 22:54
  • 2
    @Jays, I looked at this again, and I think the safest thing to do might be `View.MeasureSpec.getSize(getWidth())` (and height also). The measurespec can signal other things inside that top character, and if something changes it might bite you later (say if it reports 0x8000xxxx). – Tim Apr 24 '12 at 23:04
5

getHeight() and getWidth() return the size in pixels see http://developer.android.com/reference/android/view/View.html#getHeight%28%29

The extra size is likely to be coming from the View.MeasureSpec constant EXACTLY see http://developer.android.com/reference/android/view/View.MeasureSpec.html#getSize%28int%29

EdChum
  • 376,765
  • 198
  • 813
  • 562
3

Maybe, you did something like this in your custom view.

final int desiredHSpec = MeasureSpec.makeMeasureSpec(pixelHeight, MeasureSpec.MODE_CONSTANT);
final int desiredWSpec = MeasureSpec.makeMeasureSpec(pixelWidth, MeasureSpec.MODE_CONSTANT);
setMeasuredDimension(desiredWSpec, desiredHSpec);

like this Android:Why after override onMeasure() in a custom view, the view's text can't show in RalativeLayout?

If you change it into

setMeasuredDimension(width, height);

, you will get simple small width, height on your onSizeChanged() and onLayout().

Community
  • 1
  • 1
19 Lee
  • 108
  • 6
0

I also had this problem. The way I solved it was overriding the onMeasure method. This way the view always has the same size. For example, If you want the view to have the size and width of the screen you should do something like this:

    @Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
    {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        WindowManager wm = (WindowManager)this.getContext().getSystemService(Context.WINDOW_SERVICE);
        Display display = wm.getDefaultDisplay();
//display.getWidth() and display.getHeight() return the screen dimensions
        this.setMeasuredDimension(display.getWidth(), display.getHeight());
    }
jmarhuen
  • 91
  • 1
  • 2