ConstraintLayout weighted chain won't budge












0















For some reason, the weights for my ConstraintLayout items are not being applied. I've looked through the several times but I'm not sure where I've gone wrong.



enter image description here



XML



<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/my_scrollView_LL"
android:fillViewport="true"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingStart="@dimen/activity_horizontal_margin"
android:paddingEnd="@dimen/activity_horizontal_margin">

<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/my_constraintLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</ScrollView>


Kotlin



class MyFragment : androidx.fragment.app.Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_sv, container, false)
}

override fun onActivityCreated(savedInstanceState: Bundle?) {
val v = view
val myCL = v!!.findViewById<ConstraintLayout>(R.id.my_constraintLayout)


val rlp1 = ConstraintLayout.LayoutParams(ConstraintLayout.LayoutParams.WRAP_CONTENT, ConstraintLayout.LayoutParams.WRAP_CONTENT)


val r = context!!.resources
val fourDp = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 4f, r.displayMetrics).toInt()
val tenDp = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10f, r.displayMetrics).toInt()


/*
* CardView items
*/
val ivIcon = ImageView(context)
ivIcon.setImageResource(R.drawable.ic_warning)
ivIcon.setColorFilter((colorFTC), android.graphics.PorterDuff.Mode.SRC_IN)
ivIcon.layoutParams = rlp1

val tvText = TextView(context)
tvText.text = "Lorem ipsum dolor sit amet, eos aliquam vulputate percipitur ei."
TextViewCompat.setTextAppearance(tvText, android.R.style.TextAppearance_Medium)
tvText.layoutParams = rlp1

val cvMain = context?.let { CardView(it) }
when {
cvMain != null -> {
cvMain.radius = fourDp.toFloat()
cvMain.setContentPadding(tenDp, tenDp, tenDp, tenDp)
cvMain.useCompatPadding = true
cvMain.cardElevation = fourDp.toFloat()
}
}

cvMain!!.id = View.generateViewId()
ivIcon.id = View.generateViewId()
tvText.id = View.generateViewId()

myCL.addView(cvMain)
cvMain.addView(ivIcon)
cvMain.addView(tvText)

// Constraint Set
val set = ConstraintSet()
set.clone(myCL)
set.connect(cvMain.id, ConstraintSet.TOP, myCL.id, ConstraintSet.TOP, 0)
set.connect(ivIcon.id, ConstraintSet.START, myCL.id, ConstraintSet.START, 0)
set.connect(ivIcon.id, ConstraintSet.END, tvText.id, ConstraintSet.START, tenDp)
set.connect(tvText.id, ConstraintSet.START, ivIcon.id, ConstraintSet.END, tenDp)
set.connect(tvText.id, ConstraintSet.END, myCL.id, ConstraintSet.END, 0)
set.setHorizontalChainStyle(ivIcon.id, ConstraintSet.CHAIN_PACKED)
set.setHorizontalChainStyle(tvText.id, ConstraintSet.CHAIN_PACKED)
set.setHorizontalWeight(ivIcon.id, 10f)
set.setHorizontalWeight(ivIcon.id, 90f)
set.applyTo(myCL)

super.onActivityCreated(savedInstanceState)
}
}









