CJ Barnwell
CJ Barnwell

Reputation: 43

In array of TextViews, only the last TextView displayed

I'm trying to display an array of TextViews, but only the last TextView is displaying. I've checked moving the constraint margin according to the i value and found that they are not on top of one another.... only the last one is being displayed. Anyone have any ideas what's going on?

int baseID = 123;
ConstraintLayout parent = (ConstraintLayout) findViewById(R.id.my_activity_view);
ConstraintLayout.LayoutParams params = new ConstraintLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);

TextView[] TV = new TextView[5];
for (int i = 0; i < 5; i++) {
    TV[i] = new TextView(this);

    TV[i].setId(baseID + i);
    TV[i].setLayoutParams(params);
    TV[i].setText("TEST TEXT "+i);
    parent.addView(TV[i]);

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

    constraintSet.connect(TV[i].getId(), ConstraintSet.TOP, R.id.topTV, ConstraintSet.BOTTOM);
    if (i > 0) constraintSet.connect(TV[i].getId(), ConstraintSet.LEFT, TV[i - 1].getId(), ConstraintSet.RIGHT);
    constraintSet.applyTo(parent);
}

Upvotes: 0

Views: 121

Answers (2)

Thanks Bro it was very Helpful My code for slider dots

public void setIndicator(){

        dots = new ImageView[4];
        ConstraintLayout.LayoutParams layoutParams = new ConstraintLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT , ViewGroup.LayoutParams.WRAP_CONTENT
        );
        layoutParams.setMargins(0,0,0,0);

        for (int i = 0; i < dots.length; i++) {

            ConstraintLayout.LayoutParams params = new ConstraintLayout.LayoutParams(
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT);

            dots[i] = new ImageView(this);

            dots[i].setId(123 + i);
            dots[i].setLayoutParams(params);
            dots[i].setImageDrawable(ContextCompat.getDrawable(
                    getApplicationContext(),
                    R.drawable.page_inactive

            ));
            mDotLayout.addView(dots[i]);

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

            if(i==0){
                constraintSet.connect(dots[i].getId(), ConstraintSet.LEFT, R.id.Constraint3, ConstraintSet.LEFT);
            }
            else {
                constraintSet.connect(dots[i-1].getId(), ConstraintSet.RIGHT, dots[i].getId(), ConstraintSet.LEFT);
                constraintSet.connect(dots[i].getId(), ConstraintSet.LEFT, dots[i - 1].getId(), ConstraintSet.RIGHT);
            }
            if(i==dots.length-1){
                constraintSet.connect(dots[i].getId(), ConstraintSet.RIGHT, R.id.Constraint3, ConstraintSet.RIGHT);
            }
            constraintSet.applyTo(mDotLayout);
        }
    }

    public void setCurrentIndicator(int position){
        System.out.println(position);
        int sz = 4;
        for(int i = 0 ; i < sz ; i++)
        {

            ImageView imageView = (ImageView) mDotLayout.getChildAt(i);
            if(i==position)
            {
                imageView.setImageDrawable(
                        ContextCompat.getDrawable(getApplication(),R.drawable.page_active)
                );
            }else{
                imageView.setImageDrawable(
                        ContextCompat.getDrawable(getApplication(),R.drawable.page_inactive)
                );
            }
        }
    }

Upvotes: 0

CJ Barnwell
CJ Barnwell

Reputation: 43

I finally got it to work by placing ConstraintLayout.LayoutParams params = new ConstraintLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); inside the for loop. Final code is as follows:

int baseID = 123;
ConstraintLayout parent = (ConstraintLayout) findViewById(R.id.my_activity_view);

TextView[] TV = new TextView[5];
for (int i = 0; i < CueList.length; i++) {

    ConstraintLayout.LayoutParams params = new ConstraintLayout.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT);

    TV[i] = new TextView(this);

    TV[i].setId(baseID + i);
    TV[i].setLayoutParams(params);
    TV[i].setText("TEST TEXT "+i);
    parent.addView(TV[i]);

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

    constraintSet.connect(TV[i].getId(), ConstraintSet.TOP, R.id.cuelist, ConstraintSet.BOTTOM);
    if (i > 0) constraintSet.connect(TV[i].getId(), ConstraintSet.LEFT, TV[i - 1].getId(), ConstraintSet.RIGHT);
    constraintSet.applyTo(parent);
}

Upvotes: 0

Related Questions