TableRow not displaying all TextViews - android

I am creating a TableRow at runtime. The trouble I have is that I have 6 TextViews created and added to the TableRow but for some reason only 3 of the TextViews appear, the 1st 2 and the last one. I know that there is no problem adding the TextView to the TableRow because if I comment out the last 2 TextViews the fourth one still overwrites the third. Because of this it seems to be that there is a limit somewhere on the number of columns I can add to this TableRow.
Any ideas where I am going wrong?
Here is my java
public class locationlist extends Activity {
/** Called when the activity is first created. */
private locationListDbAdapter mDbHelper;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.locationlist);
mDbHelper = new locationListDbAdapter(this);
mDbHelper.open();
String id = mDbHelper.fetchRow(1).getString(0);
String locationText = mDbHelper.fetchRow(1).getString(1);
String localCode = mDbHelper.fetchRow(1).getString(2);
String localService = mDbHelper.fetchRow(1).getString(3);
String localComments = mDbHelper.fetchRow(1).getString(4);
String localNextes = mDbHelper.fetchRow(1).getString(5);
TextView idView = new TextView(this);
idView.setText(id);
TextView locationView = new TextView(this);
locationView.setText(locationText);
TextView codeView = new TextView(this);
codeView.setText(localCode);
TextView serviceView = new TextView(this);
codeView.setText(localService);
TextView commentsView = new TextView(this);
codeView.setText(localComments);
TextView nextesView = new TextView(this);
codeView.setText(localNextes);
TableLayout tl = (TableLayout)findViewById(R.id.tableLayout1);
TableRow r2 = new TableRow(this);
r2.addView(idView);
r2.addView(locationView);
r2.addView(codeView);
r2.addView(serviceView);
r2.addView(commentsView);
r2.addView(nextesView);
tl.addView(r2);
this.setContentView(tl);
This is the xml file I am pointing it at
<?xml version="1.0" encoding="utf-8"?>
<TableLayout android:id="#+id/tableLayout1" android:layout_width="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="wrap_content">
<TableRow android:layout_height="wrap_content" android:id="#+id/tableRow1" android:layout_width="fill_parent">
<TextView android:id="#+id/textView11" android:layout_height="wrap_content" android:text="ID" android:layout_width="0dip" android:layout_weight="0.1"></TextView>
<TextView android:id="#+id/textView1" android:layout_height="wrap_content" android:text="Location" android:layout_width="0dip" android:layout_weight="0.4"></TextView>
<TextView android:id="#+id/textView2" android:layout_height="wrap_content" android:text="Type" android:layout_width="0dip" android:layout_weight="0.15"></TextView>
<TextView android:id="#+id/textView3" android:layout_height="wrap_content" android:text="Service" android:layout_width="0dip" android:layout_weight="0.2"></TextView>
<TextView android:id="#+id/textView4" android:layout_height="wrap_content" android:text="Comments" android:layout_weight="0.3" android:layout_width="0dip"></TextView>
<TextView android:id="#+id/textView5" android:layout_height="wrap_content" android:text="ES" android:layout_width="0dip" android:layout_weight="0.15"></TextView>
</TableRow>
<View
android:layout_height="2dip"
android:background="#FF909090" />
</TableLayout>

Why do you have the following lines? You set your codeView TextView to localCode's text, then you overwrite that three times for no discernible reason:
codeView.setText(localCode);
codeView.setText(localService);
codeView.setText(localComments);
codeView.setText(localNextes);
Your serviceView, commentsView and nextesView TextView's all appear to be blank, you never do anything like this:
serviceView.setText(localService);
commentsView.setText(localComments);
nextesView.setText(localNextes);
I would temporarily replace your database stuff with test strings so as to narrow down the error, see if it is still failing, if not, work on the things mentioned above, test again, if it is working now, replace the temporary text strings with the database stuff.

Related

Android. Programmatically added rows are not stretching well (TableLayout)

