Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.lang.reflect.Method;

import android.view.View;
import android.view.View.MeasureSpec;

public class Main {
    /**
     * Measures the height of the changeView based on the size 
     * of the the view that should be expand, and returns it.
     * 
     * The two parameters are often the same (e.g. in case of a TextView,
     * it's content changes, so it should be measured again, 
     * and it will be the one that will be applied the animation to as well.
     * 
     * TODO: Add listener support for applied animation!
     */
    private static int measureViewHeight(final View viewToExpand, final View changedView) {
        try {
            final Method m = changedView.getClass().getDeclaredMethod("onMeasure", int.class, int.class);
            m.setAccessible(true);
            m.invoke(changedView, MeasureSpec.makeMeasureSpec(viewToExpand.getWidth(), MeasureSpec.AT_MOST),
                    MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
        } catch (Exception e) {
            return -1;
        }
        int measuredHeight = changedView.getMeasuredHeight();
        return measuredHeight;
    }
}