How to create table structure in Android programmatically not in xml file - android

I am creating an Android app for online shopping. I just want to generate invoice after payment in table format programmatically. Please guide me to create a table view in java program of Android.

This is how you create a Table Layout Programmatically for both Linear Layout and Relative Layout :
-------------------------------------------------------------------------
1) Linear Layout
TableLayout.LayoutParams tableParams = new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT);
TableRow.LayoutParams rowParams = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT);
TableLayout tableLayout = new TableLayout(context);
tableLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
TableRow tableRow = new TableRow(context);
tableRow.setLayoutParams(tableParams);
TextView textView = new TextView(context);
textView.setLayoutParams(rowParams);
tableLayout.addView(tableRow);
tableRow.addView(textView);
-------------------------------------------------------------------------
2) Relative Layout
TableLayout.LayoutParams tableParams = new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT);
TableRow.LayoutParams rowParams = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT);
TableLayout tableLayout = new TableLayout(context);
tableLayout.setLayoutParams(new Relative.LayoutParams(Relative.LayoutParams.WRAP_CONTENT, Relative.LayoutParams.WRAP_CONTENT));
TableRow tableRow = new TableRow(context);
tableRow.setLayoutParams(tableParams);
TextView textView = new TextView(context);
textView.setLayoutParams(rowParams);
tableLayout.addView(tableRow);
tableRow.addView(textView);
-------------------------------------------------------------------------

Related

The specified child already has a parent - TableLayout

I'm getting data from JSON to my object BalanceSheet and want to display the data in a Table layout but Im getting the following error,
The specified child already has a parent. You must call removeView() on the child's parent first.
// on this line
tableLayout.addView(data_row);
Here is my XML:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/balance_table_SV"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Activities.BalanceTable">
</ScrollView>
and Activity :
public class BalanceTable extends AppCompatActivity {
ScrollView balance_table_SV;
ArrayList<BalanceSheet> bsList = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_balance_table);
balance_table_SV = findViewById(R.id.balance_table_SV);
bsList = (ArrayList<BalanceSheet>) getIntent().getSerializableExtra("list");
TableLayout tableLayout = new TableLayout(this);
tableLayout.setLayoutParams( new TableLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
tableLayout.setStretchAllColumns(true);
tableLayout.removeAllViews();
int rows = bsList.size();
TableRow headers_row = new TableRow(this);
TableRow data_row = new TableRow(this);
for(int i = -1; i < rows; i ++) {
if (i == -1) {
// need check
headers_row.setLayoutParams( new TableLayout.LayoutParams());
TextView headers = new TextView(this);
headers.setLayoutParams(new
TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT));
headers.setGravity(Gravity.LEFT);
headers.setPadding(5, 15, 0, 15);
headers.setText("Inv.#");
headers.setBackgroundColor(Color.parseColor("#f0f0f0"));
headers_row.addView(headers);
tableLayout.addView(headers_row);
}
else {
data_row.setLayoutParams( new TableRow.LayoutParams());
TextView tv1 = new TextView(this);
tv1.setLayoutParams(new
TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT));
tv1.setText(String.valueOf(bsList.get(i).getName()));
TextView tv2 = new TextView(this);
tv2.setLayoutParams(new
TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT));
tv2.setText(String.valueOf(bsList.get(i).getCredit()));
TextView tv3 = new TextView(this);
tv3.setLayoutParams(new
TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT));
tv3.setText(String.valueOf(bsList.get(i).getDebt()));
TextView tv4 = new TextView(this);
tv4.setLayoutParams(new
TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT));
tv4.setText(String.valueOf(bsList.get(i).getCredit()));
data_row.addView(tv1);
data_row.addView(tv2);
data_row.addView(tv3);
data_row.addView(tv4);
}
tableLayout.addView(data_row);
}
balance_table_SV.addView(tableLayout);
}
}
This happen cause you are trying to add the same view multiple times in TableLayout. as you are looping through all of your element you have to recreate the data_row again and again for adding it into the table layout.
else{
TableRow data_row = new TableRow(this);
data_row.setLayoutParams( new TableRow.LayoutParams());
TextView tv1 = new TextView(this);
tv1.setLayoutParams(new
TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT));
tv1.setText(String.valueOf(bsList.get(i).getName()));
TextView tv2 = new TextView(this);
tv2.setLayoutParams(new
TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT));
tv2.setText(String.valueOf(bsList.get(i).getCredit()));
TextView tv3= new TextView(this);
tv3.setLayoutParams(new
TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT));
tv3.setText(String.valueOf(bsList.get(i).getDebt()));
TextView tv4 = new TextView(this);
tv4.setLayoutParams(new
TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT));
tv4.setText(String.valueOf(bsList.get(i).getCredit()));
data_row.addView(tv1);
data_row.addView(tv2);
data_row.addView(tv3);
data_row.addView(tv4);
tableLayout.addView(data_row);
}
the above block will solve the problem as it will create the new object of TableRow every time and add it to TableLayout Object
Explanation:
while adding the table row second time it will generate the error cause we are trying to add the same table row which being attached to a parent in this case we can consider it as table layout which shows the row.
So Why we require to Create the new TableRow object:
A simple answer will be the same view item can not be at the multiple places.

