0

In my android studio project I have a fragment (the class is MainFragment) and a scrollview inside. In MainFragment class I want to get event, when the fragment already layout it's child views. i.e I want to call a method scrollview.getHeight() and not to get 0, because of the system has not yet calculated actual height of scrollview. Is there any solution?

Vahag Chakhoyan
  • 873
  • 1
  • 10
  • 21

2 Answers2

1

Do all the work on @Resume, in that time the fragment is created and the layout has been added so you should have the height

  @Override
  public void onResume() {
    super.onResume();
    // your code here ... 
  }
0

You can make use of the onViewCreated method. It is called when the view is created so you won't be getting 0 height from there.

@Overrride
void onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    scrollView.doOnPreDraw {
        println("Height: "+it.height)
        println("Mesured Height: "+it.measuredHeight)
    }
}
Birju Vachhani
  • 6,072
  • 4
  • 21
  • 43