0

The purpose of the following function (in an Android app) is to programmatically lay out three vertical SeekBar objects one beside the other.

The setting for the constaints is a bit tricky, but the most difficult part for me here is to get the verticality of the bars. I use rotation = 270.0F, but the rotation is ignored as soon as I set constaints up. Can anybody tell me why this is happening?

fun setAdjustBars() {
    val clrSetBarR = SeekBar(this)
    val clrSetBarG = SeekBar(this)
    val clrSetBarB = SeekBar(this)

    clrSetBarR.thumbTintList = ColorStateList.valueOf(Color.rgb(255,0,0))
    clrSetBarG.thumbTintList = ColorStateList.valueOf(Color.rgb(0,255,0))
    clrSetBarB.thumbTintList = ColorStateList.valueOf(Color.rgb(0,0,255))

    val sideColrVal = 77
    clrSetBarR.progressTintList = ColorStateList.valueOf(Color.rgb(255,sideColrVal,sideColrVal))
    clrSetBarG.progressTintList = ColorStateList.valueOf(Color.rgb(sideColrVal,255,sideColrVal))
    clrSetBarB.progressTintList = ColorStateList.valueOf(Color.rgb(sideColrVal,sideColrVal,255))

    val seekBarsArray = arrayOf(clrSetBarR,clrSetBarG,clrSetBarB)
    seekBarsArray.map {
        it.id = View.generateViewId()
        it.rotation = 270.0F
        constraintLayout?.addView(it)
        constrSet.connect(it.id, ConstraintSet.TOP, deviceFrameID, ConstraintSet.TOP)
        constrSet.connect(it.id, ConstraintSet.BOTTOM, deviceFrameID, ConstraintSet.BOTTOM)
        constrSet.connect(it.id, ConstraintSet.LEFT, deviceFrameID, ConstraintSet.LEFT)
        constrSet.connect(it.id, ConstraintSet.RIGHT, deviceFrameID, ConstraintSet.RIGHT)
    }

    val sideShift = 170
    //constrSet.setMargin(clrSetBarR.id, ConstraintSet.RIGHT,sideShift)
    //constrSet.setMargin(clrSetBarB.id, ConstraintSet.LEFT,sideShift)

    constrSet.setMargin(clrSetBarG.id, ConstraintSet.LEFT,sideShift)
    constrSet.setMargin(clrSetBarB.id, ConstraintSet.LEFT,sideShift*2)

    /*
    When using this code the 3 bars appears one after the other from top to bottom.*/
    constrSet.setMargin(clrSetBarR.id, ConstraintSet.TOP,-sideShift)
    constrSet.setMargin(clrSetBarB.id, ConstraintSet.BOTTOM,-sideShift)
    // */

    constrSet.applyTo(constraintLayout)
}
Michel
  • 10,303
  • 17
  • 82
  • 179
  • 1
    Add your views before cloning the _ConstraintSet_. See [this](https://stackoverflow.com/a/40527407/6287910) Stack Overflow answer. I will also mention that rotation occurs post-layout, so you constraints will be set to your views _then_ the rotation occurs. – Cheticamp May 07 '22 at 11:09
  • Yes, it works. Thank you for the tip. I am now trying to have the bars displayed with the proper length. I tried to use minLength but with API 28 it doesn't work. – Michel May 07 '22 at 15:59
  • minlength? You mean minWidth/minHeight? – Cheticamp May 07 '22 at 19:14
  • Yes, that's right: minWidth, minHeight, maxWidth, maxHeight. They all have the same issue of not being available with API 28. What I want to do is basic enough, there must be a way to do it. – Michel May 08 '22 at 04:08

0 Answers0