How To Create Table In Android Dynamically

Written by Biajid

Hi there, I'm Biajid, a devoted tech lover who specializes in tackling technical difficulties relate...

https://www.androidsparks.com

   Review by Android Sparks

Hi there, I'm androidsparks, a devoted tech lover who specializes in tackling technical difficulties...

https://www.androidsparks.com

How
Why

Last Updated: May 20, 2023


In many applications, displaying tabular data is a prerequisite. The ideal method is to create a Table. But for that, you need to know how to create a table in Android dynamically.

The first step to dynamically creating a table in Android is to make an XML file. You have to write the proper XML code to do this. The second step is making the source of activity. It is a little bit tough. But doing it in some sections will be easy. And the last step is launching the program.

Steps To Create Table In Android Dynamically

Implementing a dynamic Table is a bit difficult. While creating a table, a ViewGroup subtype called Android Dynamic Table shows child View components in rows and columns. All the child elements will be arranged in rows and columns with no visible borders separating them. So, let’s have a thorough discussion!

 However, the dynamic functions are almost exactly like an HTML table. It has the same number of columns as rows with the most cells. However, it becomes easy once you get the hang of it. Here are the procedures for making a dynamic Android table.

Step 1: Make An XML File

The initial step is to create an XML file. Put the project’s XML code in the correct place. The outcome will be displayed as follows.

To obtain the values for the Rows and Columns, you will require two EditTexts (TextBoxes). In this project, there are two buttons and two Table Layouts.

The children of a table layout are arranged into rows and columns. Each TableRow object that makes up a TableLayout defines a row.

Step 2: Source Of Activity

The source of the activity code is too lengthy to understand. Here is the code in sections for better understanding.

● Section 1

Two EditText class instances must be created. You must include the reference to edittext1 in the initial txt1 object. The reference to edittext2 must be included in the second txt2 object.

Thus, EditText’s ids are edittext1 and edittext2. To figure out how many rows, use txt1 and txt2, respectively.

EditText txt1;    
EditText txt2;    
Button Create;    
TableLayout TabLayout_Create;    
TableLayout TabLayout_Show;    
Button Show;    
txt1=(EditText)findViewById(R.id.editText1);    
txt2=(EditText)findViewById(R.id.editText2);    
Create =(Button)findViewById(R.id.button1);    
Show=(Button)findViewById(R.id.Show);    
TabLayout_Create =(TableLayout) findViewById(R.id.TableLayout);    
TabLayout_Show=(TableLayout) findViewById(R.id.TableLayout2); 

Use two Table Layouts to retrieve the number of user-inserted columns. TableLayout Create is the first option. Create a variable number of EditTexts in this layout (TextBoxes).

TableLayout Show is the second option. Create a variable number of TextViews for this layout. Each Edittext value present in TableLayout Create must be fetched and inserted into the TextView that is included in TableLayout Show.

There are two buttons. The Create option is the first one. In TableLayout Create, this function is used to generate a variable number of EditTexts. The show is another button. The data from each Editext is retrieved using this button and then added to a TextView.

● Section 2

Here, the Create Button receives an OnClick event. Row and Col are the two required variables. The user enters data into the TextViews txt1 and txt2 in the form of rows and columns, respectively, which are stored in the Row and Col variables.

You will need two loops. The inner loop makes a new column, while the outer loop makes a new row. new TableRow = TableRow row (MainActivity.this).

Create.setOnClickListener(new View.OnClickListener() {    
    
    @Override    
    public void onClick(View arg0) {    
        // TODO Auto-generated method stub    
        str = txt1.getText().toString();    
        stm = txt2.getText().toString();    
    
        Row = Integer.parseInt(str);    
        Col = Row = Integer.parseInt(stm);    
    
        for (i = 1; i <= Row; i++) {    
            final TableRow row = new TableRow(MainActivity.this);    
            if (i % 2 == 0) {    
                row.setBackgroundColor(Color.MAGENTA);    
            } else {    
                row.setBackgroundColor(Color.GRAY);    
            }    
    
            for (j = 1; j <= Col; j++) {    
    
                final EditText txt = new EditText(MainActivity.this);    
                txt.setTextColor(Color.GREEN);    
                txt.setTextSize(TypedValue.COMPLEX_UNIT_PT, 8);    
                txt.setTypeface(Typeface.SERIF, Typeface.BOLD);    
                txt.setGravity(Gravity.LEFT);    
                txt.setText("C" + i + j + " ");    
    
                row.addView(txt);    
    
    
            }    
            TabLayout_Create.addView(row);    
    
        }    
    }    
});   

