TableRow programmatically added does not display - android

In my xml layout, I have
<TableLayout
android:id="#+id/gridview"
android:layout_height="fill_parent"
android:layout_width="fill_parent" >
</TableLayout>
and in activity
setContentView(R.layout.main);
/* Find Tablelayout defined in main.xml */
TableLayout tl = (TableLayout)findViewById(R.id.gridview);
/* Create a new row to be added. */
TableRow tr = new TableRow(this);
tr.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
/* Create a Button to be the row-content. */
Button b = new Button(this);
b.setText("Dynamic Button");
b.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
/* Add Button to row. */
tr.addView(b);
/* Add row to TableLayout. */
tl.addView(tr,new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
But the button and textview in the tablelayout does not display. Any idea why?

Make sure you use LayoutParams of TableLayout otherwise your LayoutParams will have no effect on TableRow, something like this:
TableRow tr = new TableRow(this);
tr.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT,TableLayout.LayoutParams.WRAP_CONTENT));
or simply import only TableLayout.LayoutParams i.e
import android.widget.TableLayout.LayoutParams;

try removing this:
TableRow tr = new TableRow(this);
tr.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));

Related

Clearing a Table Layout dynamically

I am currently working on an android app where I select a customer in an activity, it retrieves all records pertaining to that customer and displays them in a table layout in a new activity.
The problem arises when I go back to the previous activity and select a different customer, the new records are added behind the records of the previous customer (meaning the previous table layout entries are not deleted when I press the back button and go to select a new customer).
I've tried 2 solutions:
tl = (TableLayout) findViewById(R.id.maintable);
tl.removeAllViewsInLayout();
and
layout = (RelativeLayout)findViewById(R.id.layout1);
tl = (TableLayout) findViewById(R.id.maintable);
int count=tl.getChildCount();
for(int i=0;i<count;i++)
tl.removeView(layout.getChildAt(i));
Both these solutions do not work for me as the new data is still being added behind the existing data.
Could anyone suggest me a different solution that could work?
Also I am attaching the entire code.
OutstandingBills.java:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_outstanding_bills);
layout = (RelativeLayout)findViewById(R.id.layout1);
tl = (TableLayout) findViewById(R.id.maintable);
tl.removeAllViewsInLayout();
int count=tl.getChildCount();
for(int i=0;i<count;i++)
tl.removeView(layout.getChildAt(i));
addHeaders();
addData();
}
public void addHeaders(){
tr = new TableRow(OutstandingBills.this);
tr.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
TextView bilDateTV = new TextView(OutstandingBills.this);
bilDateTV.setText("Date");
bilDateTV.setTextColor(Color.BLACK);
bilDateTV.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
bilDateTV.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
bilDateTV.setPadding(5, 5, 5, 0);
tr.addView(bilDateTV); // Adding textView to tablerow.
TextView billNoTV = new TextView(OutstandingBills.this);
billNoTV.setText("Bill No.");
billNoTV.setTextColor(Color.BLACK);
billNoTV.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
billNoTV.setPadding(5, 5, 5, 0);
billNoTV.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
tr.addView(billNoTV); // Adding textView to tablerow.
TextView dueAmtTV = new TextView(OutstandingBills.this);
dueAmtTV.setText("Amount Due");
dueAmtTV.setTextColor(Color.BLACK);
dueAmtTV.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
dueAmtTV.setPadding(5, 5, 5, 0);
dueAmtTV.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
tr.addView(dueAmtTV); // Adding textView to tablerow.
tl.addView(tr, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
tr = new TableRow(OutstandingBills.this);
tr.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
TextView divider = new TextView(OutstandingBills.this);
divider.setText("-----------------");
divider.setTextColor(Color.BLACK);
divider.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
divider.setPadding(5, 0, 0, 0);
divider.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
tr.addView(divider); // Adding textView to tablerow.
TextView divider2 = new TextView(OutstandingBills.this);
divider2.setText("-------------------------");
divider2.setTextColor(Color.BLACK);
divider2.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
divider2.setPadding(5, 0, 0, 0);
divider2.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
tr.addView(divider2); // Adding textView to tablerow.
TextView divider3 = new TextView(OutstandingBills.this);
divider3.setText("-------------------------");
divider3.setTextColor(Color.BLACK);
divider3.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
divider3.setPadding(5, 0, 0, 0);
divider3.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
tr.addView(divider3); // Adding textView to tablerow.
// Add the TableRow to the TableLayout
tl.addView(tr, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
}
public void addData(){
for (int i = 0; i < arr.size(); i++)
{
tr = new TableRow(OutstandingBills.this);
tr.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
bilDate = new TextView(OutstandingBills.this);
bilDate.setText(arr.get(i).toString());
bilDate.setTextColor(Color.BLACK);
bilDate.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
bilDate.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
bilDate.setPadding(5, 5, 5, 5);
tr.addView(bilDate); // Adding textView to tablerow.
bilNo = new TextView(OutstandingBills.this);
bilNo.setText(arr1.get(i).toString());
bilNo.setTextColor(Color.BLACK);
bilNo.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
bilNo.setPadding(5, 5, 5, 5);
bilNo.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
tr.addView(bilNo); // Adding textView to tablerow.
dueAmt = new TextView(OutstandingBills.this);
dueAmt.setText(arr2.get(i).toString());
dueAmt.setTextColor(Color.BLACK);
dueAmt.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
dueAmt.setPadding(5, 5, 5, 5);
dueAmt.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
tr.addView(dueAmt); // Adding textView to tablerow.
// Add the TableRow to the TableLayout
tl.addView(tr, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
}
}
OutstandingBills.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:id="#+id/layout1"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.example.ashwin.projectx1.OutstandingBills">
<TextView android:text="#string/hello_world" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollbars="none">
<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="0,1"
android:id="#+id/maintable" >
</TableLayout>
</ScrollView>
</RelativeLayout>
Implementing # Mr.Neo 's solution:
This is how I implemented your solution in my code:
addHeaders();
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
//Add view for table layout here
addData();
}
}, 2000);
But there is still no effect on the TableLayout
Screenshots:
The first time I enter the activity with a Customer name
On re-entering the activity the second time, you can see the duplicate records
Just try using tl.removeAllViews() instead of tl.removeAllViewsInLayout().
Call this method before adding views in tablelayout.
Here is the solution I am using. Using removeAllViews() and add view after seconds to completely removing. If you add immediately, some rows could not be removed. I know this is not the best solution, but it's the best for me now.
tableMainContent.removeAllViews();
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
//Add view for table layout here
}
}, 2000);

