How to get constraints working in Java Android Studio
Tags: android, java
Published: 2022-09-14 06:55:39

I am trying to programmatically define objects in one of the activities, so I'm using the ConstraintSet class it Java to do it. However, when I run the application, all of the widgets that I gave constraints to with java go to 0,0 as if they don't have constraints. I've tried looking online but havent found any fixes.

activity.java
`
    

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity);

        addCharacterGroupToSelection("Outsider",ConstraintSet.PARENT_ID);
        addCharacterGroupToSelection("Minion",R.id.activity_team_outsider);
    }

    protected void addCharacterGroupToSelection(String team, int previousGroupId) {
        int teamColor; int teamTitleId; int teamGroupId;

        switch (team) {
            case "Outsider":
                teamColor = getResources().getColor(R.color.good);
                break;
            case "Minion":
                teamColor = getResources().getColor(R.color.evil);
                break;
            default:
                throw new IllegalStateException("Unexpected value: " + team);
        }

        switch (team) {
            case "Outsider":
                teamGroupId = R.id.activity_team_outsider;
                teamTitleId = R.id.activity_team_outsider_title;
                break;
            case "Minion":
                teamGroupId = R.id.activity_team_minion;
                teamTitleId = R.id.activity_team_minion_title;
                break;
            default:
                throw new IllegalStateException("Unexpected value: " + team);
        }

        ConstraintLayout characterSelectContent = findViewById(R.id.activity_characterselect_content);
        ConstraintLayout teamGroup = new ConstraintLayout(this);
        TextView teamTitle = new TextView(this);

        teamGroup.setId(teamGroupId);

        characterSelectContent.addView(teamGroup);

        teamTitle.setId(teamTitleId);
        teamTitle.setText(team);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { teamTitle.setTextAppearance(R.style.body_black); }
        teamTitle.setTextColor(teamColor);

        teamGroup.addView(teamTitle);

        ConstraintSet constraintSet = new ConstraintSet();
        constraintSet.clone(characterSelectContent);

        constraintSet.constrainWidth(teamGroup.getId(),ConstraintLayout.LayoutParams.MATCH_PARENT);
        constraintSet.constrainHeight(teamGroup.getId(),ConstraintLayout.LayoutParams.WRAP_CONTENT);

        constraintSet.connect(teamGroup.getId(),ConstraintSet.END,ConstraintSet.PARENT_ID,ConstraintSet.END,0);
        constraintSet.connect(teamGroup.getId(),ConstraintSet.START,ConstraintSet.PARENT_ID,ConstraintSet.START,0);
        constraintSet.connect(teamGroup.getId(),ConstraintSet.TOP,previousGroupId,ConstraintSet.TOP,0);

        constraintSet.applyTo(characterSelectContent);

    }


`
activity.xml
`
    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".activity">

    <LinearLayout
        android:id="@+id/header"
        android:layout_width="0dp"
        android:layout_height="@dimen/header_height"
        android:layout_marginStart="@dimen/left_margin"
        android:layout_marginEnd="@dimen/right_margin"
        android:clipToPadding="false"
        android:orientation="horizontal"
        android:padding="-32dp"
        android:paddingLeft="-32dp"
        android:paddingRight="-32dp"
        android:paddingBottom="-32dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <ImageView
            android:id="@+id/header_editionlogo"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginStart="@dimen/left_margin"
            android:layout_marginTop="@dimen/top_margin"
            android:layout_marginBottom="@dimen/bottom_margin"
            android:layout_weight="1"
            android:contentDescription="@string/trouble_brewing"
            app:srcCompat="@drawable/tb_logo" />

        <TextView
            android:id="@+id/activity_text_select_characters"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginTop="@dimen/top_margin"
            android:layout_marginEnd="@dimen/right_margin"
            android:layout_marginBottom="@dimen/bottom_margin"
            android:layout_weight="3"
            android:text="@string/select_characters"
            android:textAlignment="center"
            android:textAppearance="@style/body_black"
            android:textColor="@color/black"
            android:textSize="34sp" />

    </LinearLayout>

    <ScrollView
        android:id="@+id/activity_characterselect"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginStart="@dimen/left_margin"
        android:layout_marginTop="1dp"
        android:layout_marginEnd="@dimen/right_margin"
        android:layout_marginBottom="@dimen/bottom_margin"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/header">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/activity_characterselect_content"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

        </androidx.constraintlayout.widget.ConstraintLayout>
    </ScrollView>


    </androidx.constraintlayout.widget.ConstraintLayout>

`

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ani8hures0ba9atc01bz.png)