I am trying to duplicate the following layout programmatically; however, the setGravity and setPadding don't seem to have an effect:
I'm trying to recreate the textviews within the TableRows (note that there's padding and the second text view is right aligned). Also, I'm trying to recreate additional entries because they are dynamic:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/myinfo_root"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:scrollbars="vertical"
>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mainTable"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1">
<TableRow>
<TextView
android:layout_column="1"
android:text="Text1"
android:padding="3dip" />
<TextView
android:id="#+id/text1"
android:text="TBD"
android:gravity="right"
android:padding="3dip" />
</TableRow>
<TableRow>
<TextView
android:layout_column="1"
android:text="Text2"
android:padding="3dip" />
<TextView
android:id="#+id/text2"
android:text="TBD"
android:gravity="right"
android:padding="3dip" />
</TableRow>
. . .
Here's a code snippet to add the TextViews. It seems to work but the entries don't have padding and are not right aligned.
int n = 0;
TableLayout table = (TableLayout) findViewById(R.id.mainTable);
for (int j=0; j<2; j++) {
Log.v(LOG_TAG, "entering for loop");
n++;
TableRow tr = new TableRow(getApplicationContext());
tr.setId(100+n);
String key = "hello";
float value = (float) 1.388);
value = value/3600;
TextView tvDynamicKey = new TextView(getApplicationContext());
TextView tvDynamicUnit = new TextView(getApplicationContext());
tvDynamicUnit.setText(Float.toString(value));
tvDynamicUnit.setGravity(Gravity.RIGHT);
tvDynamicKey.setText(key + " " + n);
tvDynamicKey.setPadding(3, 3, 3, 3);
tvDynamicUnit.setPadding(3, 3, 3, 3);
tr.addView(tvDynamicKey);
tr.addView(tvDynamicUnit);
table.addView(tr);
}
}
In your layout you always specify column1 for the left element (android:layout_column="1"). But you don't do it programmatically for the dynamic creation. You can do it like this. I tested it, it works on my environment.
tvDynamicKey.setLayoutParams(new TableRow.LayoutParams(1));
Related
I'm trying to build a table that will let me list results from a search. I want the two columns to be different widths (e.g. 30% and 70%). I have got this working for the column titles:-
<TableLayout
android:id="#+id/tableLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/textView1"
android:layout_marginTop="10dip"
android:padding="10dp"
android:shrinkColumns="*"
android:stretchColumns="*"
android:textColor="#color/white" >
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:weightSum="1" >
<TextView
android:id="#+id/TextViewTitle1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:text="Date"
android:background="#drawable/border"
android:textColor="#color/bluish" />
<TextView
android:id="#+id/TextViewTitle2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.7"
android:text="Task"
android:background="#drawable/border"
android:textColor="#color/bluish" />
</TableRow>
</TableLayout>
I then want to add rows to the table in my program where the columns have the same width as the title row, but my code generates rows where the columns are split 50%-50% on width:-
private void BuildTable(int rows, int cols) {
// outer for loop
for (int i = 1; i <= rows; i++) {
TableRow row = new TableRow(this);
row.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
for (int j = 1; j <= cols; j++) {
TextView tv1 = new TextView(this);
tv1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
tv1.setBackgroundResource(R.drawable.border);
tv1.setPadding(5, 5, 5, 5);
tv1.setText("R " + i + ", C" + j);
row.addView(tv1);
}
table_layout.addView(row);
}
}
I'd appreciate any help on getting the column width to the same as the title.
private void addRow(Data data){
TableRow row = (TableRow)View.inflate(getApplicationContext(), your table row , null);
//find your textviews and set data
(TextView)row.findViewById(R.id.TextViewTitlei1);
}
}
they will be distributed according to your required weight.
Well I've worked out what I needed to do this. Firstly set up a layout file, which I called table_row.xml
<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/tr_row"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:weightSum="1" >
<TextView
android:id="#+id/TextViewTitlei1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:text=" "
android:background="#drawable/border"
android:textColor="#color/bluish" />
<TextView
android:id="#+id/TextViewTitlei2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.7"
android:text=" "
android:background="#drawable/border"
android:textColor="#color/bluish" />
</TableRow>
Then this is a simplified version of the code to make column widths 30% and 70%
private void addTableRow(String resIdTitle, String strValue){
int yy=4;
for (int i = 1; i <= yy; i++) {
System.out.println("Step 16");
LayoutInflater inflater = getLayoutInflater();
TableRow tr = (TableRow)inflater.inflate(R.layout.table_row, table_layout, false);
System.out.println("Step 17");
// Add First Column
TextView tvTitle = (TextView)tr.findViewById(R.id.TextViewTitlei1);
tvTitle.setText(resIdTitle+ " "+i);
System.out.println("Step 18");
// Add the 3rd Column
TextView tvValue = (TextView)tr.findViewById(R.id.TextViewTitlei2);
tvValue.setText(strValue+ " "+i);
System.out.println("Step 19");
table_layout.addView(tr);
}
}
That appears to work.
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 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.
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);
When I create EditText programmatically I have no underline (in the second EditText). Like this: http://oi41.tinypic.com/2ut427d.jpg
When I create an EditText in XML everything is fine, see http://oi44.tinypic.com/nmxdnm.jpg. Where is the error?
This is my XML layout:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="10dp" >
<include layout="#layout/input_title_layout" />
<TableLayout
android:id="#+id/relativeLayout12"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" >
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="0dp" >
<TextView
style="#style/tvTitleStyle"
android:text="#string/diameter" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:text=":" />
<EditText
android:id="#+id/diameterValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:digits="0123456780."
android:inputType="numberDecimal" />
</TableRow>
</TableLayout>
</LinearLayout>
</ScrollView>
And code to add EditText programatically:
final TableLayout.LayoutParams tableParams = new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.FILL_PARENT, 1f);
final TableRow.LayoutParams rowParams = new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT);
rowParams.bottomMargin = 0;
TableLayout tableLayout = new TableLayout(context);
tableLayout.setLayoutParams(tableParams);
inputLinearLayout.addView(tableLayout);
TableRow tableRowEdit = new TableRow(context);
TextView titleTv = new TextView(context);
titleTv.setTextAppearance(context, R.style.tvTitleStyle);
titleTv.setText(context.getString(context.getResources().getIdentifier(inputTvs[i] , "string", context.getPackageName())));
TextView separatorTv = new TextView(context);
separatorTv.setGravity(Gravity.LEFT);
separatorTv.setPadding(5, 0, 5, 0);
separatorTv.setText(":");
separatorTv.setTextColor(Color.BLACK);
EditText editText = new EditText(context);
editText.setId(i);
tableRowEdit.addView(titleTv);
tableRowEdit.addView(separatorTv);
tableRowEdit.addView(editText);
tableLayout.addView(tableRowEdit);
It's hard to tell exactly what the issue might be given the code you supplied.
-Have you tried setting the layout parameters of the TableRow/TextView/EditText? The TextViews might be taking up all the visible space.
-Are you sure inputLinearLayout has been inflated?