this is my XML layout code. i want to be able to dynamically add rows the TableLayout - tableLayout1. please help
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableLayout android:id="#+id/tableLayout1"
android:layout_height="match_parent"
android:layout_width="wrap_content">
</TableLayout>
</LinearLayout>
If tableLayout1 is your TableLayout, then
for(int i=0;i<10;i++)
{
TableRow tR = new TableRow(this);
tR.setPadding(5,5,5,5);
TextView tV_txt1 = new TextView(this);
TextView tV_txt2 = new TextView(this);
tV_txt1.setText("This is the textbox1");
tV_txt2.setText("This is the textbox2");
tR.addView(tV_txt1);
tR.addView(tV_txt2);
tablelayout1.addView(tR);
}
Related
I have created a custom textview and want to add it dynamically to a table layout:
custome textview code named custom_table_cell :
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
style="#style/style_table_cell"
android:layout_width="0dp"
android:layout_weight="1"
android:gravity="center"
android:text="Cell" >
</TextView>
My activity code :
<?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:background="#drawable/bg_flight" >
<ScrollView android:layout_height="fill_parent"
android:layout_width="fill_parent" >
<TableLayout
android:id="#+id/tblInvoiceData"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</TableLayout>
</ScrollView>
</TableLayout>
and my code to add table row to table layout :
public TextView TableGetCell(Context context, String Text, int ColumnId) {
final TextView cell = (TextView) ((Activity) context).getLayoutInflater().inflate(R.layout.custom_table_cell,
null);
cell.setText(Text);
cell.setTypeface(Graphic.GetFontFaceFarsi(context));
cell.setLayoutParams(new TableRow.LayoutParams(ColumnId));
return cell;
}
public void CreateRows() {
for (final Customer d : Customers) {
TableRow tr = new TableRow(this);
tr.addView(TableGetCell(this, String.valueOf(d.Name), 0));
tr.addView(TableGetCell(this, String.valueOf(d.Family), 0));
tr.addView(TableGetCell(this, String.valueOf(d.Age), 0));
tblInvoiceData.addView(tr);
}
}
and my question : why text view not wrapped?
TableRow row = new TableRow(this);
TableRow.LayoutParams lp = new TableRow.LayoutParams(android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
row.setLayoutParams(lp);
TextView textView = new TextView(this);
textView.setLayoutParams(new TableRow.LayoutParams(android.view.ViewGroup.LayoutParams.WRAP_CONTENT, android.view.ViewGroup.LayoutParams.MATCH_PARENT, 1.0f));
textView.setText("yourValue");
row.addView(textView);
tablelayout.addView(row);
They currently appear on the center.
Android Java Code
public void populateTable() {
for(DoctorBean post: beanPostArrayList){
String result = "";
TableRow row = new TableRow(getActivity());
row.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
ImageView docImageView = new ImageView(getActivity());
docImageView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
docImageView.setPadding(5, 5, 5, 5);
docImageView.setImageResource(R.drawable.ic_doctor);
TextView textView = new TextView(getActivity());
textView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
textView.setPadding(5, 5, 5, 5);
result += "\n" + post.getHeroName();
textView.setGravity(Gravity.LEFT);
docImageView.setForegroundGravity(Gravity.LEFT);
row.setGravity(Gravity.LEFT);
textView.setText(result);
row.addView(docImageView);
row.addView(textView);
tableLayout.addView(row);
}
}
Layout Code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.google.android.gms.maps.MapView
android:id="#+id/mapView"
android:layout_width="match_parent"
android:layout_height="229dp"
class="com.google.android.gms.maps.SupportMapFragment" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TableLayout
android:id="#+id/tableLayout1"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:padding="10dp"
android:shrinkColumns="*"
android:stretchColumns="*">
</TableLayout>
</ScrollView></LinearLayout>
So there I want to align the ImageView and the TextView to the left.
An alternative approach should solve your issue
You don't need to add explicit scroll for a TableView
TableView can be directly added to your main LinearLayout. As you have already specified the height of MapView
Suggested Modified xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/mainLL"
android:orientation="vertical">
<com.google.android.gms.maps.MapView
android:id="#+id/mapView"
android:layout_width="match_parent"
android:layout_height="229dp"
class="com.google.android.gms.maps.SupportMapFragment" />
<TableLayout
android:id="#+id/tableLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</TableLayout>
</LinearLayout>
Corresponding code change
LinearLayout linearLayout=(LinearLayout)view.findViewById(R.id.mainLL);
//Table Layout parameters
TableRow.LayoutParams textViewParam = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT,1.0f);
TableLayout tableLayout = (TableLayout) view.findViewById(R.id.tableLayout);
TableRow trHead = new TableRow(context);
LayoutParams tableRowParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
trHead.setLayoutParams(tableRowParams);
TextView nameHead = new TextView(context);
nameHead.setText("Content left");
nameHead.setLayoutParams(textViewParam);
nameHead.setGravity(Gravity.LEFT);//or Gravity.CENTER according to your requirement
trHead.addView(nameHead);
TextView detailHead = new TextView(context);
detailHead.setText("Content right");
detailHead.setGravity(Gravity.RIGHT);//or Gravity.CENTER according to your requirement
detailHead.setLayoutParams(textViewParam);
trHead.addView(detailHead);
tableLayout.addView(trHead);
//add table layout to linear layout
linearLayout.add(tableLayout);
NOTE:
Many values have been referred from resource files, You can either
neglect/replicate those. I have added two textviews. You may need to
change according to your requirement
I want to use code to generate a very large TableLayout. XML works great but the file is getting huge and making changes would be tedious down the road. The final layout will have a HorizontalScrollview on the right but my problem is best illustrated as follows.
The XML file is this.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent">
<TableLayout
android:layout_width="wrap_content"
android:layout_height="match_parent">
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Table 1"/></TableRow>
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Table 1"/></TableRow>
</TableLayout>
<TableLayout
android:layout_width="wrap_content"
android:layout_height="match_parent">
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Table 2"/></TableRow>
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Table 2"/></TableRow>
</TableLayout>
</LinearLayout>
this outputs like this (sorry, can't post screenshot)
Table 1 Table 2
Table 1 Table 2
When I use code this this
LinearLayout ll = new LinearLayout(this);
ll.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT));
TableLayout table1 = new TableLayout(this);
table1.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT,TableLayout.LayoutParams.MATCH_PARENT));
for (int x = 0;x < 2;x++) {
TableRow row = new TableRow(this);
row.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,TableRow.LayoutParams.WRAP_CONTENT));
TextView tv = new TextView(this);
tv.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,TableRow.LayoutParams.WRAP_CONTENT));
tv.setText("Table 1");
row.addView(tv);
table1.addView(row);
}
TableLayout table2 = new TableLayout(this);
table2.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT,TableLayout.LayoutParams.MATCH_PARENT));
for (int x = 0;x < 2;x++) {
TableRow row = new TableRow(this);
row.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,TableRow.LayoutParams.WRAP_CONTENT));
TextView tv = new TextView(this);
tv.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,TableRow.LayoutParams.WRAP_CONTENT));
tv.setText("Table 2");
row.addView(tv);
table2.addView(row);
}
ll.addView(table1);
ll.addView(table2);
setContentView(ll);
it shows output like
Table 1
Table 1
I lose the second tablelayout. How can I change the code style to view that second tablelayout?
I didn't find out why the second table doesn't work in code but I was able to fix my problem by
Setting up the LinearLayout and the TableLayouts in XML.
Then referencing the TableLayouts and adding the TableRows in code.
I want my 2D array to be displayed as matrix format. For that which layout is suitable? Can I have any example for that?
The simple answer is to use a TableLayout with TextViews inside.
But if you want to display a large array, you should make the matrix scrollable in both directions. Below is the code that populates a matrix that is displayed as a series of EditTexts inside rows, inside a TableLayout. The TableLayout is made scrollable in both directions using two ScrollView. Adapt the code to your needs. table is the TableLayout with the id tableLayout1.
private void fillTable(final int n, final double[][] matrix, TableLayout table) {
table.removeAllViews();
for (int i = 0; i < n; i++) {
TableRow row = new TableRow(MainActivity.this);
row.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
for (int j = 0; j < n; j++) {
EditText edit = new EditText(OutputActivity.this);
edit.setInputType(InputType.TYPE_CLASS_NUMBER|InputType.TYPE_NUMBER_FLAG_DECIMAL|InputType.TYPE_NUMBER_FLAG_SIGNED);
edit.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
edit.setText(Double.toString(matrix[i][j]));
edit.setKeyListener(null);
row.addView(edit);
}
table.addView(row);
}
}
The layout XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:orientation="vertical" >
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<HorizontalScrollView
android:id="#+id/horizontalScrollView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TableLayout
android:id="#+id/tableLayout1"
android:layout_width="wrap_content"
android:layout_height="match_parent" >
</TableLayout>
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
</ScrollView>
</LinearLayout>
This question is actually related to this post Set the layout weight of a TextView programmatically
Based on the answers I just need to set the TextView layout params as follow and set the stretchColumn Property, but by adding the following code to mine, it makes the textView disappear from the Table Layout.
TextView tv = new TextView(v.getContext());
tv.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f));
So, here is my code where I want to dynamically add a row with 3 columns with 35%, 5% and 60% weight. Please let me know what is wrong with my code.
private void addTableRow(int resIdTitle, String strValue){
TableRow tr = new TableRow(this);
// Add First Column
TextView tvTitle = new TextView(this);
tvTitle.setText(resIdTitle);
tvTitle.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT, 0.35f));
tr.addView(tvTitle);
// Add 2nd Column
TextView tvColon = new TextView(this);
tvColon.setText(" : ");
tvColon.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT, 0.05f));
tr.addView(tvColon);
// Add the 3rd Column
TextView tvValue = new TextView(this);
tvValue.setText(strValue);
tvValue.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT, 0.60f));
tr.addView(tvValue);
// Finally add it to the table
tl.addView(tr, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
Log.d(TAG, "Row Added");
}
And here is my xml file,
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
<TableLayout
android:id="#+id/tl_cam_get_param"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:title="#string/strGetParamTitle"
android:scrollbars="vertical"
android:shrinkColumns="0">
</TableLayout>
</ScrollView>
I have also tried setting the layout_width to 0, but still it didn't work.
Thanks,
artsylar
Thank you everyone!
Finally, I was able to solve my problem by referencing to the following posts.
android: two issues using Tablerow+TextView in Tablelayout
How to make a TableLayout from XML using programmable way ?
Refer below for my working code.
This is where the dynamic adding of rows is done.
TableLayout tl = (TableLayout)findViewById(R.id.table_layout);
private void addTableRow(int resIdTitle, String strValue){
LayoutInflater inflater = getLayoutInflater();
TableRow tr = (TableRow)inflater.inflate(R.layout.table_row, tl, false);
// Add First Column
TextView tvTitle = (TextView)tr.findViewById(R.id.tvTitle);
tvTitle.setText(resIdTitle);
// Add the 3rd Column
TextView tvValue = (TextView)tr.findViewById(R.id.tvValue);
tvValue.setText(strValue);
tl.addView(tr);
}
This is the table_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
<TableLayout
android:id="#+id/table_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:title="#string/strGetParamTitle"
android:scrollbars="vertical"
android:shrinkColumns="0">
</TableLayout>
</ScrollView>
And finally the table_row.xml. I've set all the layout_params here.
<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:id="#+id/tvTitle"
android:paddingRight="3dip"
android:layout_width="0px"
android:layout_weight="0.35"/>
<TextView android:text=":"
android:layout_width="0px"
android:layout_weight="0.05"/>
<TextView
android:id="#+id/tvValue"
android:paddingLeft="3dip"
android:layout_width="0px"
android:layout_weight="0.6"
/>
</TableRow>