Garren Fitzenreiter
Garren Fitzenreiter

Reputation: 839

Constraints not being applied to my TextView

Here I have code which adds a few elements to a ConstraintLayout programmatically

 public void AddCategory(final String label, String img){
        final float scale = getResources().getDisplayMetrics().density;

        //Create a new constraint layout
        ConstraintLayout constraintLayout = new ConstraintLayout(getContext());
        servicesLayout.addView(constraintLayout);
        constraintLayout.setId(View.generateViewId());
        android.support.v7.widget.GridLayout.LayoutParams layoutParams = new android.support.v7.widget.GridLayout.LayoutParams(android.support.v7.widget.GridLayout.spec(android.support.v7.widget.GridLayout.UNDEFINED, 1f), android.support.v7.widget.GridLayout.spec(android.support.v7.widget.GridLayout.UNDEFINED, 1f));
        layoutParams.width = 0;
        layoutParams.height = (int) scale * 200;
        layoutParams.setMargins((int) scale * 15,(int) scale * 15,(int) scale * 15,(int) scale * 15);
        constraintLayout.setLayoutParams(layoutParams);
        constraintLayout.setBackgroundColor(getResources().getColor(R.color.colorPrimary));

        //Place ImageView in layout
        ImageView imageView = new ImageView(getContext());
        constraintLayout.addView(imageView);
        imageView.setId(View.generateViewId());
        ConstraintSet constraintSet = new ConstraintSet();
        constraintSet.connect(imageView.getId(), ConstraintSet.TOP, constraintLayout.getId(), ConstraintSet.TOP);
        constraintSet.connect(imageView.getId(), ConstraintSet.BOTTOM, constraintLayout.getId(), ConstraintSet.BOTTOM);
        constraintSet.connect(imageView.getId(), ConstraintSet.LEFT, constraintLayout.getId(), ConstraintSet.LEFT);
        constraintSet.connect(imageView.getId(), ConstraintSet.RIGHT, constraintLayout.getId(), ConstraintSet.RIGHT);
        constraintSet.applyTo(constraintLayout);
        if (getContext() != null){
            GlideApp.with(this)
                    .load(img)
                    .centerCrop()
                    .transition(DrawableTransitionOptions.withCrossFade())
                    .placeholder(R.color.colorGray)
                    .error(R.drawable.title)
                    .into(imageView);
        }

        //Place label background
        ImageView captionImageView = new ImageView(getContext());
        constraintLayout.addView(captionImageView);
        captionImageView.setId(View.generateViewId());
        ConstraintSet captionConstraintSet = new ConstraintSet();
        captionConstraintSet.connect(captionImageView.getId(), ConstraintSet.BOTTOM, constraintLayout.getId(), ConstraintSet.BOTTOM);
        captionConstraintSet.connect(captionImageView.getId(), ConstraintSet.LEFT, constraintLayout.getId(), ConstraintSet.LEFT);
        captionConstraintSet.connect(captionImageView.getId(), ConstraintSet.RIGHT, constraintLayout.getId(), ConstraintSet.RIGHT);
        captionConstraintSet.applyTo(constraintLayout);
        captionImageView.getLayoutParams().width = ConstraintLayout.LayoutParams.MATCH_CONSTRAINT;
        captionImageView.getLayoutParams().height  = (int) scale * 50;
        captionImageView.setBackgroundColor(getResources().getColor(R.color.colorPrimaryDark));
        captionImageView.setAlpha(0.7f);

        //Place text in front of the label backgroudn
        TextView textView = new TextView(getContext());
        constraintLayout.addView(textView);
        textView.setId(View.generateViewId());
        ConstraintSet labelConstraintSet = new ConstraintSet();
        labelConstraintSet.connect(textView.getId(), ConstraintSet.BOTTOM, captionImageView.getId(), ConstraintSet.BOTTOM);
        labelConstraintSet.connect(textView.getId(), ConstraintSet.LEFT, captionImageView.getId(), ConstraintSet.LEFT);
        labelConstraintSet.connect(textView.getId(), ConstraintSet.RIGHT, captionImageView.getId(), ConstraintSet.RIGHT);
        labelConstraintSet.applyTo(constraintLayout);
        textView.getLayoutParams().width = ConstraintLayout.LayoutParams.WRAP_CONTENT;
        textView.getLayoutParams().height  = ConstraintLayout.LayoutParams.WRAP_CONTENT;
        textView.setTextColor(getResources().getColor(R.color.colorWhite));
        textView.setText(label);
}

The first 3 blocks of code work just fine: Programmatically adding a ConstraintLayout and two ImageViews. The problem lies within trying to constrain the TextView element. It just simply doesn't work when trying to constrain it to one of the ImageViews which is where it needs to be. It will work when constraining it to the ConstraintLayout, but that is undesirable. Instead it will ignore all constraintSet.connect code and place it in the default top right position of the layout as shown

enter image description here

Upvotes: 4

Views: 221

Answers (1)

Nainal
Nainal

Reputation: 1748

Change the endId of constraintSet for TextView like below:-

ConstraintSet labelConstraintSet = new ConstraintSet();
    labelConstraintSet.connect(textView.getId(), ConstraintSet.BOTTOM, constraintLayout.getId(), ConstraintSet.BOTTOM,(int)scale*15);
    labelConstraintSet.connect(textView.getId(), ConstraintSet.LEFT, constraintLayout.getId(), ConstraintSet.LEFT);
    labelConstraintSet.connect(textView.getId(), ConstraintSet.RIGHT, constraintLayout.getId(), ConstraintSet.RIGHT);

Upvotes: 1

Related Questions