I have an activity in which I have implemented a tableview, the table view has a header and a body, the table is horizontally and vertically scrollable.
This is the source code of the activity:
public class ReportListActivity extends Activity {
TableLayout report_table;
TableRow tr_data;
int j = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_report_list);
report_table=(TableLayout) findViewById(R.id.report_table);
//---------------Table Header-----------------------------------------------
TableRow tr_head = new TableRow(this);
tr_head.setId(10);
tr_head.setBackgroundColor(Color.GRAY);
tr_head.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
TextView label_sr_no = new TextView(this);
label_sr_no.setId(20);
label_sr_no.setText("S.No.");
label_sr_no.setTextColor(Color.WHITE);
label_sr_no.setPadding(5,5,5,5);
tr_head.addView(label_sr_no);// add the column to the table row here
TextView label_test_name = new TextView(this);
label_test_name.setId(21);// define id that must be unique
label_test_name.setText("Test Name"); // set the text for the header
label_test_name.setTextColor(Color.WHITE); // set the color
label_test_name.setPadding(5,5,5,5); // set the padding (if required)
tr_head.addView(label_test_name); // add the column to the table row here
TextView label_test_date = new TextView(this);
label_test_date.setId(21);// define id that must be unique
label_test_date.setText("Date"); // set the text for the header
label_test_date.setTextColor(Color.WHITE); // set the color
label_test_date.setPadding(5,5,5,5); // set the padding (if required)
tr_head.addView(label_test_date); // add the column to the table row here
TextView label_ro = new TextView(this);
label_ro.setId(21);// define id that must be unique
label_ro.setText("R.O."); // set the text for the header
label_ro.setTextColor(Color.WHITE); // set the color
label_ro.setPadding(5,5,5,5); // set the padding (if required)
tr_head.addView(label_ro); // add the column to the table row here
TextView label_wo = new TextView(this);
label_wo.setId(21);// define id that must be unique
label_wo.setText("W.O."); // set the text for the header
label_wo.setTextColor(Color.WHITE); // set the color
label_wo.setPadding(5,5,5,5); // set the padding (if required)
tr_head.addView(label_wo); // add the column to the table row here
TextView label_lo = new TextView(this);
label_lo.setId(21);// define id that must be unique
label_lo.setText("L.O."); // set the text for the header
label_lo.setTextColor(Color.WHITE); // set the color
label_lo.setPadding(5,5,5,5); // set the padding (if required)
tr_head.addView(label_lo); // add the column to the table row here
TextView label_max = new TextView(this);
label_max.setId(21);// define id that must be unique
label_max.setText("Max."); // set the text for the header
label_max.setTextColor(Color.WHITE); // set the color
label_max.setPadding(5,5,5,5); // set the padding (if required)
tr_head.addView(label_max); // add the column to the table row here
TextView label_tm = new TextView(this);
label_tm.setId(21);// define id that must be unique
label_tm.setText("T.M."); // set the text for the header
label_tm.setTextColor(Color.WHITE); // set the color
label_tm.setPadding(5,5,5,5); // set the padding (if required)
tr_head.addView(label_tm); // add the column to the table row here
TextView label_rank = new TextView(this);
label_rank.setId(21);// define id that must be unique
label_rank.setText("Rank"); // set the text for the header
label_rank.setTextColor(Color.WHITE); // set the color
label_rank.setPadding(5,5,5,5); // set the padding (if required)
tr_head.addView(label_rank); // add the column to the table row here
report_table.addView(tr_head, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
//---------------Table Header-----------------------------------------------
//--------------------Table Body---------------------------
for (int i=1; i<=10; i++)
{
tr_data = new TableRow(this);
tr_data.setId(10);
tr_data.setBackgroundColor(Color.TRANSPARENT);
tr_data.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
final TextView sr_no = new TextView(this);
sr_no.setId(20);
sr_no.setText(""+i);
sr_no.setTextColor(Color.BLACK);
sr_no.setPadding(5,5,5,5);
tr_data.addView(sr_no);// add the column to the table row here
final TextView test_name = new TextView(this);
test_name.setId(21);// define id that must be unique
test_name.setText("Speed Test 60(min) Demo-ST-01"); // set the text for the header
test_name.setTextColor(Color.BLACK); // set the color
test_name.setPadding(5,5,5,5); // set the padding (if required)
tr_data.addView(test_name); // add the column to the table row here
final TextView test_date = new TextView(this);
test_date.setId(21);// define id that must be unique
test_date.setText("12 Mar 2013"); // set the text for the header
test_date.setTextColor(Color.BLACK); // set the color
test_date.setPadding(5,5,5,5); // set the padding (if required)
tr_data.addView(test_date); // add the column to the table row here
final TextView test_ro = new TextView(this);
test_ro.setId(21);// define id that must be unique
test_ro.setText("2"); // set the text for the header
test_ro.setTextColor(Color.BLACK); // set the color
test_ro.setPadding(5,5,5,5); // set the padding (if required)
tr_data.addView(test_ro); // add the column to the table row here
final TextView test_wo = new TextView(this);
test_wo.setId(21);// define id that must be unique
test_wo.setText("3"); // set the text for the header
test_wo.setTextColor(Color.BLACK); // set the color
test_wo.setPadding(5,5,5,5); // set the padding (if required)
tr_data.addView(test_wo); // add the column to the table row here
final TextView test_lo = new TextView(this);
test_lo.setId(21);// define id that must be unique
test_lo.setText("85"); // set the text for the header
test_lo.setTextColor(Color.BLACK); // set the color
test_lo.setPadding(5,5,5,5); // set the padding (if required)
tr_data.addView(test_lo); // add the column to the table row here
final TextView test_max = new TextView(this);
test_max.setId(21);// define id that must be unique
test_max.setText("101"); // set the text for the header
test_max.setTextColor(Color.BLACK); // set the color
test_max.setPadding(5,5,5,5); // set the padding (if required)
tr_data.addView(test_max); // add the column to the table row here
final TextView test_tm = new TextView(this);
test_tm.setId(21);// define id that must be unique
test_tm.setText("-1.5"); // set the text for the header
test_tm.setTextColor(Color.BLACK); // set the color
test_tm.setPadding(5,5,5,5); // set the padding (if required)
tr_data.addView(test_tm); // add the column to the table row here
final TextView test_rank = new TextView(this);
test_rank.setId(21);// define id that must be unique
test_rank.setText("5810"); // set the text for the header
test_rank.setTextColor(Color.BLACK); // set the color
test_rank.setPadding(5,5,5,5); // set the padding (if required)
tr_data.addView(test_rank); // add the column to the table row here
final TextView test_exam_set = new TextView(this);
test_exam_set.setId(21);// define id that must be unique
test_exam_set.setText("123456"); // set the text for the header
test_exam_set.setTextSize((float) 0.01);
test_exam_set.setTextColor(Color.TRANSPARENT); // set the color
test_exam_set.setPadding(5,5,5,5); // set the padding (if required)
tr_data.addView(test_exam_set); // add the column to the table row here
report_table.addView(tr_data, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
//----------------------On click table row---------------------------------------
tr_data.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// Toast.makeText(getApplicationContext(), test_name.getText().toString(), Toast.LENGTH_LONG).show();
// Toast.makeText(getApplicationContext(), test_date.getText().toString(), Toast.LENGTH_LONG).show();
// Toast.makeText(getApplicationContext(), sr_no.getText().toString(), Toast.LENGTH_LONG).show();
//Toast.makeText(getApplicationContext(), test_exam_set.getText().toString(), Toast.LENGTH_LONG).show();
Intent intent = new Intent(ReportListActivity.this, ReportListDetailsActivity.class);
finish();
ReportListActivity.this.startActivity(intent);
}
});
//----------------------On click table row---------------------------------------
}
//--------------------Table Body---------------------------
}
//onclick device back button
@Override
public void onBackPressed() {
// do something on back.
Intent intent = new Intent(ReportListActivity.this, TakeTestActivity.class);
finish();
ReportListActivity.this.startActivity(intent);
return;
}
//method to show toast message
public void makeAToast(String str) {
//yet to implement
Toast toast = Toast.makeText(this,str, Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}
}
This is the layout file:
<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".ReportListActivity" >
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
<HorizontalScrollView
android:id="@+id/horizontalScrollView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TableLayout
android:shrinkColumns="0,1,2,3,4,5,6,7,8,9"
android:id="@+id/report_table" android:layout_weight="1" android:layout_height="wrap_content" android:layout_width="match_parent">
</TableLayout>
</RelativeLayout>
</HorizontalScrollView>
</ScrollView>
</RelativeLayout>
The table can be horizontally and vertically scrolled.
This is how it looks now if scrolled horizontally:
Scroll position 1:
Scroll position 2:
What I want is that, if the table is scrolled horizontally, the S.No. column should be frozen, the rest should be scrolled. The same should happen for the table header. How should I achieve this?
Thanks in advance!
The problem is solved...............
Source code of activity:
Code of layout file:
Screen shots:
activity_main.xml
MainActivity.java
public class MainActivity extends Activity {
}