share|improve this question





























    0















    For some reason, the weights for my ConstraintLayout items are not being applied. I've looked through the several times but I'm not sure where I've gone wrong.



    enter image description here



    XML



    <?xml version="1.0" encoding="utf-8"?>
    <ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/my_scrollView_LL"
    android:fillViewport="true"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingStart="@dimen/activity_horizontal_margin"
    android:paddingEnd="@dimen/activity_horizontal_margin">

    <androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/my_constraintLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>
    </ScrollView>


    Kotlin



    class MyFragment : androidx.fragment.app.Fragment() {
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
    return inflater.inflate(R.layout.fragment_sv, container, false)
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
    val v = view
    val myCL = v!!.findViewById<ConstraintLayout>(R.id.my_constraintLayout)


    val rlp1 = ConstraintLayout.LayoutParams(ConstraintLayout.LayoutParams.WRAP_CONTENT, ConstraintLayout.LayoutParams.WRAP_CONTENT)


    val r = context!!.resources
    val fourDp = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 4f, r.displayMetrics).toInt()
    val tenDp = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10f, r.displayMetrics).toInt()


    /*
    * CardView items
    */
    val ivIcon = ImageView(context)
    ivIcon.setImageResource(R.drawable.ic_warning)
    ivIcon.setColorFilter((colorFTC), android.graphics.PorterDuff.Mode.SRC_IN)
    ivIcon.layoutParams = rlp1

    val tvText = TextView(context)
    tvText.text = "Lorem ipsum dolor sit amet, eos aliquam vulputate percipitur ei."
    TextViewCompat.setTextAppearance(tvText, android.R.style.TextAppearance_Medium)
    tvText.layoutParams = rlp1

    val cvMain = context?.let { CardView(it) }
    when {
    cvMain != null -> {
    cvMain.radius = fourDp.toFloat()
    cvMain.setContentPadding(tenDp, tenDp, tenDp, tenDp)
    cvMain.useCompatPadding = true
    cvMain.cardElevation = fourDp.toFloat()
    }
    }

    cvMain!!.id = View.generateViewId()
    ivIcon.id = View.generateViewId()
    tvText.id = View.generateViewId()

    myCL.addView(cvMain)
    cvMain.addView(ivIcon)
    cvMain.addView(tvText)

    // Constraint Set
    val set = ConstraintSet()
    set.clone(myCL)
    set.connect(cvMain.id, ConstraintSet.TOP, myCL.id, ConstraintSet.TOP, 0)
    set.connect(ivIcon.id, ConstraintSet.START, myCL.id, ConstraintSet.START, 0)
    set.connect(ivIcon.id, ConstraintSet.END, tvText.id, ConstraintSet.START, tenDp)
    set.connect(tvText.id, ConstraintSet.START, ivIcon.id, ConstraintSet.END, tenDp)
    set.connect(tvText.id, ConstraintSet.END, myCL.id, ConstraintSet.END, 0)
    set.setHorizontalChainStyle(ivIcon.id, ConstraintSet.CHAIN_PACKED)
    set.setHorizontalChainStyle(tvText.id, ConstraintSet.CHAIN_PACKED)
    set.setHorizontalWeight(ivIcon.id, 10f)
    set.setHorizontalWeight(ivIcon.id, 90f)
    set.applyTo(myCL)

    super.onActivityCreated(savedInstanceState)
    }
    }









    share|improve this question



























      0












      0








      0








      For some reason, the weights for my ConstraintLayout items are not being applied. I've looked through the several times but I'm not sure where I've gone wrong.



      enter image description here



      XML



      <?xml version="1.0" encoding="utf-8"?>
      <ScrollView
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/my_scrollView_LL"
      android:fillViewport="true"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:paddingTop="@dimen/activity_vertical_margin"
      android:paddingBottom="@dimen/activity_vertical_margin"
      android:paddingStart="@dimen/activity_horizontal_margin"
      android:paddingEnd="@dimen/activity_horizontal_margin">

      <androidx.constraintlayout.widget.ConstraintLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/my_constraintLayout"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"/>
      </ScrollView>


      Kotlin



      class MyFragment : androidx.fragment.app.Fragment() {
      override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
      return inflater.inflate(R.layout.fragment_sv, container, false)
      }

      override fun onActivityCreated(savedInstanceState: Bundle?) {
      val v = view
      val myCL = v!!.findViewById<ConstraintLayout>(R.id.my_constraintLayout)


      val rlp1 = ConstraintLayout.LayoutParams(ConstraintLayout.LayoutParams.WRAP_CONTENT, ConstraintLayout.LayoutParams.WRAP_CONTENT)


      val r = context!!.resources
      val fourDp = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 4f, r.displayMetrics).toInt()
      val tenDp = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10f, r.displayMetrics).toInt()


      /*
      * CardView items
      */
      val ivIcon = ImageView(context)
      ivIcon.setImageResource(R.drawable.ic_warning)
      ivIcon.setColorFilter((colorFTC), android.graphics.PorterDuff.Mode.SRC_IN)
      ivIcon.layoutParams = rlp1

      val tvText = TextView(context)
      tvText.text = "Lorem ipsum dolor sit amet, eos aliquam vulputate percipitur ei."
      TextViewCompat.setTextAppearance(tvText, android.R.style.TextAppearance_Medium)
      tvText.layoutParams = rlp1

      val cvMain = context?.let { CardView(it) }
      when {
      cvMain != null -> {
      cvMain.radius = fourDp.toFloat()
      cvMain.setContentPadding(tenDp, tenDp, tenDp, tenDp)
      cvMain.useCompatPadding = true
      cvMain.cardElevation = fourDp.toFloat()
      }
      }

      cvMain!!.id = View.generateViewId()
      ivIcon.id = View.generateViewId()
      tvText.id = View.generateViewId()

      myCL.addView(cvMain)
      cvMain.addView(ivIcon)
      cvMain.addView(tvText)

      // Constraint Set
      val set = ConstraintSet()
      set.clone(myCL)
      set.connect(cvMain.id, ConstraintSet.TOP, myCL.id, ConstraintSet.TOP, 0)
      set.connect(ivIcon.id, ConstraintSet.START, myCL.id, ConstraintSet.START, 0)
      set.connect(ivIcon.id, ConstraintSet.END, tvText.id, ConstraintSet.START, tenDp)
      set.connect(tvText.id, ConstraintSet.START, ivIcon.id, ConstraintSet.END, tenDp)
      set.connect(tvText.id, ConstraintSet.END, myCL.id, ConstraintSet.END, 0)
      set.setHorizontalChainStyle(ivIcon.id, ConstraintSet.CHAIN_PACKED)
      set.setHorizontalChainStyle(tvText.id, ConstraintSet.CHAIN_PACKED)
      set.setHorizontalWeight(ivIcon.id, 10f)
      set.setHorizontalWeight(ivIcon.id, 90f)
      set.applyTo(myCL)

      super.onActivityCreated(savedInstanceState)
      }
      }









      share|improve this question
















      For some reason, the weights for my ConstraintLayout items are not being applied. I've looked through the several times but I'm not sure where I've gone wrong.



      enter image description here



      XML



      <?xml version="1.0" encoding="utf-8"?>
      <ScrollView
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/my_scrollView_LL"
      android:fillViewport="true"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:paddingTop="@dimen/activity_vertical_margin"
      android:paddingBottom="@dimen/activity_vertical_margin"
      android:paddingStart="@dimen/activity_horizontal_margin"
      android:paddingEnd="@dimen/activity_horizontal_margin">

      <androidx.constraintlayout.widget.ConstraintLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/my_constraintLayout"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"/>
      </ScrollView>


      Kotlin



      class MyFragment : androidx.fragment.app.Fragment() {
      override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
      return inflater.inflate(R.layout.fragment_sv, container, false)
      }

      override fun onActivityCreated(savedInstanceState: Bundle?) {
      val v = view
      val myCL = v!!.findViewById<ConstraintLayout>(R.id.my_constraintLayout)


      val rlp1 = ConstraintLayout.LayoutParams(ConstraintLayout.LayoutParams.WRAP_CONTENT, ConstraintLayout.LayoutParams.WRAP_CONTENT)


      val r = context!!.resources
      val fourDp = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 4f, r.displayMetrics).toInt()
      val tenDp = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10f, r.displayMetrics).toInt()


      /*
      * CardView items
      */
      val ivIcon = ImageView(context)
      ivIcon.setImageResource(R.drawable.ic_warning)
      ivIcon.setColorFilter((colorFTC), android.graphics.PorterDuff.Mode.SRC_IN)
      ivIcon.layoutParams = rlp1

      val tvText = TextView(context)
      tvText.text = "Lorem ipsum dolor sit amet, eos aliquam vulputate percipitur ei."
      TextViewCompat.setTextAppearance(tvText, android.R.style.TextAppearance_Medium)
      tvText.layoutParams = rlp1

      val cvMain = context?.let { CardView(it) }
      when {
      cvMain != null -> {
      cvMain.radius = fourDp.toFloat()
      cvMain.setContentPadding(tenDp, tenDp, tenDp, tenDp)
      cvMain.useCompatPadding = true
      cvMain.cardElevation = fourDp.toFloat()
      }
      }

      cvMain!!.id = View.generateViewId()
      ivIcon.id = View.generateViewId()
      tvText.id = View.generateViewId()

      myCL.addView(cvMain)
      cvMain.addView(ivIcon)
      cvMain.addView(tvText)

      // Constraint Set
      val set = ConstraintSet()
      set.clone(myCL)
      set.connect(cvMain.id, ConstraintSet.TOP, myCL.id, ConstraintSet.TOP, 0)
      set.connect(ivIcon.id, ConstraintSet.START, myCL.id, ConstraintSet.START, 0)
      set.connect(ivIcon.id, ConstraintSet.END, tvText.id, ConstraintSet.START, tenDp)
      set.connect(tvText.id, ConstraintSet.START, ivIcon.id, ConstraintSet.END, tenDp)
      set.connect(tvText.id, ConstraintSet.END, myCL.id, ConstraintSet.END, 0)
      set.setHorizontalChainStyle(ivIcon.id, ConstraintSet.CHAIN_PACKED)
      set.setHorizontalChainStyle(tvText.id, ConstraintSet.CHAIN_PACKED)
      set.setHorizontalWeight(ivIcon.id, 10f)
      set.setHorizontalWeight(ivIcon.id, 90f)
      set.applyTo(myCL)

      super.onActivityCreated(savedInstanceState)
      }
      }






      android kotlin android-constraintlayout






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 2 at 13:44







      MacaronLover

















      asked Jan 1 at 22:09









      MacaronLoverMacaronLover

      2,09632760




      2,09632760
























          1 Answer
          1






          active

          oldest

          votes


















          3














          The constraints only apply to Views that are direct children of the parent ConstraintLayout. In your case, my_constraintLayout only has one child, which is a CardView. The ImageView and the TextView that you add using addView() are children of the CardView and not the ConstraintLayout, so the constraints you are trying to set have no effect on them.



          You need to have a ConstraintLayout inside the CardView to add the Views and set the constraints.






          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53999327%2fconstraintlayout-weighted-chain-wont-budge%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            3














            The constraints only apply to Views that are direct children of the parent ConstraintLayout. In your case, my_constraintLayout only has one child, which is a CardView. The ImageView and the TextView that you add using addView() are children of the CardView and not the ConstraintLayout, so the constraints you are trying to set have no effect on them.



            You need to have a ConstraintLayout inside the CardView to add the Views and set the constraints.






            share|improve this answer




























              3














              The constraints only apply to Views that are direct children of the parent ConstraintLayout. In your case, my_constraintLayout only has one child, which is a CardView. The ImageView and the TextView that you add using addView() are children of the CardView and not the ConstraintLayout, so the constraints you are trying to set have no effect on them.



              You need to have a ConstraintLayout inside the CardView to add the Views and set the constraints.






              share|improve this answer


























                3












                3








                3







                The constraints only apply to Views that are direct children of the parent ConstraintLayout. In your case, my_constraintLayout only has one child, which is a CardView. The ImageView and the TextView that you add using addView() are children of the CardView and not the ConstraintLayout, so the constraints you are trying to set have no effect on them.



                You need to have a ConstraintLayout inside the CardView to add the Views and set the constraints.






                share|improve this answer













                The constraints only apply to Views that are direct children of the parent ConstraintLayout. In your case, my_constraintLayout only has one child, which is a CardView. The ImageView and the TextView that you add using addView() are children of the CardView and not the ConstraintLayout, so the constraints you are trying to set have no effect on them.



                You need to have a ConstraintLayout inside the CardView to add the Views and set the constraints.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Jan 1 at 23:24









                Pawel LaskowskiPawel Laskowski

                1,7271613




                1,7271613
































                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53999327%2fconstraintlayout-weighted-chain-wont-budge%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    MongoDB - Not Authorized To Execute Command

                    How to fix TextFormField cause rebuild widget in Flutter

                    in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith