Any help on how to programatically set scrollFlags to linear layout. I can add it to xml but need to change it programatically
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<LinearLayout
android:id="@ id/scroll_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
CodePudding user response:
I don't think it does have what you need, but however I'm gonna share what I know. In order to do it programmatically you have to do two ways that most common things that every android dev does. I recommend you using the following and I'll send you the documentation of it.
Documentation for the LinearLayout what Methods, Attributes, Constructors and etc. is using: https://developer.android.com/reference/android/widget/LinearLayout
View Binding Documentation: https://developer.android.com/topic/libraries/view-binding?hl=en
<!--Add the id to identify what element you want to use-->
<LinearLayout
android:id="@ id/linear_layout_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:layout_scrollFlags="scroll||exitUntilCollapsed">
The old way of initialize the LinearLayout is this:
private lateinit var linearLayoutView: LinearLayout
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
linearLayoutView = findViewById(R.id.linear_layout_view)
//This is an example, will send a documentation for full attributes.
linearLayoutView.scrollBarStyle
}
The new way of doing is using the View Binding, instead of instantiate something from a view and then getting the id, it's less code and spend time:
In app-level module gradle, you add this feature in android:
android {
buildFeatures {
viewBinding = true
}
}
Then the code will look like this:
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(LayoutInflater.from(this))
setContentView(binding.root)
binding.linearLayoutView.scrollBarStyle
}
CodePudding user response:
Solution
private fun enableDeviceScroll(enable: Boolean) {
val params = scrollView.layoutParams as AppBarLayout.LayoutParams
if(enable){
params.scrollFlags = (AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL)
}else{
params.scrollFlags = (AppBarLayout.LayoutParams.WRAP_CONTENT)
}
}