<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Tomgxz</title>
    <description>The latest articles on DEV Community by Tomgxz (@tom_gxz).</description>
    <link>https://dev.to/tom_gxz</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F925964%2F9f598c9e-f023-405c-8e89-3d0ec9f4a42f.png</url>
      <title>DEV Community: Tomgxz</title>
      <link>https://dev.to/tom_gxz</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/tom_gxz"/>
    <language>en</language>
    <item>
      <title>How to get constraints working in Java Android Studio</title>
      <dc:creator>Tomgxz</dc:creator>
      <pubDate>Wed, 14 Sep 2022 06:55:39 +0000</pubDate>
      <link>https://dev.to/tom_gxz/how-to-get-constraints-working-in-java-android-studio-3d0l</link>
      <guid>https://dev.to/tom_gxz/how-to-get-constraints-working-in-java-android-studio-3d0l</guid>
      <description>&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;activity.java&lt;br&gt;
`&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@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 &amp;gt;= 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);

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;code&gt;&lt;br&gt;
activity.xml&lt;br&gt;
&lt;/code&gt;&lt;br&gt;
    &amp;lt;?xml version="1.0" encoding="utf-8"?&amp;gt;&lt;br&gt;
    
    xmlns:app="http://schemas.android.com/apk/res-auto"&lt;br&gt;
    xmlns:tools="http://schemas.android.com/tools"&lt;br&gt;
    android:layout_width="match_parent"&lt;br&gt;
    android:layout_height="match_parent"&lt;br&gt;
    tools:context=".activity"&amp;gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;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"&amp;gt;

    &amp;lt;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" /&amp;gt;

    &amp;lt;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" /&amp;gt;

&amp;lt;/LinearLayout&amp;gt;

&amp;lt;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"&amp;gt;

    &amp;lt;androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/activity_characterselect_content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"&amp;gt;

    &amp;lt;/androidx.constraintlayout.widget.ConstraintLayout&amp;gt;
&amp;lt;/ScrollView&amp;gt;


&amp;lt;/androidx.constraintlayout.widget.ConstraintLayout&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;`&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--jRS06VYW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ani8hures0ba9atc01bz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--jRS06VYW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ani8hures0ba9atc01bz.png" alt="Image description" width="880" height="1564"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>android</category>
      <category>java</category>
    </item>
  </channel>
</rss>
