0

I have two fragments. In fragment A there is a RecyclerView, when I click on the element of which I get to another fragment B. In fragment A I have a MaterialToolbar which can have different heights, so in onViewCreated() I call materialToolbar.height to get its height. When I open fragment A everything works fine, however when I click on an element in the RecyclerView, I get to fragment B, in which on pressing the back button I will go back to the fragment A and the height of my materialToolbar will already be 0. Why does it return 0? And how can I make it return the value I need?

onesector
  • 361
  • 5
  • 18
  • Check this, i think that for your particular problem the best option is to use. View.getMeasuredHeigth() https://stackoverflow.com/questions/3591784/views-getwidth-and-getheight-returns-0 – Agustin Pazos Nov 16 '22 at 15:00
  • Can you please share some code for us to understand. Especially where you set toolbar height in fragment A. – Mert Nov 16 '22 at 15:24

1 Answers1

0

There's an observer which listens to view-heirarchy of any given view.

toolbar.viewTreeObserver.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener {
        override fun onGlobalLayout() {
            binding.mbSelect.viewTreeObserver.removeOnGlobalLayoutListener(this)
            val height = materialToolbar.height
        }
    })

Try this above code in you fragment A, you will get the actual height toolbar

Sohaib Ahmed
  • 1,990
  • 1
  • 5
  • 23