-1

I am pretty new in Android.

I have some problems to create a dynamic LinearLayout programmatically. I actually have some information in ArrayLists that I need to display. The problem is that I don't know how many items are in the ArrayList, so I need to create the same layout (LinearLayoutChild) for every item in the ArrayList.

For example, I have created this (in this case, let's say the ArrayLists have 2 items each)

 ArrayList<String> alistA = new ArrayList<String>();
    ArrayList<String> alistB = new ArrayList<String>();
    ArrayList<String> alistC = new ArrayList<String>();
    ArrayList<String> alistD = new ArrayList<String>();
    ArrayList<String> alistE = new ArrayList<String>();
    alistA.add(0, "TextA1");
    alistA.add(1, "TextA2");
    alistB.add(0, "TextB1");
    alistB.add(1, "TextB2");
    alistC.add(0, "TextC1");
    alistC.add(1, "TextC2");
    alistD.add(0, "TextD1");
    alistD.add(1, "TextD2");
    alistE.add(0, "TextE1");
    alistE.add(1, "TextE2");

    int NumberArray = 2;

    for(int i = 0; i<NumberArray;i++){
        // How can i do this?
    }

I want to display it like : alistA[0] -> Tv1 alistB[0] -> Tv2 and so on...

alistA[[1]] -> Tv1 (newly created) alistB[[1]] -> Tv2 (newly created) ...

My XML file :

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.murat.testlol.MainActivity"
android:id="@+id/LinearLayout1"
android:orientation="vertical">


<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/Et1"/>
<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/Btn1"/>



<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:weightSum="12"
    android:orientation="horizontal"
    android:id="@+id/LinearLayoutChild">

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

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/Tv1"
            android:gravity="center"
            android:text="TextView1"
            />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="TextView2"
            android:id="@+id/Tv2"
            android:gravity="center"/>

    </LinearLayout>


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


        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/Tv3"
            android:gravity="center"
            android:text="TextView3"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:id="@+id/Tv4"
            android:text="TextView4"/>


    </LinearLayout>

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


        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/Tv5"
            android:gravity="center"
            android:text="TextView5"/>
    </LinearLayout>



</LinearLayout>

The EditText and button need to stay at the same place.

Thank you.

Rob
  • 2,243
  • 4
  • 29
  • 40
JackDupont
  • 37
  • 1
  • 4

3 Answers3

1

Sounds like a good use case for Android's ListView. From Google's documentation:

ListView is a view group that displays a list of scrollable items. The list items are automatically inserted to the list using an Adapter that pulls content from a source such as an array or database query and converts each item result into a view that's placed into the list.

To go this route, update main.xml to include a ListView element -- and move the LinearLayoutChild into its own XML file.

main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.murat.testlol.MainActivity"
    android:id="@+id/LinearLayout1"
    android:orientation="vertical">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/Et1"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/Btn1"/>

    <ListView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/list"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</LinearLayout>

generic_linear_layout_child.xml:

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

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/Tv1"
        android:gravity="center"
        android:text="TextView1"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="TextView2"
        android:id="@+id/Tv2"
        android:gravity="center"/>

</LinearLayout>

Then, create an adapter to bind the data in your array to the appropriate UI elements. Here's a SO post on how to create a custom adapter: Custom Adapter for List View

Community
  • 1
  • 1
0

Do the following. Get your data and fill a linearLayout container during runtime after you have retrieved / created your data. Here is an example for creating the views.

private LinearLayout container;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    container = new LinearLayout(this);
    container.setOrientation(LinearLayout.VERTICAL)
    // + other layout stuff / better inflate this from xml
    // Get data and call createLayout with the data
}

createLayout(List<List<String>> lists) {
    for (List<String> list : lists) {
        LinearLayout linearLayout = new LinearLayout(this);
        linearLayout.setOrientation(LinearLayout.HORIZONTAL);
        // + other layout stuff / better inflate this from xml
        for (String string : list) {
            TextView textView = new AppCompatTextView(this);
            textView.setText(string);
            linearLayout.addView(textView);
        }
        container.addView(linearLayout);
    }
}
luckyhandler
  • 10,651
  • 3
  • 47
  • 64
0

You need to create the linearlayouts and textviews dynamically, using routines like this:

public  TextView makeTextView (String text)
{
    TextView tv = new TextView (context)
    tv.setText (text) ;
    // customise the layout of the text here, eg...
    tv.setTextColor(Color.RED) ;
    return tv;
}

public  LinearLayout makeHorizLayout ()
{
    LinearLayout ll = new LinearLayout (context);
    ll.setOrientation(LinearLayout.HORIZONTAL);
    ll.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    return ll;
}

After you have loaded the schema, you find the view that you want to insert the linearLayouts into like this:

View mainView = findViewById (R.id.LinearLayout1);

You then add a linearlayout to a view like this:

LinearLayout ll = makeHorizlayout ();
mainView.add(ll);

Then you add textviews to the LinearLayout like this:

TextView tv = makeTextView ("whatever text");
ll.addView (tv);
JavaLatte
  • 378
  • 5
  • 18