I am trying to programmatically add a row to a table which is defined in XML (this row has internal table in it).
<TableLayout android:id="#+id/maintable"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:stretchColumns="*" xmlns:android="http://schemas.android.com/apk/res/android">
<TableRow android:id="#+id/tabrow">
<TableLayout android:id="#+id/tableLayout1"
android:layout_width="fill_parent" android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<TableRow>
<TextView android:id="#+id/titlerow" android:layout_height="fill_parent" android:text="Hello world"
android:gravity="center" android:layout_width="fill_parent"
android:layout_weight="1" android:textSize="17sp"></TextView>
</TableRow>
<TableRow>
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:layout_weight="1">
<TextView android:text="#string/time1" android:id="#+id/time1" style="#style/timeview"></TextView>
<TextView android:text="#string/time2" style="#style/dayview"></TextView>
<TextView android:text="#string/time3" style="#style/dayview"></TextView>
</LinearLayout>
</TableRow>
</TableLayout>
</TableRow>
Now I want to just add this(tabrow) row many times in the table.
How can I do it ?
something like this should do:
// get your table layout
TableLayout tl = (TableLayout) findViewById(R.id.WhateverYoursIs);
// Create new row
TableRow tr = new TableRow(this);
tr.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
// Create textview
TextView t = new TextView(this);
//
// configure your textview's and do this 2 more times
//
t.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
// add textview to row
tr.addView(t);
//
// do this 2 more times
//
// add row to table
tl.addView(tr, new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));
you just have to put the row-creation-stuff in a loop depending on how often you need it
Related
I'm working in an Android application and I want to add a TableRow programmatically in my TableLayout.
I have this TableLayout:
<TableLayout
android:id="#+id/details_table"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableRow>
<TextView
android:text="4686"
android:layout_width="wrap_content"
android:layout_column="0"
android:layout_weight="1"
android:textSize="7px"
android:textColor="#color/black" />
<TextView
android:text="sdhiuf osdfh isdhf ihdf"
android:layout_width="wrap_content"
android:layout_column="1"
android:layout_weight="1"
android:textSize="7px"
android:textColor="#color/black" />
<TextView
android:text="2"
android:layout_width="wrap_content"
android:layout_column="2"
android:layout_weight="1"
android:textSize="7px"
android:textColor="#color/black" />
<TextView
android:text="UN"
android:layout_width="wrap_content"
android:layout_column="3"
android:layout_weight="1"
android:textSize="7px"
android:textColor="#color/black" />
</TableRow>
and I want to add this exactly TableRow programmatically.
I'm trying something like this:
TableLayout detailsTable = (TableLayout) l.findViewById(R.id.details_table);
for(Nfce_Product nfceProduct : nfceProducts){
TableRow tableRow = new TableRow(getActivity());
TextView tvProductCode = new TextView(getActivity());
tvProductCode.setLayoutParams(new TableRow.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT, 1f));
tvProductCode.setText(nfceProduct.getProduct_code());
tvProductCode.setTextSize(TypedValue.COMPLEX_UNIT_PX, productDetailsTextSize);
tvProductCode.setTextColor(getResources().getColor(R.color.black));
TextView tvProductDescription = new TextView(getActivity());
tvProductDescription.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT, 1f));
tvProductDescription.setText(nfceProduct.getProduct_description());
tvProductDescription.setTextSize(TypedValue.COMPLEX_UNIT_PX, productDetailsTextSize);
tvProductDescription.setTextColor(getResources().getColor(R.color.black));
TextView tvProductAmount = new TextView(getActivity());
tvProductAmount.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT, 1f));
tvProductAmount.setText(String.valueOf(nfceProduct.getAmount()));
tvProductAmount.setTextSize(TypedValue.COMPLEX_UNIT_PX, productDetailsTextSize);
tvProductAmount.setTextColor(getResources().getColor(R.color.black));
TextView tvProductMetric = new TextView(getActivity());
tvProductMetric.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT, 1f));
tvProductMetric.setText(nfceProduct.getProduct_metric());
tvProductMetric.setTextSize(TypedValue.COMPLEX_UNIT_PX, productDetailsTextSize);
tvProductMetric.setTextColor(getResources().getColor(R.color.black));
tableRow.addView(tvProductCode);
tableRow.addView(tvProductDescription);
tableRow.addView(tvProductAmount);
tableRow.addView(tvProductMetric);
detailsTable.addView(tableRow);
}
Here is my own answer to my own dynamically created TableRow question. I think my answer is detailed enough, you should have no problems taking it as your own! My issue involved not only dynamically creating TableRows, but also being able to touch each row and have something happen. I've gone further than that nowadays to make each CELL separately clickable.
Edit: You need to have a separate TableRow XML file that you can access, separate from the actual TableLayout you're trying to dynamically populate.
Edit2: I should probably try to make an actual solution for you, so that you see what I'm talking about:
First, create (or keep) your XML file containing your TableLayout. Second, Create a separate TableRow XML file.
(This is your potential tablerow.xml file)
<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:focusable="true" >
<TextView
android:id="#+id/tableCell1"
android:layout_width="wrap_content"
android:layout_column="0"
android:layout_weight="1"
android:textSize="7px"
android:textColor="#color/black" />
<TextView
android:id="#+id/tableCell2"
android:layout_width="wrap_content"
android:layout_column="1"
android:layout_weight="1"
android:textSize="7px"
android:textColor="#color/black" />
<TextView
android:id="#+id/tableCell3"
android:layout_width="wrap_content"
android:layout_column="2"
android:layout_weight="1"
android:textSize="7px"
android:textColor="#color/black" />
<TextView
android:id="#+id/tableCell4"
android:layout_width="wrap_content"
android:layout_column="3"
android:layout_weight="1"
android:textSize="7px"
android:textColor="#color/black" />
</TableRow>
Now... back to your actual code!
for (Nfce_Product nfceProduct : nfceProducts) {
final TableLayout detailsTable = (TableLayout) findViewById(R.id.details_table);
final TableRow tableRow = (TableRow) getLayoutInflater().inflate(R.layout.tablerow, null);
TextView tv;
//Filling in cells
tv = (TextView) tableRow.findViewById(R.id.tableCell1);
tv.setText(nfceProduct.getProduct_code());
tv = (TextView) tableRow.findViewById(R.id.tableCell2);
tv.setText(nfceProduct.getProduct_description());
tv = (TextView) tableRow.findViewById(R.id.tableCell3);
tv.setText(nfceProduct.getAmount());
tv = (TextView) tableRow.findViewById(R.id.tableCell4);
tv.setText(nfceProduct.getProduct_metric());
//Add row to the table
detailsTable.addView(tableRow);
} //End for
If you still need/want to change the text size and color, you can do that after the setText() line and before you do findViewById again. If you want to have column headers that basically say Code, Description, Amount, and Metric, make a TableRow inside the TableLayout like you have currently. The TableRows created programmatically will fall in line after that "header" row.
I have a layout file called row which is basically a wrapper for how i want my row to look like
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TableRow
android:id="#+id/tableRow"
android:layout_height="wrap_content"
android:layout_width="match_parent">
<TextView
android:id="#+id/oid"
android:layout_width="0dp"
android:layout_weight="1" android:background="#dcdcdc"
android:textColor="#000000"
android:padding="5dip"
/>
<TextView
android:id="#+id/value"
android:layout_width="0dp"
android:layout_weight="1" android:background="#d3d3d3"
android:textColor="#000000"
android:padding="5dip"
/>
<TextView
android:id="#+id/type"
android:layout_width="0dp"
android:layout_weight="1" android:background="#dcdcdc"
android:textColor="#000000"
android:padding="5dip"
/>
</TableRow>
</LinearLayout>
Now i d like to be able to create a row using this layout and place it in my table that is defined in the fragment layout. This is my current code for creating a row(it doesn't use the row layout file) and adding it to the table, but the row is not formatted as i d like it to be.
TableLayout varbindTable = (TableLayout) view2.findViewById(R.id.tableLayout1);
TableRow row = new TableRow(getActivity());
row.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.MATCH_PARENT));
varbindTable.addView(row);
TextView name = new TextView(getActivity());
name.setText(((EditText)view.findViewById(R.id.oid)).getText().toString());
name.setTextColor(Color.BLACK);
name.setLayoutParams(new TableRow.LayoutParams(
0,
TableRow.LayoutParams.MATCH_PARENT));
TextView value = new TextView(getActivity());
value.setText(((EditText)view.findViewById(R.id.value)).getText().toString());
value.setTextColor(Color.BLACK);
value.setLayoutParams(new TableRow.LayoutParams(
0,
TableRow.LayoutParams.MATCH_PARENT));
TextView type = new TextView(getActivity());
type.setText(((Spinner)view.findViewById(R.id.data_type_spinner)).getSelectedItem().toString());
type.setTextColor(Color.BLACK);
type.setLayoutParams(new TableRow.LayoutParams(
0,
TableRow.LayoutParams.MATCH_PARENT));
row.addView(name);
row.addView(value);
row.addView(type);
Well, at first glance, the issue looks like your settings are different in the 2 examples. In the xml, you are using layout_width="0dp" and layout_weight="1". However, in the code you are using MATCH_PARENT.
Closest example I could find:
How do you setLayoutParams() for an ImageView?
I want to create a very simple scheduler by inserting data into a table. I have no problem getting this information, however, I cannot seem to insert into a meaningful table. By meaningful table, I mean a table that starts out as such, with subsequent additions inserted below their respected title:
DAY 1:
DAY 2:
DAY 3:
DAY 4:
I get the course information from the user and want to categorize the courses based on when they occur in their timetable. I am having a rather difficult time organizing this into a table.
Any suggestions on how I might efficiently sort this?
Use TableLayout and create a template of your layout in XML. For example:
<?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" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp" >
<TextView
android:id="#+id/textView1"
android:text="DAY 1:"
android:textAppearance="?android:attr/textAppearanceLarge" />
</TableRow>
<TableRow
android:id="#+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp" >
<TextView
android:id="#+id/textView1"
android:text="DAY 2:"
android:textAppearance="?android:attr/textAppearanceLarge" />
</TableRow>
<TableRow
android:id="#+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp" >
<TextView
android:id="#+id/textView1"
android:text="DAY 3:"
android:textAppearance="?android:attr/textAppearanceLarge" />
</TableRow>
<TableRow
android:id="#+id/tableRow4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp" >
<TextView
android:id="#+id/textView1"
android:text="DAY 4:"
android:textAppearance="?android:attr/textAppearanceLarge" />
</TableRow>
</TableLayout>
Then inspect a list of courses information and for every course add programatically info about it as a TableRow to your previously created layout . Use TableLayout#addView(View child, int index) for that. For example to add TableRow containing TextView as the last row to TableLayout:
TableLayout tl = (TableLayout) findViewById(R.id.my_table_layout);
TableRow tr = new TableRow(context);
tr.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
TextView tv = new TextView(context);
tv.setText("Some text");
tv.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
tr.addView(tv);
tl.addView(tr, new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));
I'm trying to build a dynamic table in my Android application. I would like to have a 70% wide column and a 30% wide column.
If I do it with the layout xml using android:layout_weight I don't have any problem:
<TableLayout
android:id="#+id/TableLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="*"
android:background="#ffffff" >
<TableRow
android:id="#+id/TableRow4"
style="#style/RigaTabella"
android:background="#d2ef7b" >
<TextView
android:id="#+id/TitoloSpesaTabella"
style="#style/Titolo"
android:layout_width="0px"
android:layout_weight="0.7"
android:background="#cdcdcd"
android:text="#string/spesa" />
<TextView
android:id="#+id/TitoloCostoTabella"
style="#style/Titolo"
android:layout_width="0px"
android:layout_weight="0.3"
android:background="#ababab"
android:text="#string/costo" />
</TableRow>
</TableLayout>
This is what I obtain: http://i.imgur.com/DHpXBzJ.jpg
(I used this gray background to show the real dimension of the two textView)
However if I try with this code:
TableLayout TableLayout = (TableLayout) findViewById(R.id.TableLayout);
for (int i = 0; i < 10; i++) {
TableRow Row = (TableRow) getLayoutInflater().inflate(R.layout.table_row_style, null);
if (i % 2 == 0)
Row.setBackgroundColor(Color.parseColor("#ffffff"));
else
Row.setBackgroundColor(Color.parseColor("#f1f1f1"));
TextView tv = (TextView) getLayoutInflater().inflate(R.layout.testo_sx_style, null);
tv.setText("Test Test");
TextView tv2 = (TextView) getLayoutInflater().inflate(R.layout.testo_dx_style, null);
tv2.setText("$ 1000");
Row.addView(tv);
Row.addView(tv2);
TableLayout.addView(Row);
}
where testo_sx_style.xml is:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="0px"
android:layout_height="wrap_content"
android:layout_weight="0.7"
android:gravity="left"
android:textSize="13sp"
android:textColor="#161616"
android:typeface="monospace"
android:background="#cdcdcd"
/>
and where testo_dx_style.xml is:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="0px"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:gravity="left"
android:textSize="13sp"
android:textColor="#161616"
android:typeface="monospace"
android:background="#ababab"
/>
I obtain this: http://i.imgur.com/oe6YHsz.png
I really don't know what it's wrong. I also tried, for example, to set in testo_sx_style.xml the layout_width to 100px but it doesn't change anything. It seems that some properties aren't applied to my code.
Anyone know what is the error? Thanks in advance.
Have a good day :)
p.s. Sorry for my English
Solved! With just this line of code:
tv.setLayoutParams(new TableRow.LayoutParams(0, LayoutParams.WRAP_CONTENT, 0.7f));
With this I can set the width to 0px, the height to WRAP_CONTENT and the weight to 0.7.
It works great :)
p.s. I found the solution here: Set the layout weight of a TextView programmatically
You could possibly try something like this, using the weight parameter of the layout:
TableRow tr1 = new TableRow(this);
tabInfo = new TableLayout(this);
tabInfo.setLayoutParams(new TableRow.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, 4.0f));
tr1.addView(tabInfo);
picture
Hi I have developed an app which uses the MySQL database. I have a code which retrieves the data and am trying to display the data in the form of a database.
This is my java code which accesses the database and try to print it in the form of a table dynamically.
My problem is the app force closes when I execute on a phone.
I got the following error.
This is my php code.
Use TableLayout to display your data in the form of tables.Also to connect to a mySQL database, you would need to build a php script which connects to the database.You have not shown your php script,so please provide that.
java.lang.ClassCastException:
"Thrown to indicate that the code has attempted to cast an object to a subclass
of which it is not an instance."
You have casted a TableLayout as TextView in your java class. So, change the TableLayout to TextView in your XML layout.
[Edit]
Got it Now:
Create a new TextView for the error to be displayed. Then change the following line:
error = (TextView) findViewById(R.id.myErrorView);
[Edit 2]
Example for TableLayout (not compiled)
XML Layout
<?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" >
<TextView
android:id="#+id/myHeading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TableLayout Example"
/>
<ScrollView android:layout_width="fill_parent" android:layout_height="wrap_content">
<TableLayout
android:id="#+id/tablelayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TableRow android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_marginBottom="2dp" android:layout_gravity="center_horizontal">
<TextView android:id="#+id/tabletv1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="ID" android:textStyle="bold" android:layout_gravity="center_horizontal"/>
<TextView android:id="#+id/tabletv2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Name" android:textStyle="bold" android:layout_gravity="center_horizontal"/>
<TextView android:id="#+id/tabletv3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Category" android:textStyle="bold" android:layout_gravity="center_horizontal"/>
<TextView android:id="#+id/tabletv4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Category" android:textStyle="bold" android:layout_gravity="center_horizontal"/>
</TableRow>
</TableLayout>
</ScrollView>
</LinearLayout>
Java Class
super.onCreate(savedInstanceState);
setContentView(R.layout.myLayout);
TableLayout table = (TableLayout) findViewById(R.id.tablelayout1);
TextView heading = (TextView) findViewById(R.id.myHeading);
// Your other codes
//////////////////////
for(int i=0;i<jArray.length();i++){
JSONObject json_data = jArray.getJSONObject(i);
TableRow row = new TableRow(this);
TableLayout.LayoutParams tableRowParams= new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.WRAP_CONTENT);
tableRowParams.setMargins(0, 0, 0, 2);
row.setGravity(Gravity.CENTER_HORIZONTAL);
row.setLayoutParams(tableRowParams);
TextView t1 = new TextView(this);
TextView t2 = new TextView(this);
TextView t3 = new TextView(this);
TextView t4 = new TextView(this);
t1.setText(""+json_data.getInt("prod_id"));
t2.setText(json_data.getString("prod_name"));
t3.setText(json_data.getString("prod_category"));
t4.setText(""+json_data.getDouble("prod_cost"));
t1.setGravity(Gravity.CENTER_HORIZONTAL);
t2.setGravity(Gravity.CENTER_HORIZONTAL);
t3.setGravity(Gravity.CENTER_HORIZONTAL);
t4.setGravity(Gravity.CENTER_HORIZONTAL);
row.addView(t1);
row.addView(t2);
row.addView(t3);
row.addView(t4);
table.addView(row);
}
Hope this helps!