I have a TableLayout declared in a XML File, it will show scores, and these scores are added via rows programmatically. The problem is that the layout without any score added looks like this (As it should be):
Correct
But when a score's row is added, it looks like this:
Wrong
I can’t understand why this is happening, I would like to see the columns well stretched and there’s no apparent reason that explains this behavior. If anyone can help me i'll be very grateful.
Thanks in advance and sorry for my english.
Here is my XML and my code:
XML:
<TableLayout xmlns...
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:stretchColumns="*"
android:layout_column="5"
android:id="#+id/scoresTable">
<TableRow
android:layout_gravity="center_horizontal"
android:id="#+id/titleScoresRow"
android:layout_height="42dp">
<TextView
android:id="#+id/scoresTitle"
android:layout_width="match_parent"
android:layout_height="40dp"
android:text="Best Scores"
android:layout_span="5"
android:gravity="center"
/>
</TableRow>
<TableRow
android:layout_gravity="center_horizontal"
android:id="#+id/firstRow"
android:layout_height="42dp">
<TextView
android:id="#+id/MODE"
android:layout_width="match_parent"
android:layout_height="40dp"
android:text="MODE"
android:gravity="center"
/>
<TextView
android:id="#+id/KANA"
android:layout_width="match_parent"
android:layout_height="40dp"
android:text="KANA"
android:gravity="center"
/>
<TextView
android:id="#+id/DIFF"
android:layout_width="match_parent"
android:layout_height="40dp"
android:text="DIFF"
android:gravity="center"
/>
<TextView
android:id="#+id/SCORE"
android:layout_width="match_parent"
android:layout_height="40dp"
android:text="SCORE"
android:gravity="center"
/>
<TextView
android:id="#+id/DATE"
android:layout_width="match_parent"
android:layout_height="40dp"
android:text="DATE"
android:gravity="center"
/>
</TableRow>
Activity:
TableLayout table = (TableLayout) findViewById(R.id.scoresTable);
createOrderedCSV(getApplicationContext(), 4, ":");
ArrayList<String> scores = readScores("OrderedScoresCSV.csv");
// Score rows
for (int i = scores.size() - 1; i >= 0; i--) {
String[] currentScore = scores.get(i).split(":");
TextView mode = new TextView(this);
mode.setText(currentScore[0]);
TextView kana = new TextView(this);
kana.setText(currentScore[2]);
TextView diff = new TextView(this);
diff.setText(currentScore[1]);
TextView score = new TextView(this);
score.setText(currentScore[4]);
TextView date = new TextView(this);
date.setText(currentScore[3]);
TableRow row = new TableRow(this);
row.addView(mode);
row.addView(kana);
row.addView(diff);
row.addView(score);
row.addView(date);
table.addView(row, new TableLayout.LayoutParams(
TableLayout.LayoutParams.FILL_PARENT,
TableLayout.LayoutParams.WRAP_CONTENT));
}
TableRow buttonRow = new TableRow(this);
Button eraseScores = new Button(this);
eraseScores.setWidth(200);
eraseScores.setHeight(60);
eraseScores.setText("Erase Scores");
buttonRow.setGravity(Gravity.CENTER_HORIZONTAL);
buttonRow.addView(eraseScores);
table.addView(buttonRow);
It was caused by the button row that I added at the end. It was attached to the first column.

Incorrect order for layouts inside a TableRow

My code is as below and the result is like in the image. Any idea how to move the "test" to the back and "check", "session", "set" to in front? I thought the layout will follow the sequence to display, I have no idea why it doesn't follow the sequence and moves "test" in front.
TableRow.LayoutParams rowParams2 = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT);
TableRow trNew1 = (TableRow) findViewById(R.id.tr13);
TextView textView18 = new TextView(this);
textView18.setGravity(Gravity.CENTER);
textView18.setBackgroundResource(R.drawable.cell_shape);
textView18.setText("Check");
textView18.setLayoutParams(rowParams2);
trNew1.addView(textView18,0);
TextView textView19 = new TextView(this);
textView19.setGravity(Gravity.CENTER);
textView19.setBackgroundResource(R.drawable.cell_shape);
textView19.setText("Session");
textView19.setLayoutParams(rowParams2);
trNew1.addView(textView19,1);
TextView textView20 = new TextView(this);
textView20.setGravity(Gravity.CENTER);
textView20.setBackgroundResource(R.drawable.cell_shape);
textView20.setText("Set");
textView20.setLayoutParams(rowParams2);
trNew1.addView(textView20,2);
TextView textView7 = (TextView) findViewById(R.id.tv);
//TableRow.LayoutParams params = (TableRow.LayoutParams)textView7.getLayoutParams();
rowParams2.span = 6;
textView7.setLayoutParams(rowParams2);
The xml part:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:stretchColumns="*">
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/tr13"
>
<TableLayout
android:layout_height="wrap_content"
android:layout_marginRight="4dp"
android:id="#+id/tl"
android:background="#drawable/cell_shape"
android:stretchColumns="*">
<TableRow
android:background="#drawable/cell_shape"
android:id="#+id/tr4"
>
<TextView
android:id="#+id/tv"
android:layout_gravity="center"
android:text="Test" />
</TableRow>
<TableRow
android:id="#+id/tr"
>
</TableRow>
</TableLayout>
</TableRow>
</TableLayout>
Result image:
i have no idea why it not follow the sequence and move the test in
front.
This is happening because the TableLayout containing the "Test" TextView is already in the layout and the basic addView() method takes this in consideration when adding new views. If you want a different order then use the addView() method that also takes an int representing the position of the child in the parent:
// add textView18 as the first child of the parent(initially the TableLayout tl is at position 0)
trNew1.addView(textView18, 0);
// add as the second child(positions are 0 based)
trNew1.addView(textView19, 1);
// add as the third child, at this moment the TableLayout tl will be at position 3
trNew1.addView(textView20, 2);

