I think the simplest way to do it is to put the contents of CardView
inside a FrameLayout
, as according to the docs:
Child views are drawn in a stack, with the most recently added child on top. The size of the FrameLayout is the size of its largest child (plus padding), visible or not (if the FrameLayout's parent permits). Views that are View.GONE are used for sizing only if setConsiderGoneChildrenWhenMeasuring() is set to true.
Suppose this is the CardView
layout:
<androidx.cardview.widget.CardView>
<!-- your contents -->
</androidx.cardview.widget.CardView>
Change it into:
<androidx.cardview.widget.CardView>
<FrameLayout>
<LinearLayout android:orientation="horizontal">
<View
android:id="@+id/left"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0"
android:background="color1"/>
<View
android:id="@+id/right"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="100"
android:background="color2"/>
</LinearLayout>
<!-- your contents -->
</FrameLayout>
</androidx.cardview.widget.CardView>
And in the code:
findViewById<View>(R.id.left).layoutParams = LinearLayout.LayoutParams(
0, LinearLayout.LayoutParams.MATCH_PARENT, x)
findViewById<View>(R.id.right).layoutParams = LinearLayout.LayoutParams(
0, LinearLayout.LayoutParams.MATCH_PARENT, 100f - x)