All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

How To Create Table In Android Dynamically

Last Updated : Jul 1, 2023

How To Create Table In Android Dynamically

In this article we will show you the solution of how to create table in android dynamically, two EditTexts (TextBoxes) will be used in the preceding activity to get the row and column values.

During this activity, there are two buttons and two Table Layouts. An arrangement of rows and columns is created by a table layout.

There are multiple rows defined by TableLayout, each defined by a TableRow object.

A Table Layout and a Column Layout are used to determine how many columns the user has inserted.

We create EditTexts (TextBoxes) dynamically with TableLayout_Create and display them using TableLayout_Show.

A dynamic number of TextViews is created in this layout. TableLayout_Create fetches the value of each Edittext and inserts it into TableLayout_Show's TextView.

Two buttons are also used. First, there is a button for creating. In TableLayout Create, this button is used to generate a random number of EditTexts.

Show is another option. This button retrieves data from each Editext and inserts it into a TextView.

By clicking this button, EditTexts can be created dynamically in TableLayout_Create.

There is also a button called Show. A TextView can be populated with data from each Editorext using this button.

An object called Row is created in the current activity by this statement. The activity name here is MainActivity.

The following step was to create a number of Edittext objects and assign properties to each of them.

The Edittext object is assigned to the Row object after all the properties have been assigned to each edittext object. A view can be assigned to another view using Addview.

Our internal loop terminates with TabLayout_Create TabLayout assigning every row. This button will create a table containing EditTexts with default values when we click on it.

To begin, we retrieved the values of all EditText controls in 'TableLayout,' then pasted them into a TextView control and a new TableLayout.

A child control of a parent control can be retrieved using the getChildAt method.

This method is given an integer value that represents the control's index number.

A view was inserted into another view using the addView method.

To insert each Textview into a row, we first insert it into a row, which is then inserted into TabLayout_show.

Creating dynamic views is the best way to avoid having repeating XML code.

After creating a separate layout and inflating them inside a LinearLayout, we'll store the user data in an ArrayList and display them as toasts.

Step By Step Guide On How To Create Table In Android Dynamically :-

private void addTableRow() {
  final TableLayout = (TableLayout) findViewById(R.id.my_table);
  final TableRow tr = (TableRow) getLayoutInflater().inflate(R.layout.table_row_item0, null);
  TextView tv1;
  tv1 = (TextView) tr.findViewById(R.id.cell_1);
  tv1.setText(...);
  ...
  tv1 = (TextView) tr.findViewById(R.id.cell_N);
  tv1.setText(...);
  table.addView(tr);
  tv1 = new TextView(this);
  tv1.setBackgroundColor(Color.parseColor("#80808080"));
  tv1.setHeight(/height of separator line. 2dip will be enough/);
  table.addView(tv1);
  registerForContextMenu(tr);
}
  1. In order to insert a private void into a table, you need to insert a table row first.
  2. After that, we create the layout of the table.
  3. A textview is then created by ID.
  4. In the next step, we should register the context menu for each table row
  5. After that, the program is closed

Conclusion :-

Creating dynamic views is the best way to avoid having repeating XML code.

After creating a separate layout and inflating them inside a LinearLayout, we'll store the user data in an ArrayList and display them as toasts.

I hope this article on how to create table in android dynamically helps you and the steps and mentioned above are easy to follow and implement.

Author Image About Pragati

Experienced coding content writer who enjoys breaking down complex concepts in programming languages like Java, Python, C, and C++. Over three years of experience producing interesting and relevant content for a variety of entities. Committed to providing concise and easy-to-understand articles that assist readers in easily understanding technology and industry trends in the world of coding and software development.

Follow Pragati On Linkedin 🡪