Programatically add view to match parent to fill TableLayout - android

I am trying to dynamically create a View to act as a spacer between table rows however my view is only going as wide as the text even though I am trying to have them match parent.
I am dynamically adding to the TableLayout with
TableRow tr = new TableRow(this);
tr.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
tr.setOrientation(LinearLayout.VERTICAL);
tr.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
layout.setPadding(15, 15, 10, 15);
TextView description = new TextView(this);
description.setText(row.getString(TAG_DESCRIPTION));
description.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.black));
description.setTextSize(25);
TextView dateCreated = new TextView(this);
dateCreated.setText("Date Created: " + row.getString(TAG_DATEADDED).replace("\\", ""));
dateCreated.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.black));
dateCreated.setTextSize(20);
TextView reportType = new TextView(this);
reportType.setText("Report Type: " + row.getString(TAG_REPTYPE));
reportType.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.black));
reportType.setTextSize(20);
View view = new View(this);
view.setMinimumHeight(2);
view.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color.black));
view.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
layout.addView(description);
layout.addView(dateCreated);
layout.addView(reportType);
layout.addView(view);
tr.addView(layout);
existingReports.addView(tr);
and I define my TableLayout in my XML with
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/txt_numExistingReports"
android:layout_alignStart="#+id/txt_numExistingReports"
android:layout_below="#+id/txt_numExistingReports"
android:layout_alignParentBottom="true"
android:background="#android:color/white"
android:layout_toLeftOf="#+id/home"
android:layout_toStartOf="#+id/home">
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="30dp"
android:showDividers="middle"
android:id="#+id/existingReports"
android:layout_marginTop="5dp"
android:divider="?android:attr/dividerHorizontal"
android:background="#android:color/white"/>
</ScrollView>

The scrollview width is Wrap Content...try with Match Parent

Related

Modify columns width from code android

