Vahag Chakhoyan
Vahag Chakhoyan

Reputation: 779

How to get fragment's event, when it layouts it's child layouts?

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?

Upvotes: 0

Views: 80

Answers (2)

Birju Vachhani
Birju Vachhani

Reputation: 6353

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)
    }
}

Upvotes: 0

Granit Berisha
Granit Berisha

Reputation: 86

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 ... 
  }

Upvotes: 1

Related Questions