javaandroidanimationheightandroid-linearlayout

Get what the WRAP_CONTENT height would be


My purpose is to have an invisible LinearLayout that appear whit an animation when click on a specific button. To do this i have set the default height to WRAP_CONTENT, get the height when the app start, set the height to 0 and start the animation when i click on the button. Here is the code:

linearLayout.post(new Runnable() {
    @Override
    public void run(){
        height = linearLayout.getMeasuredHeight();
        linearLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, 0));
    }
});


findViewById(R.id.btnOperator).setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Animation ani = new ShowAnim(linearLayout, height/* target layout height */);
        ani.setDuration(1000/* animation time */);
        linearLayout.startAnimation(ani);

    }
});

This work pretty good, but i want to do different. I want that the default height is 0, and then calculate what the WRAP_CONTENT height would be, and pass it to:

Animation ani = new ShowAnim(linearLayout, height/* target layout height */);

How could i do this? I searched but found anything.


Solution

  • Try this code:

    linearLayout.measure(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    height = linearLayout.getMeasuredHeight();