Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get view height when part of the view is off screen?

Let's say I have a view structure like

<ConstraintLayout>
    <CustomViewLayout>
        ...
        ...
    </CustomViewLayout>
</ConstraintLayout>

This is simplified but I use the above in a bottom sheet and I sometimes change the height of the ConstraintLayout and the peek height of the bottom sheet depending on my CustomViewLayout height. My problem is that if part of the CustomViewLayout is cut off, that is - it is somewhat outside the screen because the ConstraintLayout isn't high enough - I'm no longer able to get a correct "full height" of it. I always only seem to get the visible part on screen of the view in that case.

So how can I get the full height of a view that is partly off screen?

Thanks!

Edit:

I should add that what I've tried is a globalLayoutListener, as well as a customViewLayout.post {} (and the usual customViewLayout.height of course). But none of these measure the full height when part of the view is outside the screen.

like image 346
Algar Avatar asked Oct 06 '18 06:10

Algar


1 Answers

To check my previous answer, I developed a test project to examine the exact situation. It was successful to measure layout height when some part of it is outside of screen (layout height measured 4231px where the screen height was 1920px). If the view is long enough that is not completely shown by the screen, you should put it in a scrollable view. So I put the customViewLayout in a NestedScrollView to consume residual scroll amount after the bottom sheet has expanded. Here is the code and result:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:showIn="@layout/activity_main"
    tools:context=".MainActivity">

    <android.support.constraint.ConstraintLayout
        android:id="@+id/layoutBottomSheet"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="android.support.design.widget.BottomSheetBehavior">

        <android.support.v4.widget.NestedScrollView
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <LinearLayout
                android:id="@+id/customViewLayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:background="@color/colorAccent">

                <TextView
                    android:id="@+id/stateTextView"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textColor="#FFFFFF"
                    android:layout_marginTop="16dp"
                    android:gravity="center_horizontal"/>

                <TextView
                    android:id="@+id/sizeTextView"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textColor="#FFFFFF"
                    android:padding="24dp"
                    android:gravity="center_horizontal"/>

                <TextView
                    android:id="@+id/contentTextView"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textColor="#FFFFFF"
                    android:padding="24dp"
                    android:text="@string/lorem"
                    android:gravity="center_horizontal"/>

            </LinearLayout>

        </android.support.v4.widget.NestedScrollView>

    </android.support.constraint.ConstraintLayout>

</android.support.design.widget.CoordinatorLayout>

MainActivity:

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        setSupportActionBar(toolbar)

        customViewLayout.viewTreeObserver.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener {
            override fun onGlobalLayout() {
                customViewLayout.viewTreeObserver.removeGlobalOnLayoutListener(this)
                val height = customViewLayout.measuredHeight
                val width = customViewLayout.measuredWidth
                sizeTextView.text = "Height: $height px                Width: $width px"
            }
        })

        val sheetBehavior = BottomSheetBehavior.from(layoutBottomSheet)
        sheetBehavior.setBottomSheetCallback(object : BottomSheetBehavior.BottomSheetCallback() {
            override fun onSlide(bottomSheet: View, offset: Float) {
            }

            override fun onStateChanged(bottomSheet: View, newState: Int) {
                when (newState) {
                    BottomSheetBehavior.STATE_HIDDEN -> stateTextView.text = "State: Hidden"
                    BottomSheetBehavior.STATE_EXPANDED -> stateTextView.text = "State: Expanded"
                    BottomSheetBehavior.STATE_COLLAPSED -> stateTextView.text = "State: Collapsed"
                    BottomSheetBehavior.STATE_DRAGGING -> stateTextView.text = "State: Dragging"
                    BottomSheetBehavior.STATE_SETTLING -> stateTextView.text = "State: Settling"
                }
            }
        })
    }

}

Screen:

enter image description here

like image 195
aminography Avatar answered Sep 21 '22 09:09

aminography