I am trying to find out the height of a certain layout in my fragment's layout as soon as it is created because I want to use it in an animation to make it pop up on click events. These are the two animations:
private void hideEditSetFragment() {
TranslateAnimation animate = new TranslateAnimation(
0, // fromXDelta
0, // toXDelta
0, // fromYDelta
mEditSetContainer.getHeight()); // toYDelta
animate.setDuration(500);
// animate.setFillAfter(true);
TranslateAnimation animate1 = new TranslateAnimation(0,0,mBottomNavigationView.getHeight(),0);
animate1.setDuration(500);
// animate1.setFillAfter(true);
mBottomNavigationView.startAnimation(animate1);
mEditSetContainer.startAnimation(animate);
mBottomNavigationView.setVisibility(View.VISIBLE);
mEditSetContainer.setVisibility(View.GONE);
}
private void showEditSetFragment() {
mEditSetContainer.setVisibility(View.INVISIBLE);
Log.i(TAG, "showEditSetFragment(): mEditSetContainer height is " + mEditSetContainer.getMeasuredHeight());
TranslateAnimation animate = new TranslateAnimation(
0, // fromXDelta
0, // toXDelta
mEditSetContainer.getHeight(), // fromYDelta
0); // toYDelta
animate.setDuration(1000);
//animate.setFillAfter(true);
TranslateAnimation animate1 = new TranslateAnimation(0,0,0,mBottomNavigationView.getHeight());
animate1.setDuration(1000);
//animate1.setFillAfter(true);
mBottomNavigationView.startAnimation(animate1);
mEditSetContainer.startAnimation(animate);
mBottomNavigationView.setVisibility(View.INVISIBLE);
mEditSetContainer.setVisibility(View.VISIBLE);
}
The problem is that my mEditSetContainer
needs to be drawn at least once to be able to be used in this animation as I use its height. So, during onCreateView()
, I set it to visible:
@Override
public View onCreateView(LayoutInflater inflater, final ViewGroup container, Bundle onSavedInstanceState) {
Log.i(TAG, "onCreateView()");
View v = inflater.inflate(R.layout.fragment_current_workout, container, false);
v.findViewById(R.id.fragment_current_workout_recycler_view);
mPerformedExercisesRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
mEditSetContainer = (ConstraintLayout) v.findViewById(R.id.fragment_current_workout_edit_set_container);
mEditSetFragment = new EditSetFragment();
getChildFragmentManager().beginTransaction().replace(R.id.fragment_current_workout_edit_set_container, mEditSetFragment, "EditSetFragment").commit();
mEditSetContainer.setVisibility(View.VISIBLE);
mEditSetContainer.post(new Runnable() {
@Override
public void run() {
Log.i(TAG, "Runnable called " + String.valueOf( mEditSetContainer.getHeight()));
Log.i(TAG, "Runnable called " + String.valueOf( mBottomNavigationView.getHeight()));
mEditSetContainer.setVisibility(View.GONE);
}
});
return v;
}
I used a runnable here as it is supposed to be called after being draw according to this thread. However, the view is still not drawn for the mEditSetContainer
when the view for the mBottomNavigationView
is. It is to note that the mEditSetContainer
has its height defined by wrap-content and that it is a container for a nested fragment. My question is how can I get its height when the fragment is created? I need to know this to be able to run my animation. What currently happens is that on the first click, the animation is skipped but it works after. This is also a related thread that could help, but hasn't.
Also, please do not suggest setting my views to invisible. I need to have them set to gone as they are not supposed to take room when not in use. The mEditSetFragment
overlaps other content when shown.