dynamically added table is not displayed in android

there is something going wrong , such that firstly i created separately xml file for table row ,it did not work, then i created table row and text view programatically, again it is not displayed when i run , i can not get if what is wrong with my code
table = (TableLayout) findViewById(R.id.goodsTable);
for (int i = 0; i < attrName.length; i++) {
TableRow tr = new TableRow(getApplicationContext());
tr.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
TextView atName = new TextView(getApplicationContext());
atName.setText(attrName[i]);
tr.addView(atName);
table.addView(tr, new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));
}
table.requestLayout();
Try This it may be help to you
for (int i = 0; i < attrName.length; i++) {
TableRow tr = new TableRow(this);
tr.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
TextView atName = new TextView(this);
atName.setText(attrName[i]);
atName.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
tr.addView(atName);
table.addView(tr, new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));
}
You must apply LayoutParam for TextView and for create new Object for TableRow or TextView you should use Activity Context not ApplicationContext

Android Adding more than one TextView to a TableRow Programmatically

I have a LinearLayout inside a FrameLayout for tab purposes. And I'm trying to add TableRow's to the LinearLayout in the code.
LinearLayout testLayout = (LinearLayout)findViewById(R.id.testLayout);
TableRow tableRow = new TableRow(this);
tableRow.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
This gets my LinearLayout and creates a TableRow to the specifications I want. I know this part is working.
TextView textOne = new TextView(this);
textOne.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
textOne.setText("One");
TextView textTwo = new TextView(this);
textTwo.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
textTwo.setText("Two");
Here I make my two TextViews with no problem.
tableRow.addView(textOne);
tableRow.addView(textTwo);
testLayout.addView(tableRow, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
Here is where I assume everything goes wrong. What happens is it only shows the textTwo. I don't know why it's not showing both of them in order like a normal TableRow would in XML. And I repeat, this must be done in code.
Please help, thank you.
Have you imported this import android.widget.TableRow.LayoutParams
Below code works for me
TableLayout tl = (TableLayout) findViewById(R.id.spreadsheet);
TableRow tr = new TableRow(this);
LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
tr.setLayoutParams(lp);
TextView tvLeft = new TextView(this);
tvLeft.setLayoutParams(lp);
tvLeft.setBackgroundColor(Color.WHITE);
tvLeft.setText("OMG");
TextView tvCenter = new TextView(this);
tvCenter.setLayoutParams(lp);
tvCenter.setBackgroundColor(Color.WHITE);
tvCenter.setText("It");
TextView tvRight = new TextView(this);
tvRight.setLayoutParams(lp);
tvRight.setBackgroundColor(Color.WHITE);
tvRight.setText("WORKED!!!");
tr.addView(tvLeft);
tr.addView(tvCenter);
tr.addView(tvRight);
tl.addView(tr, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
tl.addView(tr, new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));
I think you want to create your TextView dynamically and should're getting error "removeView () parent." Here is a good solution for that:
TableView tlSkills = (TableView) findViewById(R.id.myTableView);
if(listSkills.size() > 0) {
TableRow tableRow = new TableRow(getContext());
int i = 0;
for (Skills s : listSkills) {
TextView textView = new TextView(getContext());
textView.setText("" + s.getName());
tableRow.addView(textView);
if(i > 0) {
tlSkills.removeView(tableRow);//this is to avoid the error I mentioned above.
}
tlSkills.addView(tableRow);
i++;
}
}

Centering TableLayout rows using Java Code

I would to create a TableLayout and center its content.
I write this piece of code:
TableLayout tl = new TableLayout(this);
tl.setGravity(Gravity.CENTER);
TableRow firstRow = new TableRow(this);
TableRow secondRow = new TableRow(this);
But It doesn't work. Rows are centered but only vertically... How can I do?
Thanks in advance.
Edit:
TableLayout tl = new TableLayout(this);
tl.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, Gravity.CENTER_HORIZONTAL));
TableRow firstRow = new TableRow(this);
TableRow secondRow = new TableRow(this);
firstRow.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.FILL_PARENT, Gravity.CENTER));
secondRow.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.FILL_PARENT, Gravity.CENTER));
You should set a LayoutParams for the TableLayout and set the gravity on it:
tl.setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.CENTER_HORIZONTAL) );