TableLayout error in Android

I have a XML file containing TableLayout as follows:
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10dp" >
<TableLayout
android:id="#+id/resulttable"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:weightSum="3" >
<TableRow
android:id="#+id/tr"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/tv_discription"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<TextView
android:id="#+id/tv_left_vehicle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<TextView
android:id="#+id/tv_right_vehicle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</TableRow>
</TableLayout>
</ScrollView>
In the above table I want to show array list of string, for this I have done following code:
TableLayout table = (TableLayout) findViewById(R.id.resulttable);
for(int i=0;i<DArray.size();i++)
{
TableRow row = (TableRow)findViewById(R.id.tr);
String discription = DArray.get(i);
String leftVehicle = LArray.get(i);
String rightVehicle = RArray.get(i);
TextView tvD = (TextView)findViewById(R.id.tv_discription);
tvD.setText(discription);
TextView tvlPrice = (TextView)findViewById(R.id.tv_left_vehicle);
tvlPrice.setText(leftVehicle);
TextView tvrPrice = (TextView)findViewById(R.id.tv_right_vehicle);
tvrPrice.setText(rightVehicle);
row.addView(tvD);
row.addView(tvlPrice);
row.addView(tvrPrice);
table.addView(row);
}
but this code is arising exception. Here is LogCat error for my code.
E/AndroidRuntime(1897): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
I am beginner for this situation please help me find out the problem.
i think you should Try this way
TableLayout table = (TableLayout) findViewById(R.id.resulttable);
TableRow row = (TableRow)findViewById(R.id.tr);
TextView tvD = (TextView)findViewById(R.id.tv_discription);
TextView tvlPrice = (TextView)findViewById(R.id.tv_left_vehicle);
TextView tvrPrice = (TextView)findViewById(R.id.tv_right_vehicle);
for(int i=0;i<DArray.size();i++)
{
String discription = DArray.get(i);
String leftVehicle = LArray.get(i);
String rightVehicle = RArray.get(i);
tvD.setText(discription);
tvlPrice.setText(leftVehicle);
tvrPrice.setText(rightVehicle);
row.addView(tvD);
row.addView(tvlPrice);
row.addView(tvrPrice);
table.addView(row,id);
}
Provide id for row positioning
The thing is that you added in the xml the row and then you add it again and again in the loop.
If you want to have a TableLayout and add rows on it dinamicaly then you might want to create the row with its content in another xml file and inflate that xml in the for loop and then add the row to the table.
So, lets say you have the following inside another xml called row.xml:
<TableRow
android:id="#+id/tr"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/tv_discription"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<TextView
android:id="#+id/tv_left_vehicle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<TextView
android:id="#+id/tv_right_vehicle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</TableRow>
And the TableLayout in another one (xml file):
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10dp" >
<TableLayout
android:id="#+id/resulttable"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:weightSum="3" >
</TableLayout>
</ScrollView>
Then you would have this in the code:
TableLayout table = (TableLayout) findViewById(R.id.resulttable);
for(int i=0; i < DArray.size(); i++){
//get the row from the xml and add it on the table at each loop step
TableRow row = getActivity().getLayoutInflater() .inflate(R.layout.row, table, true);
String discription = DArray.get(i);
String leftVehicle = LArray.get(i);
String rightVehicle = RArray.get(i);
// get the labels from the row and populate themm with data, note that you don't have to add them again as they are allready added..
TextView tvD = (TextView)row.findViewById(R.id.tv_discription);
tvD.setText(discription);
TextView tvlPrice = (TextView)row.findViewById(R.id.tv_left_vehicle);
tvlPrice.setText(leftVehicle);
TextView tvrPrice = (TextView)row.findViewById(R.id.tv_right_vehicle);
tvrPrice.setText(rightVehicle);
}
You might have to tweak the code above a bit as I wrote it from my mind and didn't tested it but that should be the trick.
Why Cant you use listview and custom adapters for this implementation?. which will make your life easier.
Okay the second potential problem is also: table.addView(row);
rowgets added every single loop iteration but it's the same view. You can only add a view once to the hierarchy. You probably want a new instance for each loop iteration. I can't say for sure but this is likely the problem. or try to add with index position in viewgroup like
table.addView(row, index);

