Add relative location RelativeLayout.BELOW


#1

I’ve the below code, in which I construct a “card” and adding to it both Switch and button, I need the button to be exactly under the switch, how to make it!

val swithy = Switch(this).apply {
    text = "active"
    isChecked = true
    id = View.generateViewId()
}

val mcard = CardView(this).apply {
    background = getDrawable(R.drawable.card_background)
    radius = 12F
    setContentPadding(25, 25, 25, 25)
    setCardBackgroundColor(Color.LTGRAY)
    cardElevation = 8F
    maxCardElevation = 12F
    addView(swithy, RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
            LayoutParams.WRAP_CONTENT).apply {
        RelativeLayout.ALIGN_PARENT_BOTTOM
        RelativeLayout.ALIGN_PARENT_LEFT
    })
    addView(Button(this.context).apply {
        text = "click me"
    }, LayoutParams(LayoutParams.WRAP_CONTENT,
            LayoutParams.WRAP_CONTENT).apply {
   /*     addRule(RelativeLayout.BELOW, swithy.id);  */
   // How to make this?!  it works in Java, how to make it in Kotlin
    })
}

card_background.xml is:

<?xml version="1.0" encoding="UTF-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle"
    android:name="custom_view"
    android:id="@+id/custom_view">
    <stroke
        android:width="1dp"
        android:color="#c2f2f2f2" />
    <solid
        android:color="#FFFFFFFF"
        />

    <corners
        android:bottomRightRadius="2dp"
        android:bottomLeftRadius="2dp"
        android:topLeftRadius="2dp"
        android:topRightRadius="2dp" />
</shape>

#2

This is a question about the use of the Android framework, not about Kotlin. Please ask it in a different forum, for example on StackOverflow.