I would like to know if there is a way to modify the width of two columns in a table layout (with three columns in total).
The problem is that every column has the same width and a lot of space is wasted because the first two only contain an id and a date meanwhile the last one contains a multiline description.
This is the code in which the table layout is populated:
tr_head.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
tr_head.setBackgroundColor(Color.rgb(0, 230, 230));
TextView lblSystemStoricalHeaderId = new TextView(this);
lblSystemStoricalHeaderId.setText("ID ");
tr_head.addView(lblSystemStoricalHeaderId);
TextView lblSystemStoricalHeaderData = new TextView(this);
lblSystemStoricalHeaderData.setText("Data");
tr_head.addView(lblSystemStoricalHeaderData);
TextView lblSystemStoricalHeaderDescription = new TextView(this);
lblSystemStoricalHeaderDescription.setText(" Descrizione");
tr_head.addView(lblSystemStoricalHeaderDescription);
tr_head.setMinimumHeight(30);
tlSystemStorical.addView(tr_head, new TableLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
for(int i=0;i<reportList.length;i++){
TableRow tr = new TableRow(this);
tr.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
TextView lblSystemStoricalId = new TextView(this);
lblSystemStoricalId.setText(""+reportList[i].getId()+"");
tr.addView(lblSystemStoricalId);
TextView lblSystemStoricalData = new TextView(this);
long dataC = reportList[i].getDate()*1000;
java.util.Date df = new java.util.Date(dataC);
String vv = new SimpleDateFormat("dd/MM/yyyy").format(df);
lblSystemStoricalData.setText(vv);
tr.addView(lblSystemStoricalData);
TableRow.LayoutParams tv3Params = new TableRow.LayoutParams();
tv3Params.width=0;
tv3Params.weight=1;
TextView lblSystemStoricalDescription = new TextView(this);
lblSystemStoricalDescription.setText(""+reportList[i].getReportDescription());
lblSystemStoricalDescription.setLayoutParams(tv3Params);
tr.addView(lblSystemStoricalDescription);
tr.setBackgroundResource(R.drawable.border);
tr.setMinimumHeight(40);
tlSystemStorical.addView(tr, new TableLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
}
and this is the xml file:
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/scroll_view_system_description"
>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="*"
android:id="#+id/tl_system_storical"
>
</TableLayout>
</FrameLayout>
</ScrollView>
You can set width property to 0 for TableRow children, but specify layout_weight for them. So you'd write:
float[] weights = new float[] { 0.2f, 0.2f, 0.6f};
TextView lblSystemStoricalId = new TextView(this);
lblSystemStoricalId.setLayoutParams(new TableRow.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT, weights[0]))
Where 0 is no width for view (because you're using weights), and weights array contains weights for your three views.
These weights should sum up to 1, and each weight is percent of the width that the column should take. So you need to assign weights to your TableRow children - lblSystemStoricalId would get weights[0], lblSystemStoricalData would get weights[1] and lblSystemStoricalDescription would have weights[2].

android Table Layout does not fit the screen

I'm using a table layout with a vertical scroll view to display a huge list of Data programmaticlly. On a tablet it works with a perfect way but the problem when I run it on a phone like galaxy or nexus 1 the width of the table layout does not match the width of the screen so two or three columns are hidden I have tried all combination match_parent , wrap_content ... I used an horizontal scroll view it works but I don't think that it's a good solution
this is the xml file :
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout 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"
tools:context="..."
>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mv_table"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:layout_marginTop="50dp">
</TableLayout>
</RelativeLayout>
</ScrollView>
this is my code :
TableLayout mvtable = (TableLayout) findViewById(R.id.mv_table);
TableRow tr_head = new TableRow(this);
tr_head.setId(10);
tr_head.setBackgroundColor(Color.rgb(24,65,95));
TextView label_Date = new TextView(this);
label_Date.setId(20);
label_Date.setText("Date");
label_Date.setTextColor(Color.WHITE);
label_Date.setPadding(5, 5, 5, 5);
tr_head.addView(label_Date);
TextView label_Debit = new TextView(this);
label_Debit.setId(21);
label_Debit.setText("Débit");
label_Debit.setTextColor(Color.WHITE);
label_Debit.setPadding(5, 5, 5, 5);
tr_head.addView(label_Debit);
.....
tr_head.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
mvtable.addView(tr_head, new TableLayout.LayoutParams(
TableLayout.LayoutParams.FILL_PARENT,
TableLayout.LayoutParams.WRAP_CONTENT));
//setting data programmaticlly
int c ;
int i=1;
for (Movement mv : movementsList)
{
TableRow tr = new TableRow(this);
if (i % 2 != 0)
{
c=Color.WHITE;
tr.setBackgroundColor(Color.GRAY);}
else
{c=Color.BLACK;
}
tr.setId(100 + i);
tr.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
TextView labelDate = new TextView(this);
labelDate.setId(200+i);
labelDate.setText(mv.getDate());
labelDate.setTextColor(c);
labelDate.setPadding(5, 5, 5, 5);
tr.addView(labelDate);
TextView labelDebit = new TextView(this);
labelDebit.setId(210+i);
labelDebit.setText(mv.getDebit());
labelDebit.setTextColor(c);
labelDebit.setPadding(5, 5, 5, 5);
labelDebit.setBackgroundColor(Color.rgb(255,174,185));
tr.addView(labelDebit);
......
tr.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
mvtable.addView(tr, new TableLayout.LayoutParams(
TableLayout.LayoutParams.FILL_PARENT,
TableLayout.LayoutParams.WRAP_CONTENT));
i++;
}
use LinearLayout instead of RelativeLayout it may solve your problem. because RelativeLayout overlaps one on other

Android LinearLayout in ScrollView

I tryning to get a LinearLayout in a ScrollView like this:
The greenspace should be wrap_content and the red one should take the remaining space.
But this is my Result:
This is my Code:
foreach(Ele el : elements) {}
LinearLayout layout = new LinearLayout(getActivity());
LayoutParams layout_parm = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
layout.setOrientation(LinearLayout.HORIZONTAL);
layout.setLayoutParams(layout_parm);
TextView tv = new TextView(getActivity());
tv.setLayoutParams( new LayoutParams(0, LayoutParams.WRAP_CONTENT, 1) );
tv.setBackgroundColor(getActivity().getResources().getColor((R.color.red)));
tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
tv.setText("Name...");
tv.setPadding(0, 0, 0, getDP(5));
layout.addView(tv);
TextView iView = new TextView(getActivity());
iView.setText("OTO");
iView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
iView.setBackgroundColor(getActivity().getResources().getColor((R.color.green)));
iView.setLayoutParams( new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 0) );
layout.addView(iView);
frg_layout.addView(layout);
}
I am so confused! Maybe you can help me to find out my fail...
Thank you!
The Parent-ViewGroup:
<LinearLayout
android:id="#+id/frg_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:gravity="top"
android:orientation="vertical" />
you change the layout orientation
LinearLayout layout = new LinearLayout(getActivity());
LayoutParams layout_parm = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
layout.setOrientation(LinearLayout.VERTICAL);
layout.setLayoutParams(layout_parm);
uset this type in xml
<LinearLayout
android:id="#+id/frg_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/TextView1"
android:layout_width="250dp"
android:layout_height="60dp"/>
<TextView
android:id="#+id/textview2"
android:layout_width="250dp"
android:layout_height="60dp"
/>
</linearlayout>
I think you've mostly got it right, just need a few minor tweaks:
for each(Ele el:elements){
LinearLayout layout = new LinearLayout(getActivity());
LayoutParams layout_parm = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
layout.setOrientation(LinearLayout.HORIZONTAL);
layout.setLayoutParams(layout_parm);
TextView tv = new TextView(getActivity());
//adjust the last number here to make the first block wider or larger as you want
tv.setLayoutParams(new LayoutParams(0, LayoutParams.WRAP_CONTENT, 2));
tv.setBackgroundColor(getActivity().getResources().getColor((R.color.red)));
tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
tv.setText("Name...");
tv.setPadding(0, 0, 0, getDP(5));
layout.addView(tv);
TextView iView = new TextView(getActivity());
iView.setText("OTO");
iView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
iView.setBackgroundColor(getActivity().getResources().getColor((R.color.green)));
//adjust your layout param to be a weight instead of wrap_content
iView.setLayoutParams(new LayoutParams(0, LayoutParams.WRAP_CONTENT, 1));
layout.addView(iView);
frg_layout.addView(layout);
}
See if that works! An alternative would be to build an xml layout which I think is generally cleaner, but programatically it should work roughly the same way.
Using LayoutInflater with xml will bring the solutuion!
Thank you!

