4

I was having this problem: https://stackoverflow.com/questions/20121696/slidingmenu-bug-in-android-4-3
But now I've fixed and I want to share my solution 'cause probably someone will need it too.
I'll answer this question myself bellow.

Community
  • 1
  • 1
GuilhE
  • 11,591
  • 16
  • 75
  • 116
  • I faced this issue too http://stackoverflow.com/questions/28798288/getmeasuredheight-returns-16777215-on-android-4-2-2/28803819?noredirect=1#comment45881409_28803819 – Madhur Ahuja Mar 02 '15 at 06:50

1 Answers1

6

So the problem I was having lies on the fact that Build.VERSION_CODES.JELLY_BEAN_MR2 has a problem when we want to create a MeasureSpec:

MeasureSpec.makeMeasureSpec(ViewGroup.LayoutParams..., MeasureSpec.EXACTLY);

With MeasureSpec.EXACTLY when I perform for example a .measure(widthMeasureSpec, heightMeasureSpec); it returns values completely strange, so this problem can be solved if we use MeasureSpec.AT_MOST instead of MeasureSpec.EXACTLY.


Hope it helps someone in the future ;)
ps: I don't know if Android Kitkat (4.4, API 19) has this problem too.

EDIT: it does.

int widthMeasureSpec;
int heightMeasureSpec;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
    widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(ViewGroup.LayoutParams.MATCH_PARENT, View.MeasureSpec.AT_MOST);
    heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(ViewGroup.LayoutParams.WRAP_CONTENT, View.MeasureSpec.AT_MOST);
} else {
    widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(ViewGroup.LayoutParams.MATCH_PARENT, View.MeasureSpec.EXACTLY);
    heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(ViewGroup.LayoutParams.WRAP_CONTENT, View.MeasureSpec.EXACTLY);
}
GuilhE
  • 11,591
  • 16
  • 75
  • 116
  • whats a point of passing WRAP_CONTENT as a first param to nakeMeasureSpec? – pskink Nov 22 '13 at 15:32
  • @pskink, well because in my XML I've declared layout_width="wrap_content" and layout_height="match_parent", and I think (not 100% sure on this) when I create a MeasureSpec I have to use the same setup. – GuilhE Nov 22 '13 at 21:27
  • or if you dont want to analize the source code create a custom view set layout_width to "wrap_content", override onMeasure and log MeasureSpec.toString() passing the first param of onMeasure – pskink Nov 22 '13 at 21:59
  • Kitkat and Lollipop do not have this issue. – Madhur Ahuja Mar 02 '15 at 06:50