Creating Constraint layout uisng java file

Issue

I am trying to create constraint layout using Mainactivity.java but I am facing some problems. I’m getting the below output.

enter image description here

The constraint layout isn’t matching the size of parent.

I have created 2 classes 1 is MainActivity.java and another is MyConstraintLayout.Java

MyconstraintLayout.java

package com.example.checkbox;

import android.content.Context;
import android.view.ViewGroup;
import android.widget.Button;

import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.ConstraintLayout;

public class MyConstraintLayout extends ConstraintLayout {
    public MyConstraintLayout(@NonNull Context context) {
        super(context);
        setId(generateViewId());
        ViewGroup.LayoutParams layout = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        setLayoutParams(layout);
        setBackgroundColor(getResources().getColor(R.color.teal_200));

        createWidgets(context);
    }


    void createWidgets(Context context){
        ConstraintLayout.LayoutParams l1 = new ConstraintLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        Button b1 = new Button(context);
        b1.setText("Button");
        b1.setId(generateViewId());
        addView(b1);
        setLayoutParams(l1);

        ConstraintLayout.LayoutParams l2 = new ConstraintLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        Button b2 = new Button(context);
        b2.setText("Button");
        b2.setId(generateViewId());
        addView(b2);
        setLayoutParams(l2);


        l1.bottomToBottom = getId();
        l1.topToTop = getId();
        l1.endToEnd= getId();
        l1.startToStart = getId();


        l2.bottomToBottom = getId();
        l2.topToBottom = b1.getId();
        l2.endToEnd= getId();
        l2.startToStart = getId();


    }
}

MainActivity.java

package com.example.checkbox;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;

import android.app.Activity;
import android.os.Bundle;



public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MyConstraintLayout parent_layout = new MyConstraintLayout(this);
        setContentView(parent_layout);
    }
}

Don’t have any XML file.

Thank you in advance

Solution

You are calling setLayoutParams() on the ConstraintLayout and not on the child Views that you’ve added to the ConstraintLayout. You should be doing something like this instead:

b1.setLayoutParams(l1);
b2.setLayoutParams(l2);

Answered By – David Wasser

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published