TableRow is being added but doesn't display

I have a small app that periodically needs a table row added to the tablelayout. Per the getChildCount() method the row is being added but it doesn't display. Any suggestions would be greatly appreciated
TableLayout tbl = (TableLayout)findViewById(R.id.mytbl);
TableRow row = new TableRow(this);
LinearLayout ll = new LinearLayout(this);
TextView txtView = new TextView(this);
row.setId(1234);
row.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,TableRow.LayoutParams.WRAP_CONTENT));
row.setGravity(Gravity.CENTER);
row.setBackgroundResource(android.R.color.darker_gray);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT,1.0f);
ll.setId(1589);
ll.setLayoutParams(lp);
ll.setOrientation(LinearLayout.VERTICAL);
ll.setBackgroundResource(R.color.header_background);
ll.setGravity(Gravity.CENTER);
String msg = "Sample Text";
txtView.setId(657);
txtView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
txtView.setTextColor(R.color.a_color);
txtView.setBackgroundResource(R.drawable.border_a);
txtView.setGravity(Gravity.TOP | Gravity.LEFT);
txtView.setText(msg);
ll.addView(txtView);
row.addView(ll);
tbl.addView(row,new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));//maybe add this with layout params tl.addView(tr,new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
i've tried tbl.RequestLayout() and tbl.Invalidate() but no help. The table is wrapped in a scrollview which is my best guess as to where the problem is. I've been tinkering with this for several hours so a nudge in the right direction sure would be helpful
<ScrollView
android:id="#+id/scrVw_lst"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TableLayout
android:id="#+id/mytbl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stretchColumns="*">
Have you run the code for adding a row in your TableLayout? I've simulated adding a row on a click of a Button and I get a awkward Divide by 0 exception.
Anyway, change the LayoutParams of the enclosing Linearlayout to TableRow.LayoutParams(its parent is the TableRow):
TableRow.LayoutParams lp = new TableRow.LayoutParams(
TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.WRAP_CONTENT, 1.0f);
ll.setId(1589);
ll.setLayoutParams(lp)
Also, if this doesn't solve the problem, try to add:
android:fillViewport="true"
to the ScrollView tag in the xml layout.

Add TableRow dynamically to linearlayout android

I need to dynamically add some tablerows to a linearlayout in my app. I write this code:
LinearLayout tabella = (LinearLayout) findViewById(R.id.tabella_contatori);
for(int i =0; i<array_list.size(); i++){
TableRow row = new TableRow(getApplicationContext());
row.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
TextView data = new TextView(getApplicationContext());
data.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 0.2f));
data.setTextAppearance(getApplicationContext(), android.R.attr.textAppearanceMedium);
data.setTextColor(Color.BLACK);
data.setBackgroundColor(Color.WHITE);
data.setPadding(2, 0, 0, 0);
data.setText("asd");
row.addView(data);
tabella.addView(row);
}
}
But when I open the app nothing appens. I already check if array_list.size is bigger than 0.
How can I do?
Thanks, Mattia
The problem is in your TextView Layout Params. The type should be TableRow.LayoutParams not LinearLayout.LayoutParams.
data.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
Take Table layout and try using this in your main.xml ...
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/myTableLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView android:text="Some Text"/>
</TableRow>
</TableLayout>
in your Activity
this.setContentView(R.layout.main);
/* Find Tablelayout defined in main.xml */
TableLayout tl = (TableLayout)findViewById(R.id.myTableLayout);
/* Create a new row to be added. */
TableRow tr = new TableRow(this);
tr.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
/* Create a TextView to be the row-content. */
TextView data = new TextView(getApplicationContext());
data.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, 0.2f));
data.setTextAppearance(getApplicationContext(), android.R.attr.textAppearanceMedium);
data.setTextColor(Color.BLACK);
data.setBackgroundColor(Color.WHITE);
data.setPadding(2, 0, 0, 0);
data.setText("asd");
/* Add TextView to row. */
tr.addView(data);
/* Add row to TableLayout. */
tl.addView(tr,new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));

Categories

Resources