Addint textview to tableview dynamically - android

I want to populate the android tableview's rows with an array at runtime...
How to go about it....please advise
Thanks,

You can populate table view row's by reading tables child as follows:
TableLayout x = (TableLayout) findViewById(R.id.table_id);
for(int i=0;i<x.getChildCount();i++){
TableRow tr = (TableRow) x.getChildAt(index);
//Do here with table row, what u want to do.
}

TableLayout layout = (TableLayout) findViewById(R.id.table);
TableRow tr = new TableRow(context);
TextView view = new TextView(context);
view.setText("Test");
tr.addView(view);
layout.addView(tr);

Related

how can i pin up the first row of a TableLayout?

I show a TableLayout with Data, the first row is the columns' name and since the second row is data, i need to pin up the first row but i have to be scrollable horizontally with the data(others rows) not vertically, but the data(others rows) just the data, have to be both(horizontally and vertically), because a i need to see always the columns' name.
I make my tablelayout like this:
TableLayout table= (TableLayout) findViewById(R.id.table);
//columns' name (first row)
TableRow column= new TableRow(this);
for(int i=0;i<lenght; i++){
TextView columnName = new TextView(this);
columnName.setText("Column "+i);
column.addView(columnName);
}
table.addView(column); //with this i add the first row with columns' name
//Data
for(int i=0;i<lenght; i++){
TableRow rowData = new TableRow(this);
TextView data= new TextView(this);
data.setText("data "+i);
rowData .addView(data);
table.addView(rowData );//add rows(since the second row)
}
With this structure i scroll all the tablelayout:
->ScrollView
->HorizontalScrollView
->TableLayout
I fix the header doing this :
<HorizontalScrollView>
<TableLayout>
<TableRow>
<TableLayout>
<--! Headers -->
</TableLayout>
</TableRow>
<TableRow>
<ScrollView>
<TableLayout>
<--! Rows/Data -->
</TableLayout>
</ScrollView>
</TableRow>
</TableLayout>
</HorizontalScrollView>
Obviously if you want it dynamic, you have to do the same but with programming.
Something like this
Your xml:
<HorizonalScrollView>
<TableLayout
android:"#+id/tableMaster">
</TableLayout>
</HorizonalScrollView>
-------------------------------------------------------------------------------------
Your jave code:
//it contains the TablesLayout
TableLayout tableFirst = (TableLayout) findViewById(R.id.tableMaster);
TableRow row1 = new TableRow(this);
TableRow row2 = new TableRow(this);
//The Header
TableLayout tableSecond = new TableLayout(this);
TableRow rowHeader = new TableRow(this);
tableSecond.addView(rowHeader);
//The Data
ScrollView scroll = new ScrollView(this);//It makes this table scrollable vertically
TableLayout tableThird = new TableLayout(this);
TableRow rowData = new TableRow(this);
tableThird.addView(rowData);
scroll.addView(tableThird);
//Add the header
row1.addView(tableSecond);
tableFirst.addView(row1);
//Add the data scrollable
row2.addView(scroll);
tableFirst.addView(row2);
You have to put your own logic.

Add row dynamically in TableLayout

I want to dynamically add a row in a table whenever this function is called from a different class. Right now it crashes the program. I looked through many topics but couldn't find anything.
public void newRow(String time, String cloc, String loc, String contact) {
TableLayout tl = (TableLayout) findViewById(R.id.table);
TableRow tr = new TableRow(this);
TextView t1 = new TextView(this);
TextView t2 = new TextView(this);
TextView t3 = new TextView(this);
TextView t4 = new TextView(this);
t1.setText(time);
t1.setText(cloc);
t1.setText(loc);
t1.setText(contact);
tr.addView(t1);
tr.addView(t2);
tr.addView(t3);
tr.addView(t4);
tl.addView(tr, TableLayout.LayoutParams.WRAP_CONTENT);
}
Don't create new instance of TableLayout on each call
Create a global instance of TableLayout so that it will be accessible
in newRow() method. Remember that you would want to creat just one
TableLayout and repeatedly add new rows to it.
You are calling setText() method repeatedly on t1 instance. I don't
think you intend to do that.
Post the crash logs so that I could help you further.

How we can align one or more textviews one below to another in table row of table layout in android through java code not xml

I am consrtucting a registration form in table layout for which I have to add various textviews in a table row one one below another.
You can get something like this:
Here is the code, you can get the basic idea.
// <table>
TableLayout table = new TableLayout(context);
// <row1>
TableRow row = new TableRow(context);
// <column1>
LinearLayout container = new LinearLayout(context);
container.setBackgroundColor(Color.parseColor("#ff0000"));
container.setOrientation(LinearLayout.VERTICAL);
container.addView(getSampleTextView("Stacked TextView 1", "#ff0000"));
container.addView(getSampleTextView("Stacked TextView 2", "#ffff00"));
row.addView(container);
// </column1>
// <column2>
row.addView(getSampleTextView("TextView in Another Column", "#ffffff"));
// </column2>
table.addView(row);
// </row1>
// <row2>
TableRow row2 = new TableRow(context);
row2.setBackgroundColor(Color.parseColor("#00ffff"));
row2.addView(getSampleTextView("TextView in Next Row", "#0000ff"));
table.addView(row2);
// </row2>
Here is how I get sample text view for demo purposes:
private TextView getSampleTextView(String text, String color) {
TextView textView = new TextView(getContext());
setLayoutParams(new TableRow.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
textView.setText(text);
textView.setBackgroundColor(Color.parseColor(color));
return textView;
}

TableLayout related issue

I want to show an array of strings into table layout. Having following code.I am able to get the data into log file but table layout i am seeing nothing.
TableLayout tl=(TableLayout)findViewById(R.id.maintable);
for(int current=0;current<cursor1.getCount();current++)
{
//create a table row
TableRow tr=new TableRow(this);
tr.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
// create a text view for start time
TextView sTime= new TextView(this);
sTime.setText(values[current]);
sTime.setTextColor(Color.YELLOW);
Log.i("Data Check", values[current]);
sTime.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
tr.addView(sTime);
//add the table row into TableLayout
tl.addView(tr, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
}
I know this is long time after but it may help you or someone else.
Where you create the row and add layout params to it , use the following type of params
TableRow.LayoutParams NOT the TableLayout.LayoutParams

Dynamically re-creating a table layout

I have a dynamic table layout created in OnCreate(). Everytime the activity resumes, the data in the table rows can change, so I would like to destroy the table rows and create new ones.
How can I do that? Thanks!!!
Here is what the OnCreate table creation portion looks like:
tableLayout tl = new TableLayout(this);
for (i = 0; i < numberOfVariables; i++) {
TableRow tr = new TableRow(this);
CheckBox cb = new CheckBox(this);
tr.addView(cb);
TextView dv = new TextView(this);
tr.addView(dv);
EditText et = new EditText(this);
tr.addView(et);
tl.addView(tr);
}
ll.addView(tl);
Just use tl.removeAllViews();, and then recreate the rows.

Categories

Resources