Search code examples
androidandroid-4.3-jelly-beanandroid-4.2-jelly-bean

Android Jelly Bean MeasureSpec bug


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.


Solution

  • 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);
    }