Here is my xml:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/test"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
>
<TextView
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
<TextView
android:id="#+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
</TableRow>
I generate the rest dynamically.
Right now it centers it horizontally on the screen but everything is on the left side. I know its a simple fix but I can't seem to get it right.
Here is my manual code:
for(Item l : leaders)
{
// Make a new row
TableRow row = new TableRow(this);
row.setBackgroundColor(Color.DKGRAY);
TextView name= new TextView(this);
TextView score = new TextView(this);
createView(row, name, l.getName());
createView(row, score, String.valueOf(l.getScore()));
name.setTextColor(Color.MAGENTA); // Just to distinguish between the columns
// Add row to table layout
tl.addView(row);
}
}
private void createView(TableRow row, TextView textView, String string)
{
textView.setText(string);
textView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
textView.setTextColor(Color.CYAN);
textView.setTextSize(32);
textView.setPadding(20, 0, 0, 0);
row.setPadding(0, 1, 0, 1);
row.addView(textView);
}
Also, the given answers didn't change the layout.
If anybody has the same problem, the trick is to set
android:stretchColumns="0"
in the TableLayout and
android:layout_gravity="center"
android:layout_width="match_parent"
within the content views inside the rows.
I think u should write android:layout_gravity="center" in table layout instead of android:gravity="center".
and you can ad the layoutGravity Centre for Table Layout set in center in parrent Layouts
as
android:layout_gravity="center"
you can set the Gravity Centre
myTableLayout.setGravity(Gravity.CENTER);
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:
Here is a cut-down version of the table layout im using. I'm trying to simulate a HTML percentage width by using a weightSum of 100 on the table and applying layout_weight on each view appropriately.
From my C# code I'm then looking to add each line item dynamically as new TableRow to the table layout. I can't seem to figure out how to ensure that the columns line up in the new rows. This is different to HTML, where columns are always the same width per row.
I've been messing around LayoutParameters to try and set a weight, but doesn't seem possible on a textview.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
style="#style/pagetemplate">
<TableLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:minWidth="25px"
android:minHeight="25px"
android:weightSum="100"
android:id="#+id/idBasketTable"
style="#style/pagetemplate">
<TableRow>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="10"
android:text="Options"
android:visibility="invisible" />
<TextView
android:text="Product Name"
style="#style/textViewHeader"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="match_parent"
android:layout_weight="30"
android:layout_height="wrap_content" />
<TextView
android:text="Qty"
style="#style/textViewHeader"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="match_parent"
android:layout_weight="30"
android:layout_height="wrap_content" />
<TextView
android:text="Price"
style="#style/textViewHeader"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="match_parent"
android:layout_weight="30"
android:layout_height="wrap_content" />
</TableRow>
</TableLayout>
<include
layout="#layout/Menu"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" />
</RelativeLayout>
Then from my code I'm attempting something similar to this (cut down for simplicity). You'll notice my attempted LayoutParameter code commented out on each View as this was making them disappear all together from the layout. Not sure whether I should be concentrating on the layout styles of the row itself or the individual views?
Secondly, in the first row, I've tried to create a column that has no title. The line item rows will include a small delete button here, which is why I've tried to make an empty column. Is there a better way to do this rather than creating a textView (used for nothing) and setting to invisible?
TableLayout tl = (TableLayout)FindViewById(Resource.Id.idBasketTable);
TableRow tr = new TableRow(this)
{
LayoutParameters = new TableRow.LayoutParams(TableRow.LayoutParams.MatchParent, TableRow.LayoutParams.MatchParent, 100)
};
TextView tv_ProductName = new TextView(this)
{
Text = item.ItemName//,
// LayoutParameters = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MatchParent,ViewGroup.LayoutParams.WrapContent)
};
tv_ProductName.SetTextAppearance(this, Resource.Style.textViewDetail);
TextView tv_Qty = new TextView(this)
{
Text = item.ItemQty.ToString()//,
// LayoutParameters = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.WrapContent)
};
tv_Qty.SetTextAppearance(this, Resource.Style.textViewDetail);
TextView tv_Price = new TextView(this)
{
Text = item.ItemPrice.ToString()//,
// LayoutParameters = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.WrapContent)
};
tv_Price.SetTextAppearance(this, Resource.Style.textViewDetail);
ImageButton ib_Delete = new ImageButton(this)
{
// LayoutParameters = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MatchParent,ViewGroup.LayoutParams.WrapContent)
};
//set the view values for the current row.
ib_Delete.SetImageResource(Resource.Drawable.deleteitem);
Guid itemID = item.ItemID;
//assign the delete onclick event.
ib_Delete.Click += delegate
{
deleteItem_OnClick(ib_Delete, itemID);
};
//add views to the row.
tr.AddView(ib_Delete);
tr.AddView(tv_ProductName);
tr.AddView(tv_Qty);
tr.AddView(tv_Price);
//add the row to the table layout.
tl.AddView(tr);
Hope this all makes sense, appreciate the feedback :)
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 have a table layout after some view in my xml file. Below is the code.
<ScrollView>
<LinearLayout>
..................................
.....................................
<Button
android:id="#+id/fetchbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="show details"
android:layout_margin="6dip"
android:layout_gravity="center_horizontal"
android:onClick="buttonOnClick"/>
<TableLayout
android:id="#+id/mytable_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:orientation="vertical"
>
</TableLayout>
</LinearLayout>
</ScrollView>
Below is the code to add the rows programatically for a table layout:
public void buttonOnClick(View view)
{
showData();
}
public void showData()
{
List<DataSource> data = dbHelper.getData(selectedoption);
TableLayout tl = (TableLayout)findViewById(R.id.mytable_layout);
for(DataClass dataclass:data)
{
TableRow tr = new TableRow(ViewScreen.this);
TableRow.LayoutParams params = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT);
tr.setLayoutParams(params);
TextView labelTV = new TextView(ViewScreen.this);
labelTV.setText(dataclass.getColumnName());
labelTV.setTextColor(Color.RED);
TableRow.LayoutParams paramsTV1 = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT);
labelTV.setLayoutParams(paramsTV1);
tr.addView(labelTV);
// Creating a TextView to house the value of the after-tax income
TextView valueTV = new TextView(ViewScreen.this);
valueTV.setText(dataclass.getColumnValue());
valueTV.setTextColor(Color.BLACK);
TableRow.LayoutParams paramsTV2 = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT);
valueTV.setLayoutParams(paramsTV2);
tr.addView(valueTV);
// Adding the TableRow to the TableLayout
tl.addView(tr, new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
}
}
So as per the above code, it's working fine. But the table row is always with one line. The table row height is not increased according to the content. Instead the part of text goes off screen and invisible. Can some one please help to solve this issue?
Picture:
create a custom single_row.xml as below:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relativeLayoutSingleRowRecordsDetailedView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff" >
<TextView
android:id="#+id/textViewSingleRowRecordsDetailedViewCategoryField"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="10dp"
android:text="TextView"
android:textColor="#04B4AE"
android:textSize="15dp"
android:typeface="sans" />
<TextView
android:id="#+id/textViewSingleRowRecordsDetailedViewCategoryFieldValue"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"
android:text="TextView"
android:textColor="#000000"
android:textSize="15dp"
android:typeface="sans" />
</RelativeLayout>
And then in my java code:
...
LayoutInflater inflate = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View view = inflate.inflate(R.layout.single_row_records_detailed_view, null);
TextView tv_field_name = (TextView) view.findViewById(R.id.textViewSingleRowRecordsDetailedViewCategoryField);
TextView tv_field_value = (TextView) view.findViewById(R.id.textViewSingleRowRecordsDetailedViewCategoryFieldValue);
if(al_field_value.get(i).equals(""))
{
null_count = null_count + 1;
continue;
}
else
{
tv_field_name.setText(get_field_name(al_cat_field_id.get(i)));
tv_field_value.setText(al_field_value.get(i));
}
linearLayout_details.addView(view);
...
Try making your Textview multiline.
add below lines to your code to make textview multiline.
yourtextview.setSingleLine(false);
hope this helps
set the following to your tablelayout:-
tablename.setColumnShrinkable(ColumnIndex, true);
you can set the shrinkable attribute of the tablelayout to get the effect you want. the document from Android developer website :
The width of a column is defined by the row with the widest cell in
that column. However, a TableLayout can specify certain columns as
shrinkable or stretchable by calling setColumnShrinkable() or
setColumnStretchable(). If marked as shrinkable, the column width can
be shrunk to fit the table into its parent object. If marked as
stretchable, it can expand in width to fit any extra space. The total
width of the table is defined by its parent container. It is important
to remember that a column can be both shrinkable and stretchable. In
such a situation, the column will change its size to always use up the
available space, but never more. Finally, you can hide a column by
calling setColumnCollapsed().
example :
<TableLayout
android:id="#+id/fragment_print_job_detail_info_group"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:shrinkColumns="1"
android:layout_margin="#dimen/image_button_margin"
android:background="#drawable/round_corner_white_background_shape" >