How to dispaly result list into TableLayout row in android?

I have a little bit problem that displaying list data to TextView in Android. My scenario is I have a TableLayout with Default One TableRow. Inside table row, I has been created new LinearLayout. Then, Four TextView created inside LinearLayout. I adding some default value to this textview. My default value is
1, 2014-02-05, S02, 20
Here my code snippet for above scenari0.
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#color/sync_history_table_color"
android:id="#+id/history_table"
android:orientation="vertical"
>
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingTop="1dp"
android:id="#+id/row_start">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="30dp" android:id="#+id/row_linear" android:layout_weight="1"
android:background="#color/isp_home_color">
<TextView android:layout_width="0dp" android:layout_height="match_parent" android:text="#string/sync_default_no"
android:id="#+id/history_display_no" android:layout_weight="0.165"
android:gravity="center_vertical|left" android:paddingLeft="10dp"
android:textColor="#color/sync_history_text_color"/>
<TextView android:layout_width="0dp"
android:layout_height="match_parent"
android:text="#string/sync_default_date"
android:id="#+id/history_display_date"
android:layout_weight="0.5"
android:layout_gravity="center"
android:gravity="center"
android:textColor="#color/sync_history_text_color"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="#string/sync_default_order"
android:id="#+id/history_display_orderid"
android:layout_weight="0.5"
android:layout_gravity="center"
android:gravity="center"
android:textColor="#color/sync_history_text_color"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="#string/sync_default_qty"
android:id="#+id/history_display_quantity" android:layout_weight="0.5" android:layout_gravity="center"
android:gravity="center"
android:textColor="#color/sync_history_text_color"/>
</LinearLayout>
</TableRow>
</TableLayout>
Actually, I want to create dynamically TableRow, LinearLayout and TextView. Then, I put the list result to add these. The list result come from sqlite db. But, I can't get what I want. Here my code for looping the list and display the result. But, I got my some default value. Please pointing to me how can I get this. Thanks.
Here snippet for display list.
public void displayHistory(){
List<IspHistoryListObject> historyList = sqlaccess.getAllItems();
TableLayout showRow = (TableLayout) findViewById(R.id.history_table);
int count = 0;
for(IspHistoryListObject hl : historyList) {
count++;
TableRow tr = new TableRow(this);
LinearLayout ll = new LinearLayout(this);
ll.setLayoutParams(new LinearLayout.LayoutParams(50,30));
TextView tv_sync_no = new TextView(this);
TextView tv_sync_date = new TextView(this);
TextView tv_sync_orderid = new TextView(this);
TextView tv_sync_qty = new TextView(this);
tv_sync_no.setText(String.valueOf(count));
tv_sync_date.setText(hl.getSyncDate().substring(0,10));
tv_sync_orderid.setText(hl.getSyncOrderIdRef());
tv_sync_qty.setText(String.valueOf(hl.getQty()));
ll.addView(tv_sync_no);
ll.addView(tv_sync_date);
ll.addView(tv_sync_orderid);
ll.addView(tv_sync_qty);
tr.addView(ll);
showRow.addView(tr);
}
}
try this way here I gave sample or demo code you have to modify as per your requirement and still have problem let me know
main layout
1. table.xml
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/tableLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp"
android:orientation="vertical">
</TableLayout>
table row layout
2. table_item.xml
<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/history_display_no"
android:layout_weight="0.25"
android:gravity="center"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:id="#+id/history_display_date"
android:layout_weight="0.25"
android:gravity="center"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:id="#+id/history_display_orderid"
android:layout_weight="0.25"
android:gravity="center"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:id="#+id/history_display_quantity"
android:layout_weight="0.25"
android:gravity="center"/>
</TableRow>
activity class
3. Activity
public class MyActivity extends Activity {
private TableLayout tableLayout;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.table);
tableLayout=(TableLayout)findViewById(R.id.tableLayout);
for (int i=0;i<5;i++){
View tableRow = LayoutInflater.from(this).inflate(R.layout.table_item,null,false);
TextView history_display_no = (TextView) tableRow.findViewById(R.id.history_display_no);
TextView history_display_date = (TextView) tableRow.findViewById(R.id.history_display_date);
TextView history_display_orderid = (TextView) tableRow.findViewById(R.id.history_display_orderid);
TextView history_display_quantity = (TextView) tableRow.findViewById(R.id.history_display_quantity);
history_display_no.setText(""+(i+1));
history_display_date.setText("2014-02-05");
history_display_orderid.setText("S0"+(i+1));
history_display_quantity.setText(""+(20+(i+1)));
tableLayout.addView(tableRow);
}
}
}
check out context hierachy
for(IspHistoryListObject hl : historyList) {
count++;
TableRow tr = new TableRow(showRow.getContext());
LinearLayout ll = new LinearLayout(tr.getContext());
ll.setLayoutParams(new LinearLayout.LayoutParams(50,30));
TextView tv_sync_no = new TextView(ll.getContext());
TextView tv_sync_date = new TextView(ll.getContext());
TextView tv_sync_orderid = new TextView(ll.getContext());
TextView tv_sync_qty = new TextView(ll.getContext());
tv_sync_no.setText(String.valueOf(count));
tv_sync_date.setText(hl.getSyncDate().substring(0,10));
tv_sync_orderid.setText(hl.getSyncOrderIdRef());
tv_sync_qty.setText(String.valueOf(hl.getQty()));
ll.addView(tv_sync_no);
ll.addView(tv_sync_date);
ll.addView(tv_sync_orderid);
ll.addView(tv_sync_qty);
tr.addView(ll);
showRow.addView(tr);
}
With listview you can easily do this:
ActivityMain.java
ListView lst = (ListView) findViewById(R.id.listView);
View header = getLayoutInflater().inflate(R.layout.list_header, null);
lst.addHeaderView(header);
lst.setAdapter(new CustomerListAdapter(this,4));
list_header.xml is header layout