The command adds a fresh Row object to the active activity. It is called MainActivity. After that, you should make several Edittext objects and give each one an attribute.

To assign a view to another view, use the Add view command. Each row needs to be assigned to TabLayout Create TabLayout at the conclusion of the internal loop. This button creates a table when it is clicked.

This table will have several EditTexts, each with a predetermined value.

● Section 3

Get the value of every EditText control available in the “TableLayout” structure. Then you must add this value to both a new TableLayout and a TextView control.

Extract the parent control’s child control using the getChildAt function. This method accepts a numeric value corresponding to the control’s index number.

To add a view to another view, utilize the addView function. As a result, at the end of the inner loop, you must first put every Textview into a row. TabLayout show view should now contain this row (view).

Show.setOnClickListener(new View.OnClickListener() {    
    
    @Override    
    public void onClick(View arg0) {    
        // TODO Auto-generated method stub    
    
        for (i = 0; i < Row; i++) {    
            final TableRow row = (TableRow) TabLayout_Create.getChildAt(i);    
            final TableRow row1 = new TableRow(MainActivity.this);    
    
            if (i % 2 == 0) {    
                row1.setBackgroundColor(Color.YELLOW);    
            } else {    
                row1.setBackgroundColor(Color.RED);    
            }    
            for (j = 0; j < Col; j++) {    
                final EditText etxt = (EditText) row.getChildAt(j);    
    
                final TextView txt = new TextView(MainActivity.this);    
                txt.setTextColor(Color.GREEN);    
                txt.setTextSize(TypedValue.COMPLEX_UNIT_PT, 8);    
                txt.setTypeface(Typeface.SERIF, Typeface.BOLD);    
                txt.setGravity(Gravity.LEFT);    
                txt.setText(etxt.getText());    
    
                row1.addView(txt);    
            }    
            TabLayout_Show.addView(row1);    
        }    
    
    }    
});   

Step 3: Run The Application

Try launching your program. Your original Android mobile phone must be connected to your computer.

You must open a project’s activity file and select the RunPlay icon from the toolbar to launch the app. Choose your smartphone as an option, then verify that it is showing your original display.

Tips To Follow

People frequently run into issues when establishing or modifying a new column for a table on Android. It is also really bothersome. To fix the problem, use the tips listed below:

  • While adding the codes, keep the correct comma and brackets. A single comma or bracket error can prevent an application from running. Additionally, locating the mistake is quite challenging and frustrating.
  • Put the codes where they belong. Otherwise, your program won’t produce the results you want.
  • While coding, pay close attention to the screen. There will be fewer errors as a result.
  • Add the EditText carefully.
  • When opening the program, ensure your computer and mobile device are connected. Double-check the phone’s connection to ensure it is secure.
  • Place each EditText control’s value in the “TableLayout” structure correctly. Value error results from any mistakes.

Conclusion

A dynamic table in Android arranges its kids in columns and rows. Row, column, and cell borders are not visible in dynamic TableLayout containers. The number of columns in the table will match the number of cells in the row. Cells in a table may be empty.

Hopefully, we have gone through every detail of how to create a table in Android dynamically. It will display everything in a logical order. Enjoy the advantages by doing this carefully. Coding is fun!

Biajid

Hi there, I'm Biajid, a devoted tech lover who specializes in tackling technical difficulties related to Android phones and operating systems. Over the years, I've gained extensive experience in resolving complex issues and have become a seasoned expert in the field. I'm delighted to have you on my website, and I'm confident that the resources and solutions provided here will prove to be valuable to you

UnhelpfulMostly unhelpfulPossibly helpfulMostly helpfulVery helpful (No Ratings Yet)
Loading...

Last Updated: May 20, 2023

Post View : 325

Recent Posts