0

Problem description:
I need to set the height of a ScrollView equal to n times the height of an inner horizontal LinearLayout, inside onCreateView() and in a fragment.
I dynamically add ImageView's inside the LinearLayout, so the height is not fixed.

Possible solutions:
I’ve been doing some research and there seems to be at least two solutions that are repeated:

  1. Using a Runnable (link).
  2. Using a ViewTreeObserver (link).

Is there any other more elegant/simpler/correct way to tackle this in 2020?
In case there isn’t, which way do you suggest me to go?

Basics:
In onCreateView() I’m able to assign any value to the height of the ScrollView, and that works fine.

override fun onCreateView(...) {
//Some other unrelated code

binding.MyScrollView.layoutParams.height = 100

}

..but unfortunately I don't have a fixed value.

I need to retrieve the LinearLayout's height, but as you know, it's not created yet in onCreateView().

val layoutHeight = binding.MyLinearLayout.height //returns 0

And that is what I need to solve.

Just in case, I’m using Kotlin.

MatJB
  • 106
  • 2
  • 8

0 Answers0