0

I create checkboxes dynamically within the Constraintlayout. I need to use this layout, not the Linearlayout. So I created the given below methods

order.xml file

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/clReasons"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="5dp"
        android:layout_marginStart="5dp"
        android:layout_marginTop="5dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/clDepartments">
    </androidx.constraintlayout.widget.ConstraintLayout>

orderstatus.java

   linearMain =(ConstraintLayout) findViewById(R.id.clReasons);

   for(int intReasonCount=0;intReasonCount<reasonsList.size();intReasonCount++)
    {
    checkBox=new CheckBox(OrderStatus.this);                            
    checkBox.setId(Integer.valueOf(reasonsList.get(intReasonCount)
    .get(ReasonInfo.ReasonID))

    );
                            
    checkBox.setText(String.valueOf(reasonsList.get(intReasonCount)
    .get(ReasonInfo.ReasonDesc)));
                           
    linearMain.addView(checkBox

    );

The output is

enter image description here

All checkboxes are aligned at one position. I need to set this dynamic checkbox are 2 columns at each row(2 checkboxes at each row). Please refer to the image.

Kishan Viramgama
  • 893
  • 1
  • 11
  • 23

1 Answers1

0

You need to set constaints for child views in code. For example check this answer: ConstraintLayout: change constraints programmatically

Denis
  • 71
  • 7