Using lparams in CardView's direct children causes ClassCastException #269
Description
Hi,
I have a DSL similar to the following:
cardView {
cardElevation = dip(6).toFloat()
useCompatPadding = true
relativeLayout {
textView {
text = "TESTING"
}.lparams { margin = dip(8) }
}.lparams { width = matchParent } //Problematic line
}.lparams { width = matchParent ; centerInParent() }
But this throws the following exception:
java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams cannot be cast to android.widget.FrameLayout$LayoutParams
at android.widget.FrameLayout.onMeasure(FrameLayout.java:195)
at android.support.v7.widget.CardView.onMeasure(CardView.java:211)
at android.view.View.measure(View.java:18788)
Because of this line https://android.googlesource.com/platform/frameworks/support/+/master/v7/cardview/src/android/support/v7/widget/CardView.java#211 that calls super.onMeasure(widthMeasureSpec, heightMeasureSpec);
which in turn calls https://chromium.googlesource.com/android_tools/+/master/sdk/sources/android-23/android/widget/FrameLayout.java#195 or final LayoutParams lp = (LayoutParams) child.getLayoutParams();
Here, the RelativeLayout.LayoutParams
is typecasted to FrameLayout.LayoutParams
which causes the exception. IIRC, the LayoutParams
is choosen by Anko depending on the parent which should have been FrameLayout.LayoutParams
for a CardView but is chosen as RelativeLayout.LayoutParams
. For the time being, I've found a workaround which is given below.
cardView {
cardElevation = dip(6).toFloat()
useCompatPadding = true
relativeLayout {
textView {
text = "TESTING"
}.lparams { margin = dip(8) }
}.layoutParams = FrameLayout.LayoutParams( //Explicitly create a FrameLayout.LayoutParams
FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT
)
}.lparams { width = matchParent ; centerInParent() }
I'd like to know if this is a bug in Anko or if I'm doing something wrong. Please advice.