Android: How to create the tableView Dynamically as like below xml code?

0

In My app I want to create the TableView Dynamically: Here I have xml view as like what I want Dynamicaly in Andriod.

XML View:

                    <TableRow android:weightSum="1">
                        <TextView  android:gravity="left"
                        android:textStyle="bold" android:textSize="15sp" android:textColor="#EDA700"
                        android:layout_marginTop="3dp" android:layout_marginBottom="8dp"
                        android:text="PAYE calculations: Tax period 01 April 2011 to 31 March 2012" android:layout_weight="1"/>
                </TableRow>

                    <View android:layout_height="2dip" android:background="#FF000000" />

                    <TableRow android:weightSum="1">
                        <TextView  android:gravity="center"
                        android:textStyle="bold" android:textSize="16sp" android:textColor="#000000"
                        android:layout_marginTop="3dp" android:layout_marginBottom="3dp"
                        android:text="All employees: Total PAYE to withhold" android:layout_weight="1"/>
                    </TableRow>

                    <!--    taxcode value    -->
                    <!-- =================== -->
                    <TableRow android:weightSum="1" android:layout_marginRight="5dp">
                        <TextView android:gravity="left"
                        android:textSize="13sp" android:textColor="#000000"
                        android:layout_marginTop="3dp" android:layout_marginBottom="3dp"
                        android:text="Total gross income (salary/wages)" android:layout_weight="1"/>
                    <TextView  android:gravity="center"
                            android:textSize="13sp" android:singleLine="true"
                        android:layout_marginLeft="5dp" android:layout_marginRight="5dp"
                        android:layout_marginTop="3dp" android:layout_marginBottom="3dp"
                        android:textColor="#000000" android:layout_weight="1"/>
                    <TextView android:id="@+id/taxcodeTextView" android:gravity="center" android:textColor="#000000"
                            android:textSize="13sp" android:singleLine="true"
                        android:layout_marginTop="3dp" android:layout_marginBottom="3dp"
                        android:layout_marginLeft="5dp" android:layout_marginRight="5dp"
                        android:text="00" />
                    </TableRow>       

                    <!-- KiwiSaver employee Deduction percentage-->
                    <!-- ====================================== -->
                    <TableRow android:weightSum="1" android:layout_marginRight="5dp">
                        <TextView android:gravity="left"
                        android:textSize="13sp" android:textColor="#000000"
                        android:layout_marginTop="3dp" android:layout_marginBottom="3dp"
                        android:text="Total PAYE (including earner's levy)" android:layout_weight="1"/>
                    <TextView android:gravity="center"
                            android:textSize="13sp" android:singleLine="true" android:textStyle="bold"
                        android:layout_marginLeft="5dp" android:layout_marginRight="5dp"
                        android:layout_marginTop="3dp" android:layout_marginBottom="3dp"
                        android:text="minus"  android:textColor="#000000" android:layout_weight="1"/>
                    <TextView
                        android:id="@+id/kiwiSaverEmployeeDeductionPercentageTextView" 
                        android:gravity="center" android:textColor="#000000"
                            android:textSize="13sp" android:singleLine="true"
                        android:layout_marginTop="3dp" android:layout_marginBottom="3dp"
                        android:layout_marginLeft="5dp" android:layout_marginRight="5dp"
                        android:text="0.0%"/>
                    </TableRow>                 
                    </TableLayout>

I have created dynamicaly view as like below code:

List<TextView> textListWord = new ArrayList<TextView>(c2.getCount());
    List<TextView> textListAnswer = new ArrayList<TextView>(c2.getCount());
    List<ImageView> imageListAnswer = new ArrayList<ImageView>(c2.getCount());

    //for(int i = 0; i < c.getCount(); i++)
    c2.moveToFirst();
    do
    {    
        RelativeLayout innerLayout = new RelativeLayout(this);
        innerLayout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
        innerLayout.setBackgroundColor(0x00000000);

        // set the Multiple TextView
        TextView qWordTV = new TextView(getApplicationContext());
        TextView aWordTV = new TextView(getApplicationContext());
        ImageView aImageView = new ImageView(getApplicationContext());

        qWordTV.setText("\n"+c2.getString(3).toString());
        qWordTV.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
        qWordTV.setTextColor(0xFFFF0000);
        //qWordTV.layout(22, 0, 0, 0);
        qWordTV.setPadding(22, 0, 0, 0);

        aWordTV.setText("\n"+c2.getString(2).toString());
        aWordTV.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
        aWordTV.setGravity(Gravity.CENTER);
        aWordTV.setTextColor(0xFFFF0000);    
        if(c2.getString(2).equals(c2.getString(3)))
        {
            aImageView.setImageResource(R.drawable.correct);
        }
        else
        {
            aImageView.setImageResource(R.drawable.incorrect);
        }

        aImageView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));

        aImageView.setPadding(400, 25, 0, 0);


        /**** Any other text view setup code ****/    

        innerLayout.addView(qWordTV);
        innerLayout.addView(aWordTV);
        innerLayout.addView(aImageView);

        myLinearLayout.addView(innerLayout);

        textListWord.add(qWordTV); 
        textListAnswer.add(aWordTV);
        imageListAnswer.add(aImageView);
        //c.moveToNext();

    } 
    while (c2.moveToNext());

This will create the view dynamically as per the condition of the loop.

I want the below XML layout in to dynamically java code to create it.

Thanks.

android
xml
android-layout
android-emulator
android-ndk
asked on Stack Overflow Jan 9, 2012 by Shreyash Mahajan • edited Jan 22, 2019 by Paul Floyd

1 Answer

1

Hi take a look at this link it should have what you want!!! http://wowjava.wordpress.com/2011/04/01/dynamic-tablelayout-in-android/

answered on Stack Overflow Jan 9, 2012 by Hannibal Smith

User contributions licensed under CC BY-SA 3.0