Skip to content Skip to sidebar Skip to footer

Android: Get Dimension Of Extended View Using Addongloballayoutlistener But Fails

I am implementing the following addOnGlobalLayoutListener so as to measure the Extended View (doodleView) in main activity (A)'s OnCreate section. doodleView = (DoodleView) find

Solution 1:

I was doing something very similar, but then I found onMeasure. So if this is essentially a one time thing try doing something similar to this:

protectedvoidonMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    Viewview= cardSlots[1];
    intcardHeight= view.getMeasuredHeight();
    intcardWidth= view.getMeasuredWidth();
    resizeView(playedSlot, cardWidth, cardHeight);
}

staticpublicvoidresizeView(View view, int cardWidth, int cardHeight) {
    view.getLayoutParams().height = cardHeight;
    view.getLayoutParams().width = cardWidth;
    view.invalidate();
    //view.requestLayout();
    ((View)view.getParent()).invalidate();
}

This way you have less to worry about like doing this:

getViewTreeObserver().removeOnGlobalLayoutListener(this);

which you may have forgotten.

Solution 2:

I had the same problem. I modified the function as

doodleView.getViewTreeObserver().addOnGlobalLayoutListener(newViewTreeObserver.OnGlobalLayoutListener() 
  {
        @OverridepublicvoidonGlobalLayout() 
        {
            doodleViewWidth = doodleView.getWidth(); 
            doodleViewHeight = doodleView.getHeight();
        }
  });

all I have done is just added ViewTreeObserver class name before OnGlobalLayoutListener().

Post a Comment for "Android: Get Dimension Of Extended View Using Addongloballayoutlistener But Fails"