I got a problem when trying to update dynamically the layoutParams of a view.
The view to update is a ConstraintLayout, and I want to dynamically change it's app:layout_constraintDimensionRatio property.
The fragment XML :
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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:context=".LevelFragment">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/board"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="32dp"
android:layout_marginEnd="32dp"
android:layout_marginTop="32dp"
android:layout_marginBottom="32dp"
android:background="@color/black"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
</androidx.constraintlayout.widget.ConstraintLayout>
</FrameLayout>
But when I try to change it from Kotlin fragment code
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
board = view.findViewById(R.id.board)
// Set ratio
val layoutParams = board!!.layoutParams as ConstraintLayout.LayoutParams
layoutParams.dimensionRatio = "5:1"
board!!.layoutParams = layoutParams
}
It fails with this error after debug build :
android.widget.FrameLayout$LayoutParams cannot be cast to androidx.constraintlayout.widget.ConstraintLayout$LayoutParams
So, I'm wondering why it complains about FrameLayout to ConstraintLayout cast, because the layoutParams is taken from the board View, which is a ConstraintLayout...
Does the paramsLayout refers to the parent view and not the view itself ?
And if so, how to update view dimensionRatio property ?
Thanks !
LayoutParamsare the parameters which parentViewGroupuses to layout its children. Each child has its ownLayoutParamswhich has a specific type based on type of its parent. i.e. children ofFrameLayouthasFrameLayout.LayoutParamsas theirLayoutParamsand children ofLinearLayouthasLinearLayout.LayoutParamsas theirLayoutParamsand so on. Also they can not get cast to each other, it means you can not castLinearLayout.LayoutParamstoFrameLayout.LayoutParamssince they have different implementation ofLayoutParams. But all ofLayoutParamshave extededViewGroup.LayoutParams, so it is safe to cast them toViewGroup.LayoutParams.In your case you are casting
board.layoutParamstoConstraintLayout.LayoutParamsbut since parent ofboardis aFrameLayoutthen itsLayoutParamsis of typeFrameLayout.LayoutParamsand can not get cast toConstraintLayout.LayoutParams.If you want to fix this you have to replace parent of
boardwhich is aFrameLayoutwith aConstraintLayout.Also you can read here if you want to see how
LayoutParamsworks.