This is the table header:
<android.support.v4.widget.NestedScrollView
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:scrollbars="vertical"
android:id="#+id/roster_bat_stats_table_header_adv1"
android:scrollbarStyle="outsideInset"
android:fillViewport="true"
android:layout_gravity="top">
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="#drawable/divider_stand_table_list"
android:showDividers="middle"
android:isScrollContainer="true"
android:stretchColumns="9"
android:orientation="horizontal">
<TableRow android:layout_width="match_parent"
android:id="#id/header_table_row"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_weight="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="BAT"
android:background="#drawable/mybutton"
android:textAlignment="center"
android:textColor="#android:color/white"
android:layout_weight="0.0652"
android:textSize="18sp" />
etc...
Here is the actual table without header:
<android.support.v4.widget.NestedScrollView
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="#+id/nestedScrollBatStatsAdv1"
android:scrollbars="vertical"
android:scrollbarStyle="outsideInset"
android:fillViewport="true"
android:layout_gravity="top">
<TableLayout
android:id="#+id/rosterBatStatsTableAdv1"
android:layout_width="match_parent"
android:visibility="gone"
android:layout_height="match_parent"
android:divider="#drawable/divider_stand_table_list"
android:showDividers="middle"
android:isScrollContainer="true"
android:stretchColumns="9"
android:orientation="horizontal">
</TableLayout>
</android.support.v4.widget.NestedScrollView>
I'm adding android:layout_weight="0.0652" in the header section above.
The actual table rows are added programmaticaly by creating TableRow and setting weights for each element inside of it:
double[] weigths = new double[]{0.0893, 0.0893, 0.2500, 0.0833, 0.1012, 0.0952, 0.0893, 0.1071, 0.0952};
creating table row:
TableRow tableRow = new TableRow(context);
TableRow.LayoutParams lp = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT, 1);
tableRow.setLayoutParams(lp);
tableRow.setOrientation(TableRow.HORIZONTAL);
tableRow.setBackgroundColor(resources.getColor(trainingPlayerSelectRowColor));
tableRow.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
tableRow.setTag(player.getId());
Creating elements inside of tablerow:
for (int i = 0; i < weights.length; ++i) {
TextView h1 = new TextView(context);
h1.setText(values[i]);
TableRow.LayoutParams params = new TableRow.LayoutParams(0, TableRow.LayoutParams.WRAP_CONTENT, (float) weights[i]);
h1.setLayoutParams(params);
h1.setTextColor(resources.getColor(standingsTableHeadTXT));
if (i == 2) {
h1.setGravity(Gravity.LEFT);
} else {
h1.setGravity(Gravity.CENTER);
}
h1.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
tableRow.addView(h1);
}
The values array is an array of Strings.
I made weights (layout_weight) in the header_table_row same as the rows added programatically in the actual table. But they do not match:
Is there any mistake or how can I make columns in both tables match?
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:
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.
So, I have a TableLayout with 4 TableRow in one xml file. I also have a xml file defining a TextView that will be inserted in the row (3 TextView per row).
I'd like those TextView to have the same width and that was working well when everything was in the same xml file (at the begining my TextView were "hard-coded") but now that I add them programmatically, they don't have the same width.
Here is the first XML file
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/homeTable"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/lmbg"
android:clickable="true"
android:padding="20dp" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center"
android:minHeight="200dp" >
</TableRow>
<TableRow
android:id="#+id/tableRow2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center"
android:minHeight="200dp" >
</TableRow>
<TableRow
android:id="#+id/tableRow3"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center"
android:minHeight="200dp" >
</TableRow>
<TableRow
android:id="#+id/tableRow4"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center"
android:minHeight="200dp" >
</TableRow>
</TableLayout>
This is my TextView item
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:layout_weight="1"
android:clickable="true"
android:gravity="center"
android:onClick="goToList"
android:textSize="14sp" />
And finally this is how I add them programmatically
for(int i = 0; i < mTopCategories.size(); i++){
Category c = mTopCategories.get(i);
TableRow row = ((TableRow)findViewById(rowID[idIdx])) ;
cat = (TextView) getLayoutInflater().inflate(R.layout.cat_grid_item, null);
int iconID = getResources().getIdentifier(c.getSlug().replace('-', '_'), "drawable", getPackageName());
cat.setCompoundDrawablesWithIntrinsicBounds(0, iconID, 0, 0);
int textID = getResources().getIdentifier(c.getSlug().replace('-', '_'), "string", getPackageName());
cat.setText(textID);
cat.setTag(c.getId().toString());
row.addView(cat);
}
Try explicitly setting the layout params on the TextViews like this:
TableRow.LayoutParams lParams = new TableRow.LayoutParams(0, -2 /* WRAP_CONTENT */, 0.33f /* 33% of width */);
cat.setLayoutParams(lParams);
See http://developer.android.com/reference/android/widget/TableRow.LayoutParams.html#TableRow.LayoutParams%28int,%20int,%20float%29 for more info.
Try this way,you even no required any xml.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TableLayout table = new TableLayout(this);
table.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.MATCH_PARENT));
int id=1;
for (int i=0;i<4;i++){
TableRow tableRow = new TableRow(this);
tableRow.setOrientation(TableLayout.HORIZONTAL);
for(int j=0;j<3;j++){
TextView textView = new TextView(this);
textView.setGravity(Gravity.CENTER);
textView.setId(id++);
textView.setText(String.valueOf(textView.getId()));
textView.setTag(textView.getId());
textView.setTextColor(Color.WHITE);
textView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(context, "Id is : " + v.getId(), Toast.LENGTH_SHORT).show();
}
});
textView.setBackgroundColor(Color.BLUE);
TableRow.LayoutParams textviewParams = new TableRow.LayoutParams(0, TableRow.LayoutParams.MATCH_PARENT,1f);
textviewParams.setMargins(5, 5, 5, 5);
tableRow.addView(textView,textviewParams);
}
table.addView(tableRow,new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT,0,1f));
}
setContentView(table);
}
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?
I'm a beginner in Android devt (started 3 days ago) and have a problem declaring a View's Layout. I have checked everywhere on how to set a TextView's margins programatically and so far, none of them work. The TextView always disappears when I apply the Layout.
Here's my code:
TableLayout tView = (TableLayout)findViewById(R.id.AllDocumentsTable);
TableRow trView = buildRow();
TextView tViewProjTitle = buildCell();
tViewProjTitle.setText(doc.project);
TextView tViewDocTitle = buildCell();
tViewDocTitle.setText(doc.document);
trView.addView(tViewProjTitle);
trView.addView(tViewDocTitle);
try {
tView.addView(trView, i);
}
catch (Exception e) {
Log.e("adding tablerow", e.getMessage());
}
buildRow()..
private TableRow buildRow(){
TableRow retRow = new TableRow(this);
retRow.setBackgroundColor(Color.WHITE);
TableLayout.LayoutParams rowLayout = new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT,
TableLayout.LayoutParams.WRAP_CONTENT);
rowLayout.setMargins(2, 2, 2, 2);
retRow.setLayoutParams(rowLayout);
return retRow;
}
buildCell()..
private TextView buildCell(){
TextView retTView = new TextView(this);
retTView.setBackgroundColor(Color.WHITE);
retTView.setGravity(0);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(2, 2, 2, 2);
retTView.setLayoutParams(params);
return retTView;
}
My Activity's layout.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff">
<HorizontalScrollView android:id="#+id/horizontalScrollView1" android:layout_width="wrap_content" android:layout_height="wrap_content">
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff">
<TableLayout android:layout_height="wrap_content"
android:id="#+id/AllDocumentsTable"
android:layout_width="match_parent"
android:background="#ffffff">
<TableRow android:layout_margin="2dp"
android:background="#000000">
<TextView android:text="Test Text1." android:layout_margin="2dp" android:background="#ffffff"></TextView>
<TextView android:text="Test Text2" android:layout_margin="2dp" android:background="#ffffff"></TextView>
</TableRow>
</TableLayout>
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
Help!!! :)
you can set the margins like this:
LinearLayout.LayoutParams lp=(LinearLayout.LayoutParams)textview.getLayoutParams();
lp.topMargin=10;
lp.leftMargin=10;