newpie newpie - 6 months ago 15
Android Question

create multiple linearlayouts and textvews programatically

create multiple linearlayouts and textvews programatically

i ahve tried o explain in xm see xml file to undersatnad the question

see image for my desired output

below xml is what i want but programatically

<RelativeLayout
android:id="@+id/fl_main"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="@color/white">



<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">


<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ABc" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ABc" />

</LinearLayout>

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ABc" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ABc" />

</LinearLayout>

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ABc" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ABc" />

</LinearLayout>

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ABc" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ABc" />

</LinearLayout>

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ABc" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ABc" />

</LinearLayout>

</LinearLayout>

</RelativeLayout



i wan to acheive above xml file like output but totally programatically


desired image output

Answer

Try This

add LinearLayout into your xml

<LinearLayout
    android:id="@+id/ll_main"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:background="@android:color/white">

</LinearLayout>

and change in your Java file like this

 LinearLayout ll_main = (LinearLayout) findViewById(R.id.ll_main);

    for(int i= 0; i <5 ;i++) {
        LinearLayout parent = new LinearLayout(Main.this);
        LinearLayout.LayoutParams param= new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
        param.weight = 1;
        parent.setLayoutParams(param);
        parent.setOrientation(LinearLayout.VERTICAL);

        TextView tv = new TextView(Main.this);
        tv.setText("T1");

        TextView tv2 = new TextView(Main.this);
        tv2.setText("T2");
        parent.addView(tv);
        parent.addView(tv2);
        ll_main.addView(parent);
    }