dynamically added table is not displayed in android - 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

Related

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

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);
-------------------------------------------------------------------------

getting data from dynamically created table row containing many textviews

i created a table row dynamically, That contains three textviews.now i want to get the value of three textview's value on click on tablerow(tr).means i want to get the companyTV,valueTV & YearTV's value.
Thanks.
for (int i = 0; i < match.length; i++) {
/** Create a TableRow dynamically **/
tr.setBackground(getResources().getDrawable(R.drawable.tv_bg));
tr = new TableRow(this);
tr.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.FILL_PARENT));
/** Creating a TextView to add to the row **/
companyTV = new TextView(this);
companyTV.setText(match[i]);
companyTV.isClickable();
companyTV.setTextColor(Color.RED);
companyTV.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
companyTV.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
companyTV.setPadding(5, 5, 5, 5);
tr.addView(companyTV); // Adding textView to tablerow.
/** Creating another textview **/
valueTV = new TextView(this);
valueTV.setText(kanr[i]);
valueTV.isClickable();
valueTV.setTextColor(Color.RED);
valueTV.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
valueTV.setPadding(5, 5, 5, 5);
valueTV.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
tr.addView(valueTV); // Adding textView to tablerow.
YearTV = new TextView(this);
YearTV.setText(ort[i]);
YearTV.setTextColor(Color.RED);
YearTV.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
YearTV.setPadding(5, 5, 5, 5);
YearTV.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
tr.addView(YearTV); // Adding textView to tablerow.
// Add the TableRow to the TableLayout
tl.addView(tr, new TableLayout.LayoutParams(
TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
}
First you have one error in your code when you are setting background of table row before you have created it. You should first create new table row, and then set its background:
for (int i = 0; i < match.length; i++) {
/** Create a TableRow dynamically **/
tr = new TableRow(this);
tr.setBackground(getResources().getDrawable(R.drawable.tv_bg));
You can access table row children views by index in your OnClickListener
tr.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
TableRow tr = (TableRow)v;
TextView companyTV, valueTV, yearTV;
String company, value, year;
companyTV = (TextView) tr.getChildAt(0);
valueTV = (TextView) tr.getChildAt(1);
yearTV = (TextView) tr.getChildAt(2);
company = companyTV.getText().toString();
value = valueTV.getText().toString();
year = yearTV.getText().toString();
}
});
If you want to track which table row is currently selected, you can create global variable for your activity
TableRow selectedTR;
and then set that inside OnClickListener
selectedTR = (TableRow)v;
When you use that selectedTR variable make sure that you check it for null value in case there is no selection made yet.

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++;
}
}

Multiple rows for table in android application

I am a starter in android application development. Kindly help me how to create multiple rows in a table and key performance to create a row. I am able to create 3 rows now. but i want to autogenerate the rows while entering the datas in table.
try this:
//Declare mytable in layout XML file.
TableLayout tl = (TableLayout) findViewById(R.id.mytable);
//loop through number of times u want to create row.
for (int current = 0; current < numofrowsrequired; current++)
{
// Create a TableRow and give it an ID
TableRow tr = new TableRow(this);
tr.setId(100+current);
tr.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
//add required views with required parameters
TextView labelTV = new TextView(this);
labelTV.setId(200+current);
labelTV.setText(provinces[current]);
labelTV.setTextColor(Color.BLACK);
labelTV.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
tr.addView(labelTV);
// Add the TableRow to the TableLayout
tl.addView(tr, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
}

how to put a custom view in a table row in android?

I am creating table rows programmatically and i want to put a view that i created before in a row. Is there any way to do it?
for ( current = 0; current < rapor1.length; current++)
{
TableRow tr = new TableRow(this);
tr.setId(100+current);
tr.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
TextView f1 = new TextView(this);
f1.setId(200+current);
f1.setText(rapor1[current]);
f1.setTextColor(Color.BLACK);
f1.setWidth(100);
tr.addView(f1);
DrawView dv = new DrawView(this);
tr.addView(dv);
it should be working but something is wrong.
I think it may helps. Just change this string:
tr.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
And of course you need to set LayoutParams to any view that you want to show on your screen

Categories

Resources