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.
Related
I am trying to add a single textview row to a table in android
the row should merge and center the width of the table
I am able to achieve this using XML
<TableLayout>
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollbars="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#drawable/red_cell_shape"
android:text="code"
android:textAlignment="center"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/red_cell_shape"
android:padding="5dp"
android:text="desc"
android:textAlignment="center"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/red_cell_shape"
android:text="status"
android:textAlignment="center"
android:textStyle="bold" />
</TableRow>
<TableRow
android:id="#+id/tremptyrow"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/red_cell_shape">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="#string/no_fg_scanned"
android:padding="5dp"
android:layout_span="6"
android:textAlignment="center"
android:textColor="#color/red" />
</TableRow>
</TableLayout>
this is what it should look like
but I want to do it dynamically in java and tried this but did not work
You can implement the above layout programmatically like below:
1.Create an empty TableLayout in xml like below:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/tableLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</TableLayout>
2.Get the TableLayout reference from xml and create two TableRows each one having TextView children:
//get the TableLayout
TableLayout tableLayout = findViewById(R.id.tableLayout);
//create the first TableRow
TableRow tableRow1 = new TableRow(this);
tableRow1.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT));
tableRow1.setBackgroundColor(ContextCompat.getColor(this, android.R.color.white));
//add 3 TextViews in TableRow1 and add TableRow1 to TableLayout
tableRow1.addView(getTextView(android.R.color.holo_purple, android.R.color.black, "Code"));
tableRow1.addView(getTextView(android.R.color.holo_red_light, android.R.color.black,"Desc"));
tableRow1.addView(getTextView(android.R.color.holo_green_dark, android.R.color.black,"Status"));
tableLayout.addView(tableRow1);
//create the second TableRow
TableRow tableRow2 = new TableRow(this);
tableRow2.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT));
tableRow2.setBackgroundColor(ContextCompat.getColor(this, android.R.color.white));
//add 1 TextView in TableRow2 and add TableRow2 to TableLayout
tableRow2.addView(getTextView(android.R.color.black, android.R.color.holo_red_light,"No records"));
tableLayout.addView(tableRow2);
with the below helper function to create each TextView in TableRow programmatically. The key point here is to use layoutParams.weight = 1 on TextView TableRow.LayoutParams to be able to get equal width between all columns in TableRow:
private TextView getTextView(int backgroundColorId, int textColorId, String text){
TextView tv = new TextView(this);
tv.setBackgroundColor(ContextCompat.getColor(this, backgroundColorId));
tv.setTextColor(ContextCompat.getColor(this, textColorId));
tv.setText(text);
tv.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
tv.setTypeface(tv.getTypeface(), Typeface.BOLD);
int padding = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 5, getResources().getDisplayMetrics());
tv.setPaddingRelative(padding, padding, padding, padding);
TableRow.LayoutParams layoutParams = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.MATCH_PARENT);
layoutParams.weight = 1; //this is mandatory to get equal width between all columns in TableRow
tv.setLayoutParams(layoutParams);
return tv;
}
Result:
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);
what i am trying to do is to make a dynamic table layout .. i mean the rows will be filled dynamically with text views and with scrolling horizontal and vertical but each time when the activity open my table layout start showing from the left side even i set the gravity to be right .. why this is happening?
this is my xml code :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right" >
<HorizontalScrollView
android:id="#+id/horizontalScrollView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TableLayout
android:id="#+id/table1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:stretchColumns="0,1"
android:layout_gravity="right" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:layout_gravity="right">
</TableRow>
</TableLayout>
</ScrollView>
</HorizontalScrollView>
</LinearLayout>
and here is my java code :
TableLayout ll = (TableLayout) findViewById(R.id.table1);
int razan=0;
int c=-1;
for(int s=1; s<=fixed;s++){
if(razan<=count){
for (int i = 0; i <text.length; i++) {
if(i>0 && i%7==0){
c = c+1;
TableRow row1= new TableRow(this);
TableRow.LayoutParams tr1 = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT);
row1.setLayoutParams(tr1);
TextView emptyrow = new TextView(this);
emptyrow.setText("");
// emptyrow.setBackground(getResources().getDrawable(R.drawable.back));
row1.addView(emptyrow);
ll.addView(row1,i);
}
TableRow row= new TableRow(this);
TableRow.LayoutParams tr = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT);
row.setLayoutParams(tr);
TextView product = new TextView(this);
TextView product_j = new TextView(this);
product_j.setText(prodlist.get(razan));
Resources res = getResources();
Drawable drawable = res.getDrawable(R.drawable.back);
product_j.setBackgroundDrawable(drawable);
product_j.setGravity(Gravity.RIGHT);
product_j.setTextSize(18);
product.setText(text[i]);
// product.setBackgroundDrawable(drawable);
product.setGravity(Gravity.RIGHT);
product.setPadding(3, 0, 3, 0);
product.setTextSize(18);
row.addView(product_j);
row.addView(product);
razan++;
ll.addView(row,i);
//here is the code for adding an empty row after each seven inner rows !!!
}//inner for
}
}//outer fors
}
can anyone help me?
Make your root LinearLayouts Height and width as match_parent and try gravity to right on your TableLayout
I'm experiencing the same issue as this one.
Unfortunately, the answer to that one does not work for me.
I know the rows are are added to the table, I can see the number of rows grow in logcat.
But I don't see them on my screen.
Investigated refresh (invalidate), color, dimensions, layout params, ...
... and ran out of ideas
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:orientation="vertical" >
<Button
android:id="#+id/btnAddRow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add row" />
<TableLayout
android:id="#+id/tableLayoutInventory"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:shrinkColumns="*"
android:stretchColumns="*" >
<TableRow
android:id="#+id/tableRowTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/headerCol1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ID"
android:textSize="16dp" />
<TextView
android:id="#+id/headerCol2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Titre"
android:textSize="16dp" />
</TableRow>
</TableLayout>
</LinearLayout>
Code:
public void onClick(View v) {
TableRow row = new TableRow(this);
row.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
TextView tv = new TextView(this);
tv.setText("ID, row "+rownb);
tv.setHeight(16);
tv.setTextSize(16);
tv.setTextColor(Color.RED);
tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
row.addView(tv);
TextView tv2 = new TextView(this);
tv2.setHeight(16);
tv2.setTextSize(16);
tv2.setTextColor(Color.RED);
tv2.setText("Title, row "+rownb);
tv2.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
row.addView(tv2);
table_layout.addView(row);
table_layout.requestLayout();
table_layout.invalidate();
table_layout.postInvalidate();
Log.i ("LIMA","Add row. There are now "+table_layout.getChildCount()+" rows");
}
Remove those LayoutParamson the views you're adding in the table rows
I have an Android project I am working on. I am fairly new to Android Development so I have some code in a MainActivity.java file that I am working on Creating a Table Layout for but the way my code currently is I can only add one item from that list what can I do in the Activity.java class to change this so it will programmatically add all of my Mcdonalds objects too the Table. What can I do to add both rows to the view on MainActivity.java?
Activity_Main.xml
<LinearLayout 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=".ShelterActivity" >
<TableLayout android:id="#+id/TableLayout01" android:layout_width="fill_parent" android:layout_height="wrap_content" android:stretchColumns="0">
<TableRow
android:id="#+id/TableRow01"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="#+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Address"
android:width="250px"
android:textStyle="bold"></TextView>
<TextView
android:id="#+id/TextView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Distance"
android:width="250px"
android:textStyle="bold"></TextView>
<TextView
android:id="#+id/TextView03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ETA"
android:width="250px"
android:textStyle="bold"></TextView>
</TableRow>
</TableLayout>
</LinearLayout>
MainActivity.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shelter);
List<McDonalds> mcdList = new ArrayList<McDonalds>();
ScrollView sv = new ScrollView(this);
// get a reference for the TableLayout
TableLayout ll = (TableLayout) findViewById(R.id.TableLayout01);
HorizontalScrollView hsv = new HorizontalScrollView(this);
// create a new TableRow
TableRow row = new TableRow(this);
//This will be replaced by a Read Only Stored procedure that gets the List of Shelters
McDonalds mcdonalds = new McDonalds("720 N Main St, Roswell, NM", 1.08, 8.0);
mcdList.add(mcdonalds);
McDonalds mcdonalds1 = new McDonalds("1804 S Main St, Roswell, NM", 2.9, 12.0);
mcdList.add(mcdonalds1);
for (McDonalds mcD : mcdList){
// create a new TextView
TextView t = new TextView(this);
TextView t1 = new TextView(this);
TextView t2 = new TextView(this);
String mcdAddress = mcD.getAddress();
String distance = mcD.getDistance().toString();
String ETA = mcD.getETA().toString();
t.setText(mcdAddress);
t1.setText(distance);
t2.setText(ETA);
row.addView(t);
row.addView(t1);
row.addView(t2);
}
// add the TableRow to the TableLayout
ll.addView(row,new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
}
As seen in the code above there should be two items in the list and I need a way to add both items with the ll.addView(row,new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); but this will throw a error.
You are pretty close but What you have to do is:
Remove your TableRow from your xml.
Always create a new TableRow in a loop.
All your TextViews need a layout params set by setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
Add table row to a table layout also in a loop
I would say MATCH_PARENT would be better for each table row width
The other way to do it would be to make an xml file with and it's textViews inside of it. Inflate the xml with LayoutInflater.from(context).inflate(...), fill all 3 textviews finding them by id and add this inflated tableRow to a tableLayout