adding table layout programmatically in android

I'm adding table in linearlayout programmatically but it is not displaying on screen.
Below is code -
public void displayTable(){
TableLayout.LayoutParams lp2 = new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT);
TableLayout mTableLayout=new TableLayout(getActivity());
TableRow mTableRow;
LinearLayout mTableLinearLayout;
TextView mSrNotxt,mDatetxt,mTimetxt;
Date mDate=new Date();
String mCDate=mDate.getDate()+"-"+(mDate.getMonth()+1)+"-"+(mDate.getYear()+1900)+"";
mTableLayout.setLayoutParams(lp2);
TableRow.LayoutParams lp = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT);
TableRow.LayoutParams tlp = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT);
for(int count=0;count<mTimeList.size();count++)
{
mTableRow=new TableRow(getActivity());
mTableRow.setLayoutParams(lp);
mSrNotxt=new TextView(getActivity());
mDatetxt=new TextView(getActivity());
mTimetxt=new TextView(getActivity());
mSrNotxt.setTextColor(getResources().getColor(R.color.black));
mDatetxt.setTextColor(getResources().getColor(R.color.black));
mTimetxt.setTextColor(getResources().getColor(R.color.black));
mSrNotxt.setLayoutParams(tlp);
mDatetxt.setLayoutParams(tlp);
mTimetxt.setLayoutParams(tlp);
mSrNotxt.setTextSize(12);
mDatetxt.setTextSize(12);
mTimetxt.setTextSize(12);
if(count==0){
mSrNotxt.setText("No.");
mDatetxt.setText("Date");
mTimetxt.setText("Time in Min.");
}
else{
mSrNotxt.setText((count+1+""));
mDatetxt.setText(mCDate);
mTimetxt.setText(mTimeList.get(count));
}
mTableLinearLayout=new LinearLayout(getActivity());
mTableLinearLayout.setLayoutParams(lp);
mTableLinearLayout.setOrientation(LinearLayout.HORIZONTAL);
mTableRow.addView(mTableLinearLayout);
mTableLayout.addView(mTableRow);
}
System.out.println("table created");
mChartLayout.addView(mTableLayout);
}
xml view for chartLayout -
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="#+id/chartView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
How can I display table?Is there any way?
Answer -
Need to add textview in view -
mTableLinearLayout.addView(mSrNotxt);
mTableLinearLayout.addView(mDatetxt);
mTableLinearLayout.addView(mTimetxt);
I am able to add Table using this code :
TableLayout mTableLayout = new TableLayout(this);
mTableLayout.setLayoutParams(new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
for (int count = 0; count < 3; count++) {
TableRow row = new TableRow(this);
row.setLayoutParams((new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT)));
TextView valueTV = new TextView(this);
valueTV.setText("text : "+count);
// valueTV.setId(5);
valueTV.setLayoutParams(new TableRow.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
row.addView(valueTV);
mTableLayout.addView(row);
}
mChartLayout.addView(mTableLayout);
Add other required properties
Hope it helps ツ

Add TableRow dynamically to linearlayout android

I need to dynamically add some tablerows to a linearlayout in my app. I write this code:
LinearLayout tabella = (LinearLayout) findViewById(R.id.tabella_contatori);
for(int i =0; i<array_list.size(); i++){
TableRow row = new TableRow(getApplicationContext());
row.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
TextView data = new TextView(getApplicationContext());
data.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 0.2f));
data.setTextAppearance(getApplicationContext(), android.R.attr.textAppearanceMedium);
data.setTextColor(Color.BLACK);
data.setBackgroundColor(Color.WHITE);
data.setPadding(2, 0, 0, 0);
data.setText("asd");
row.addView(data);
tabella.addView(row);
}
}
But when I open the app nothing appens. I already check if array_list.size is bigger than 0.
How can I do?
Thanks, Mattia
The problem is in your TextView Layout Params. The type should be TableRow.LayoutParams not LinearLayout.LayoutParams.
data.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
Take Table layout and try using this in your main.xml ...
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/myTableLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView android:text="Some Text"/>
</TableRow>
</TableLayout>
in your Activity
this.setContentView(R.layout.main);
/* Find Tablelayout defined in main.xml */
TableLayout tl = (TableLayout)findViewById(R.id.myTableLayout);
/* Create a new row to be added. */
TableRow tr = new TableRow(this);
tr.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
/* Create a TextView to be the row-content. */
TextView data = new TextView(getApplicationContext());
data.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, 0.2f));
data.setTextAppearance(getApplicationContext(), android.R.attr.textAppearanceMedium);
data.setTextColor(Color.BLACK);
data.setBackgroundColor(Color.WHITE);
data.setPadding(2, 0, 0, 0);
data.setText("asd");
/* Add TextView to row. */
tr.addView(data);
/* Add row to TableLayout. */
tl.addView(tr,new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));

