Setting app:layout_behavior programmatically

Explanation

Behavior is a parameter of the CoordinatorLayout.LayoutParams. You can set the behavior on an instance of CoordinatorLayout.LayoutParams with setBehavior method.

To get a proper Behavior object that represents the same thing as @string/appbar_scrolling_view_behavior you should create an instance of AppBarLayout.ScrollingViewBehavior.


Example

(this is a cleaned up version of my previous edits to the original answer)

First you will have to get an instance of a child View of your CoordinatorLayout.
Let me get this clear: it is NOT the CoordinatorLayout itself. childView is CoordinatorLayout's child.

//e.g. like this:
val childView: View = findViewById(R.id.child_view)

Assuming the childView is already attached to the CoordinatorLayout (so it already has LayoutParams), you can do:

val params: CoordinatorLayout.LayoutParams = yourView.layoutParams as CoordinatorLayout.LayoutParams
params.behavior = AppBarLayout.ScrollingViewBehavior()
yourView.requestLayout()

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)