Android - Dynamically Adding TableRow to TableLayout using an existing TableRow layout

I'm trying to adding TableRows to a TableLayout, which is working fantastically. However, I'm running into some problems with layout parameters. The spacing between the TextViews within a TableRow isn't working out like I thought it would.
My current code looks as follows.
paymentTable = (TableLayout) findViewById(R.id.paymentTable);
for(i = 0; i < cursor.getCount(); i++) {
TableRow row = new TableRow(this);
TextView payAmount = new TextView(this);
payAmount.setText(cursor.getString(cursor.getColumnIndex(DatabaseHelper.KEY_AMOUNT)));
payAmount.setPadding(payAmount.getPaddingLeft(),
payAmount.getPaddingTop(),
textView.getPaddingRight(),
payAmount.getPaddingBottom());
TextView payDate = new TextView(this);
payDate.setText(cursor.getString(cursor.getColumnIndex(DatabaseHelper.KEY_DATE)));
row.addView(payDate);
row.addView(payAmount);
paymentTable.addView(row, new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
cursor.moveToNext();
}
Afterwards, the TableLayout looks something along the lines of:
January$500
February$500
March$405
But I want it to look like:
January $500
February $500
March $405
To clarify things, I want each new TableRow and the TextViews it contains to inherit layout properties of an existing TableRow and TextView(s).
yawus this is a good tutorial will help you set the table layout
http://www.anotherandroidblog.com/2010/08/04/android-database-tutorial/
this is the xml layout
http://www.anotherandroidblog.com/2010/08/04/android-database-tutorial/6/
this is the activity code
http://www.anotherandroidblog.com/2010/08/04/android-database-tutorial/8/
TableRow tableRow= new TableRow(this);
ArrayList<Object> row = data.get(position);
TextView idText = new TextView(this);
idText.setText(row.get(0).toString());
tableRow.addView(idText);
TextView textOne = new TextView(this);
textOne.setText(row.get(1).toString());
tableRow.addView(textOne);
TextView textTwo = new TextView(this);
textTwo.setText(row.get(2).toString());
tableRow.addView(textTwo);
dataTable.addView(tableRow);
You need to add a RelativeLayout to each Table
TableRow row = new TableRow(this);
row.setId(tag);
// Creating a new RelativeLayout
RelativeLayout relativeLayout = new RelativeLayout(this);
// as the parent of that RelativeLayout is a TableRow you must use TableRow.LayoutParams
TableRow.LayoutParams rlp = new TableRow.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.MATCH_PARENT);
rlp.setMargins(0, 0, 0, 0);
row.addView(relativeLayout, rlp);
Then you add your TextView to the RelativeLayout. Like this
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
TextView payAmount = new TextView(this);
//lp.setMargins(left, top, right, bottom)
lp.setMargins(0, 0, 16, 0);
lp.addRule(RelativeLayout.ALIGN_LEFT);
payAmount.setLayoutParams(lp);
relativeLayout.addView(payAmount);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
TextView payDate = new TextView(this);
//lp.setMargins(left, top, right, bottom)
lp.setMargins(0, 0, 16, 0);
lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
payDate.setLayoutParams(lp);
relativeLayout.addView(payDate);
This is the way I handle positioning interface components within a table row.

Categories

Resources