• 11
name

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191

Backtrace:

File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

I am trying to include multiple views from a loop inside a constraint layout programmatically. How do I do so?

I want something like this:
I want something like this

I am able to do so for a single view but I am not being able to do the same thing for multiple views inside a loop. Basically I have a scroll view and inside it two lines line top and bottom I want to include the text views I created programmatically between those lines. I have provided the following lines of code:

final TextView[] myTextViews = new TextView[3];
        int i=1;
        ConstraintSet constraintSet = new ConstraintSet();
        myTextViews[i-1] = new TextView(this);
        myTextViews[i-1].setId(R.id.testid);
        myTextViews[i-1].setText("Text 1");
        ConstraintLayout.LayoutParams test = new ConstraintLayout.LayoutParams(
                ConstraintLayout.LayoutParams.MATCH_CONSTRAINT, ConstraintLayout.LayoutParams.WRAP_CONTENT);
        myTextViews[i-1].setLayoutParams(test);
        constraintLayout.addView(myTextViews[i-1]);
        constraintSet.clone(constraintLayout);
        constraintSet.connect(myTextViews[i-1].getId(), ConstraintSet.TOP, lineTop.getId(), ConstraintSet.BOTTOM, 28);
        constraintSet.connect(myTextViews[i-1].getId(), ConstraintSet.LEFT, constraintLayout.getId(), ConstraintSet.LEFT, 70);
        constraintSet.connect(myTextViews[i-1].getId(), ConstraintSet.RIGHT, constraintLayout.getId(), ConstraintSet.RIGHT, 70);

        constraintSet.applyTo(constraintLayout);
        for ( i = 1; i < 3; i++) {
            myTextViews[i] = new TextView(this);
            myTextViews[i].setId(R.id.testid+i);
            myTextViews[i].setText("Hello World "+i);
            myTextViews[i].setLayoutParams(test);
            constraintLayout.addView(myTextViews[i]);
            constraintSet.connect(myTextViews[i].getId(), ConstraintSet.TOP, myTextViews[i-1].getId(), ConstraintSet.BOTTOM, 28);
            constraintSet.connect(myTextViews[i].getId(), ConstraintSet.LEFT, constraintLayout.getId(), ConstraintSet.LEFT, 76);
            constraintSet.connect(myTextViews[i].getId(), ConstraintSet.RIGHT, constraintLayout.getId(), ConstraintSet.RIGHT, 76);
            constraintSet.applyTo(constraintLayout);
        }
        constraintSet.connect(lineBottom.getId(), ConstraintSet.TOP, myTextViews[i-1].getId(), ConstraintSet.BOTTOM, 8);
        constraintSet.applyTo(constraintLayout);

The following is the xml code:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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:id="@+id/relativeLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.reddevil.practice.practiceActivity">


    <ScrollView
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="184dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginBottom="72dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <android.support.constraint.ConstraintLayout
            android:id="@+id/scroll"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <TextView
                android:id="@+id/text1"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:background="@drawable/textgps"
                android:text="Random Text"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="0.0"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="parent" />
           <TextView
                android:id="@+id/lineTop"
                android:layout_width="0dp"
                android:layout_height="0dp"    
                android:text=" "
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="0.0"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/dropoff" />

            <TextView
                android:id="@+id/forstops"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:text=" "
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="1.0"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/lineTop" />
           <TextView
                android:id="@+id/text2"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:background="@drawable/textgps"
                android:text="Random Text"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="0.0"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="parent" />

        </android.support.constraint.ConstraintLayout>
    </ScrollView>
</android.support.constraint.ConstraintLayout>
    • I am using a scroll view. My XML layout looks like the one i have provided the link of. Do I need to paste the xml code also?
      • 2
    • You provided just an image (or i am blind) but yeah, the xml code would be helpful. I saw you only added 3 textviews, a listview or recyclerview might not be needed but if you add more view or wish to add complex items, adding them like that will fast besome a mess and won't provide good performances
      • 1
    • You want only the text between the 2 lines to be scrollable ? or the whole thing ? You can search about Recycler view tutorials to try to implement one, not easy the first time but much more customisable and better performances

You should use a RecyclerView or ListView for that kind of usage.

If you want to work with that you have to update things a little

First thing : don't use a Constraint layout (the one with the id scroll, replace it with a LinearLayout with attribute android:orientation="vertical"

you will have a xml layout that look like that (i make it simple with only important attribute but you need the other too)

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout >
    <ScrollView
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <LinearLayout
            android:id="@+id/scroll"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TextView
                android:id="@+id/text1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/textgps"
                android:text="Random Text"/>
            <View style="@style/Divider"/>

            <LinearLayout
                android:id="@+id/container"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"/>

            <View style="@style/Divider"/>
            <TextView
                android:id="@+id/text2"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:background="@drawable/textgps"
                android:text="Random Text"/>

        </LinearLayout>
    </ScrollView>
</android.support.constraint.ConstraintLayout>

Then, to add you TextViews :

final TextView[] myTextViews = new TextView[3];
LinearLayout container = findViewById(R.id.container);

LinearLayout .LayoutParams test = new LinearLayout .LayoutParams(
            LinearLayout.LayoutParams.MATCH_CONSTRAINT, LinearLayout.LayoutParams.WRAP_CONTENT);

for ( i = 1; i < 3; i++) {
        myTextViews[i] = new TextView(this);
        //i don't think you should do that
        //myTextViews[i].setId(R.id.testid+i);
        myTextViews[i].setText("Hello World "+i);
        myTextViews[i].setLayoutParams(test);
        container.addView(myTextViews[i]);
}

But you don't have much formatting to your textViews ...

You can do a xml layout file for each row you add so you can customize it easily:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/text_row_text"
        android:layout_width="0px"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:textSize="@dimen/text_size_data"
        android:layout_marginLeft="15dp"
        android:layout_marginStart="15dp"/>
    <Button
        android:id="@+id/text_row_delete"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/colorError"
        android:text="X"/>
</LinearLayout>

The code would look like this :

//List of strings instead of TextViews
String[] strings = {"test1","test2","test3"}
LinearLayout container = findViewById(R.id.container);

for(String s : strings){
    //R.layout.text_row is the layout file we just created
    View row = getLayoutInflater().inflate(R.layout.text_row, null);
    container.addView(row);

    TextView text = row.findViewById(R.id.text_row_text);
    text.setText(s);
}

If you have not much items to show it is ok to do that but don't try to make list with hundred of view like that, it would be awful for performances

  • 1
Reply Report
      • 2
    • Thanks for the help man and giving me the idea. I solved it by introducing a linear layout.

What you can do is make a reusable component (according to image you shared, a custom text view). And in your main xml file, you can use ListView or RecyclerView inside the ScrollView for making use of that reusable component.

  • 0
Reply Report
      • 2
    • Ok I will keep this in mind...but i found a work around from Kilarn123 answer. Thanks for reaching out though.