Android scrollable TableLayout with a dynamic fixed header

I have a TableLayout inside a ScrollView, that is I have a scrollable TableLayout! It is populated dynamically when I create Dialog's in different places of an Activity.
Actually everything works fine, but the fact that for every case there's a different header (with a title for each column) on that table and it scrolls along with the rows. As the content of that table is dynamic, the amount (as well as the width) of columns is unknown.
So, basically, that's my problem. I don't see point in posting code, as I need mostly a suggestion/workaround, but if that would help overcome the issue, let me know. Would appreciate some samples very much.
Here is a way :
http://sdroid.blogspot.com/2011/01/fixed-header-in-tablelayout.html
Note that there is a way to have this work every time : what you have to do is create a similar dummy row in the header, and set the texts in both dummies to the longest string you can find in the row (in number of chars, or, even better, do the comparison with Paint.getTextWidths)
package com.test.test;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
public class TestProjectTestActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TableLayout tl_head1 = (TableLayout)findViewById(R.id.tl_head);
TableLayout tl_child1 = (TableLayout)findViewById(R.id.tl_child);
String headcol1="";
TableRow tr[]= new TableRow[40];
TextView tv[] = new TextView[1000];
for(int i=0; i <=30; i++)
{
tr[i]=new TableRow(this);
for(int j=1; j<=5; j++)
{
tv[j]=new TextView(this);
tv[j].setId(j);
tv[j].setText("testingmultiple data hello"+""+j);
tv[j].setTextColor(Color.WHITE);
if(headcol1.length() < tv[j].getText().length())
{
headcol1=null;
headcol1=tv[j].getText().toString();
}
tr[i].addView(tv[j]);
}
tl_child1.addView(tr[i]);
}
TableRow trhead= new TableRow(this);
TextView tvhead[] = new TextView[5];
for(int i=0;i<=4;i++)
{
tvhead[i] = new TextView(this);
tvhead[i].setTextColor(Color.WHITE);
tvhead[i].setHeight(0);
tvhead[i].setText(headcol1);
trhead.addView(tvhead[i]);
}
tl_head1.addView(trhead);
}
}
<?xml version="1.0" encoding="utf-8"?>
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/hsv_main"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="#+id/ll_main"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TableLayout
android:id="#+id/tl_head"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TableRow
android:id="#+id/tr_head"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/tv_hidden1"
android:layout_gravity="center_horizontal"
android:text="12"
android:textColor="#FFFFFF"
/>
<TextView
android:id="#+id/tv_hidden2"
android:layout_gravity="center_horizontal"
android:text="123"
android:textColor="#FFFFFF"
/>
<TextView
android:id="#+id/tv_hidden3"
android:layout_gravity="center_horizontal"
android:text="1234"
android:textColor="#FFFFFF"
/>
<TextView
android:id="#+id/tv_hidden4"
android:layout_gravity="center_horizontal"
android:text="12345"
android:textColor="#FFFFFF"
/>
<TextView
android:id="#+id/tv_hidden5"
android:layout_gravity="center_horizontal"
android:text="123456"
android:textColor="#FFFFFF"
/>
</TableRow>
</TableLayout>
<ScrollView
android:id="#+id/sv_child"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TableLayout
android:id="#+id/tl_child"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</TableLayout>
</ScrollView>
</LinearLayout>
</HorizontalScrollView>
Android scrollable TableLayout with a dynamic fixed header is very simple. Follow this steps
Step 1:
Create a TableLayout and use android:layout_alignParentTop="true" this will keep this layout always in top.
Step 2:
With in ScrollView create another TableLayout for contents with out Rows (For Dynamic content).
Step 3:
Add Rows and columns dynamically. While adding each column (TextView) assign the width to an temporary variable.
Ex
tvContentText.measure(0, 0);
tempmaxwidth=tvContentText.getMeasuredWidth();
Step 4:
Check for max width and assign max width to main variable.
Ex:
if (tempmaxwidth > maxwidth)
maxwidth=tempmaxwidth;
Step 5:
After assigning content details. Assign the max width of each column to its header column.
Ex:
TextView tvh1 = (TextView) findViewById(R.id.h1);
tvh1.setWidth(maxwidth);
The xml and code is below
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".MainActivity">
<TableLayout
android:id="#+id/headertbl"
android:layout_alignParentTop="true"
android:layout_width="match_parent"
android:background="#color/colorPrimary"
android:layout_height="wrap_content"
android:stretchColumns="*">
<TableRow>
<TextView
android:id="#+id/h1"
android:layout_height="wrap_content"
android:text="H1"
android:textStyle="bold"
android:textAlignment="center"
android:textSize="20sp" />
<TextView
android:id="#+id/h2"
android:layout_height="wrap_content"
android:text="H2"
android:textStyle="bold"
android:textAlignment="center"
android:textSize="20sp" />
<TextView
android:id="#+id/h3"
android:layout_height="wrap_content"
android:text="H3"
android:textStyle="bold"
android:textAlignment="center"
android:textSize="20sp" />
<TextView
android:id="#+id/h4"
android:layout_height="wrap_content"
android:text="H4"
android:textStyle="bold"
android:textAlignment="center"
android:textSize="20sp" />
</TableRow>
</TableLayout>
<ScrollView
android:layout_below="#id/headertbl"
android:id="#+id/container"
android:layout_marginTop="10dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical">
<TableLayout
android:id="#+id/contenttbl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stretchColumns="*">
</TableLayout>
</ScrollView>
</RelativeLayout>
MainActivity.xml
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TableLayout tl = (TableLayout) findViewById(R.id.contenttbl);
int tempmaxwidth=0,maxwidth=0;
int tempmaxwidth1=0,maxwidth1=0;
int tempmaxwidth2=0,maxwidth2=0;
int tempmaxwidth3=0,maxwidth3=0;
for(int i=0;i<50;i++)
{
TableRow newRow = new TableRow(getApplicationContext());
newRow.setId(i);
TextView tvContentText = new TextView(getApplicationContext());
if(i!=6)
tvContentText.setText("Content 1");
else
tvContentText.setText("---- Content with Max width");
tvContentText.setTextColor(Color.BLACK);
tvContentText.setTextSize(16f);
tvContentText.measure(0, 0);
tempmaxwidth=tvContentText.getMeasuredWidth();
if (tempmaxwidth>maxwidth)
maxwidth=tempmaxwidth;
TextView tvContentText1 = new TextView(getApplicationContext());
//tvContentText1.setText(Integer.toString(maxwidth));
tvContentText1.setText("Content 2");
tvContentText1.setTextColor(Color.BLACK);
tvContentText1.setTextSize(16f);
tvContentText1.measure(0, 0);
tempmaxwidth1=tvContentText1.getMeasuredWidth();
if (tempmaxwidth1>maxwidth1)
maxwidth1=tempmaxwidth1;
TextView tvContentText2 = new TextView(getApplicationContext());
tvContentText2.setText("Content 3");
tvContentText2.setTextColor(Color.BLACK);
tvContentText2.setTextSize(16f);
tvContentText2.measure(0, 0);
tempmaxwidth2=tvContentText2.getMeasuredWidth();
if (tempmaxwidth2>maxwidth2)
maxwidth2=tempmaxwidth2;
TextView tvContentText3 = new TextView(getApplicationContext());
tvContentText3.setText("Content 4");
tvContentText3.setTextColor(Color.BLACK);
tvContentText3.setTextSize(16f);
tvContentText3.measure(0, 0);
tempmaxwidth3=tvContentText3.getMeasuredWidth();
if (tempmaxwidth3>maxwidth3)
maxwidth3=tempmaxwidth3;
newRow.addView(tvContentText);
newRow.addView(tvContentText1);
newRow.addView(tvContentText2);
newRow.addView(tvContentText3);
tl.addView(newRow);
}
// Assigning Max width to header column
TextView tvh1 = (TextView) findViewById(R.id.h1);
tvh1.setWidth(maxwidth);
TextView tvh2 = (TextView) findViewById(R.id.h2);
tvh2.setWidth(maxwidth1);
TextView tvh3= (TextView) findViewById(R.id.h3);
tvh3.setWidth(maxwidth2);
TextView tvh4= (TextView) findViewById(R.id.h4);
tvh4.setWidth(maxwidth3);
}
Well for me I didn't want to go through the trouble of worrying about alignment of columns so I created this first of all:
public static Boolean SET_TABLE_HEADER;
Then I created this function to set my TableHeader:
public void setTableHeader(TableLayout tbl) {
TableRow tr = new TableRow(context);
TextView tcView = new TextView(context);
tcView.setText("Class");
tcView.setTextColor(Color.BLACK);
tcView.setAllCaps(true);
tcView.setTypeface(null, Typeface.BOLD);
tcView.setGravity(Gravity.CENTER_HORIZONTAL);
tr.addView(tcView);
TextView tfView = new TextView(context);
tfView.setText("Fee");
tfView.setTextColor(Color.BLACK);
tfView.setAllCaps(true);
tfView.setTypeface(null, Typeface.BOLD);
tfView.setGravity(Gravity.CENTER_HORIZONTAL);
tr.addView(tfView);
TextView tqView = new TextView(context);
tqView.setText("Available");
tqView.setTextColor(Color.BLACK);
tqView.setAllCaps(true);
tqView.setTypeface(null, Typeface.BOLD);
tqView.setGravity(Gravity.CENTER_HORIZONTAL);
tr.addView(tqView);
// Adding row to Table
tbl.addView(tr);
// Table Header Has Been Set
SET_TABLE_HEADER = false;
}
Then inside my loop:
if (SET_TABLE_HEADER) {
setTableHeader(tbl);
}
Where tbl is the instance of my TableLayout in my view. This works for me.
Of course you will have to initialize SET_TABLE_HEADER to true at the point where you start creating your table.

Categories

Resources