1

My setup like this:

<androidx.coordinatorlayout.widget.CoordinatorLayout
    android:id="@+id/coordinatorLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RadioGroup
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center">

        <RadioButton
            android:id="@+id/scroll_up_radioButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Scroll Up" />

        <RadioButton
            android:id="@+id/scroll_down_radioButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Scroll Down" />

    </RadioGroup>

    <TextView
        android:id="@+id/bottom_textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="16dp"
        android:text="Botton TextView"
        android:textColor="@color/black"
        android:textSize="30sp"
        android:gravity="center"
        android:layout_gravity="bottom"
        app:layout_behavior="com.google.android.material.behavior.HideBottomViewOnScrollBehavior"/>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

How to use code to make the bottom TextView hide like this? I have tried something but didn't work:

binding.scrollUpRadioButton.setOnCheckedChangeListener { compoundButton, isChecked ->
    if (isChecked) {
        val params = binding.bottomTextView.layoutParams as CoordinatorLayout.LayoutParams

        params.behavior = AppBarLayout.ScrollingViewBehavior()

        binding.bottomTextView.requestLayout()
    }
}
Sam Chen
  • 7,597
  • 2
  • 40
  • 73

1 Answers1

1

The HideBottomViewOnScrollBehavior has the below public methods to hide or show a View programmatically:

public void slideDown (V child) - Perform an animation that will slide the child from it's current position to be totally off the screen.

and

public void slideUp (V child) - Perform an animation that will slide the child from it's current position to be totally on the screen.

So based on your example you can show/hide the bottom TextView programmatically using the below helpers:

private fun showBottomTextView(bottomTextView: TextView){
    val params = bottomTextView.layoutParams as CoordinatorLayout.LayoutParams
    val behavior = params.behavior as HideBottomViewOnScrollBehavior
    behavior.slideUp(bottomTextView)
}

private fun hideBottomTextView(bottomTextView: TextView){
    val params = bottomTextView.layoutParams as CoordinatorLayout.LayoutParams
    val behavior = params.behavior as HideBottomViewOnScrollBehavior
    behavior.slideDown(bottomTextView)
}
MariosP
  • 8,300
  • 1
  • 9
  • 30