Skip to content Skip to sidebar Skip to footer

Android: Set View Height Via Viewtreeobserver

I have two tables: TableA and TableB. TableA has the first row with height of 22 and TableB has the first row with height of 77. I want to equate first row of TableA to first row o

Solution 1:

Assuming your layout is correctly designed and this way of setting height of your textViewB is the one you really wanna go with...

You should remove OnGlobalLayoutListener as soon as it's not needed anymore. You're not doing that, and the onGlobalLayout callback is getting called on any change in the ViewTree layout. So answering your question: the way you're using ViewTreeObserver is not the best...

This way would be better:

voidresizeHeaderHeight() {
    TableRowTableA_Row= (TableRow) this.tableA.getChildAt(0);
    TableRowTableB_Row= (TableRow) this.tableB.getChildAt(0);

    finalTextViewtextViewA= (TextView) TableA_Row.getChildAt(0);
    finalTextViewtextViewB= (TextView) TableB_Row.getChildAt(0);

    textViewB.getViewTreeObserver().addOnGlobalLayoutListener(newViewTreeObserver.OnGlobalLayoutListener() {
        @OverridepublicvoidonGlobalLayout() {
            intheightB= textViewB.getHeight();
            if (heightB > 0) {
                // removing OnGlobalLayoutListenerif (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
                    textViewB.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                } else {
                    textViewB.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                }

                // setting height
                textViewA.setHeight(heightB);
            }
        }
    });
}

Post a Comment for "Android: Set View Height Via Viewtreeobserver"