No Textview occurs in TableLayout

No Textview occurs in this TableLayout. I don't know why.
I want to make the entries manually.
Another question, how can I add a horizontal line?
Code:
super.onCreate(savedInstanceState);
setContentView(R.layout.overview);
loadData();
TableLayout tl = (TableLayout)findViewById(R.id.tl);
TableRow tr = new TableRow(this);
TextView tv = new TextView(this);
TextView tv2 = new TextView(this);
//tv.se
tv.setGravity(Gravity.LEFT);
tv2.setGravity(Gravity.RIGHT);
tv.setText("Test");
tv2.setText("Test ");
tv.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f));
tv2.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f));
//tv.setTextSize(50);
//tv2.setTextSize(50);
tr.addView(tv);
tr.addView(tv2);
tl.addView(tr);
setContentView(tl);
Layout:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tl">
</TableLayout>
The problem could be that you aren't setting any LayoutParams for your TableRow.
Try doing something like this:
tr.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
Also if you want to add a horizontal line you can simply add a new View object and set its layout_height to 1dp and its backgroundColor to Color.BLACK, or whatever height/color combination you want
setContentView(R.layout.main);
TableLayout tl = (TableLayout)findViewById(R.id.tl);
TableRow tr = new TableRow(this);
TextView tv = new TextView(this);
TextView tv2 = new TextView(this);
//tv.se
tv.setGravity(Gravity.LEFT);
tv2.setGravity(Gravity.RIGHT);
tv.setText("Test");
tv2.setText("Test ");
tr.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
// tv2.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f));
//tv.setTextSize(50);
//tv2.setTextSize(50);
tr.addView(tv);
tr.addView(tv2);
tl.addView(tr);
main.xml
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/tl">
</TableLayout>
add new horizontal line using

Only one TextView occurs in TableLayout

I need one in the left and the right. All configurations should be done by code. Screenshot:
Code:
super.onCreate(savedInstanceState);
setContentView(R.layout.overview);
loadData();
TableLayout tl = (TableLayout)findViewById(R.id.tl);
TableRow tr = new TableRow(this);
TableRow tr2 = new TableRow(this);
tr.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
//tr2.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
//tr2.setLayoutParams(new ViewGroup.LayoutParams(L., LayoutParams.WRAP_CONTENT));
TextView tv = new TextView(this);
TextView tv2 = new TextView(this);
//tv.se
tv.setGravity(Gravity.LEFT);
tv2.setGravity(Gravity.RIGHT);
tv.setText("Test");
tv2.setText("Test");
//tv.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, 1f));
//tv.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f));
tv2.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, 1f));
//tv.setTextSize(50);
//tv2.setTextSize(50);
tr.addView(tv);
tr.addView(tv2);
//tl.addView(tr);
//setContentView(tl);
tl.addView(tr, new TableLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
}
Layout:
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/tl"
android:stretchColumns="*">
</TableLayout>
LayoutParams are used by views to tell their parents how they want to be laid out.
There are around 13 different types of LayoutParams defined in Android. eg. LinearLayout.Layoutparams, ViewGroup.LayoutParams, ActionBar.LayoutParams etc.
So if one is adding TextView to LinearLayout, it means LinearLayout is the parent of TextView, so while defining layout parameteres for textView one should use
LinearLayout.LayoutParams and not ViewGroup.LayoutParams
in your case you are adding two textviews to table row, so but obvious you should use TableRow.LayoutParams
so replace this line
tv2.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, 1f));
with
tv2.setLayoutParams(new TableRow.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, 1f));
Write similarly for tv
This was my first post on this site. Hope it will be Helpful :)
I think you have to use:
android:stretchColumns="0,1"
Great Example can be found here
do this:
remove 1f from tv2 layoutparams.
tl.addView(tr, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
this may solve you problem according to given link.

Categories

Resources