TableLayout fills parent - android

i want to add a TabeLayout to a LinearLayout programmatically.
This is my activity_main.xml
<?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:id="#+id/content">
</LinearLayout>
and this is how i add the table in my MainActivity
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.content);
String[] content = new String[] {"Hello", "World", "Test"};
float d = getResources().getDisplayMetrics().density;
int dp = (int)(3*d);
TableLayout.LayoutParams tableParams =
new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT,
TableLayout.LayoutParams.WRAP_CONTENT);
TableRow.LayoutParams rowParams =
new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT);
TableLayout table = new TableLayout(this);
table.setLayoutParams(tableParams);
for(int i = 0; i < content.length; i++) {
TableRow row = new TableRow(this);
row.setLayoutParams(rowParams);
int length = content[i].length();
TextView textView = new TextView(this);
textView.setLayoutParams(rowParams);
textView.setText(content[i]);
textView.setPadding(dp, 0, dp, 0);
row.setBackgroundColor(Color.BLUE);
row.addView(textView);
table.addView(row);
}
table.setBackgroundColor(Color.GRAY);
linearLayout.addView(table);
}
}
as you can see the tableParams are set to wrap_content. However, when i set the background color of the table i see, that the width of the table is as long as the display. Why is that so and how can i change that?
This is the output:
http://postimg.org/image/e29bsg9vv/

TableLayout deals with width a bit differently.
You need to let the TableLayout know that it can shrink the columns.
Try:
table.setShrinkAllColumns(true);
Take a look at the reference docs for TableLayout. In particular, notice it states the following:
The total width of the table is defined by its parent container.
Also, as a tip, you can replace:
table.setLayoutParams(tableParams);
linearLayout.addView(table);
with:
linearLayout.addView(table, tableParams);

Related

Dynamically add TableRow to above of TableLayout

I want to dynamically add TableRow to above of TableLayout. I use blow code:
In my activity:
TableLayout table = (TableLayout)ChatActivity.this.findViewById(R.id.tblChats);
for(int i = 1; i < 10; ++i)
{
TableRow row = (TableRow)LayoutInflater.from(ChatActivity.this).inflate(R.layout.chat_row, null);
((TextView)row.findViewById(R.id.attrib_name)).setText("A");
((TextView)row.findViewById(R.id.attrib_value)).setText(i + "");
table.addView(row);
}
table.requestLayout();
chat_row.xml:
<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android">
<TextView android:id="#+id/attrib_name"
android:textStyle="bold"/>
<TextView android:id="#+id/attrib_value"
android:gravity="right"
android:textStyle="normal"/>
</TableRow>
As you know these codes adds TableRow in the bottom of the TableLayout.
Is there any way to add it to the above of TableLayout?
yes you can add row as runtime in table layout.
use below code like chart.xml
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/tblChats"
android:layout_width="match_parent"
android:layout_height="match_parent"> </TableLayout>
and make activity like below ..
public class ChartActivity extends AppCompatActivity {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.chart_layout);
TableLayout table = findViewById(R.id.tblChats);
for (int i = 0; i < 5; i++) {
TableRow row = new TableRow(this);
TableRow.LayoutParams lp = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT);
row.setLayoutParams(lp);
TextView textView = new TextView(this);
textView.setText("Name" + " " + i);
EditText editText = new EditText(this);
row.addView(textView);
table.addView(row, i);
}
}
}
According to Android Team's answer I realized I must change this line:
table.addView(row);
to this:
table.addView(row, 0);

Spread imageViews evenly over tablerow

Like the title says, I have problems in spreading ImageViews evenly over tableRows. My xml is:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ProgressBar/>
<TextView/>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/progressBar">
<TableLayout
android:id="#+id/tableLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
</TableLayout>
</ScrollView>
After the Activity is started a dynamical Table is created with the following code:
for (int i = 0; i < 4; i++) {
tableRow = new TableRow(this);
tableRow.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
tableRow.setId(i + 1);
tableRow.setBackgroundResource(R.mipmap.shelfboard);
tableRow.setPadding(20, 0, 20, 0);
tableRow.setOnClickListener(thisvariable);
tableLayout.addView(tableRow);
}
Now I want to add different numbers of ImageViews of the same width for each tableRow to each tableRow.
I have 4 TableRows
add 2 ImageViews to tableRow 1
add 5 ImageViews to tableRow 2
add 3 ImageViews to tableRow 3
add 7 ImageViews to tableRow 4
My Code is:
for (int i = 0; i < adpList.size(); i++) {
int row = adpList.get(i).getShelfBoard();
if (row==1) {
int productsPerBoard = 2;
int width = (tableLayout.getWidth()) / productsPerBoard;
TableRow.LayoutParams tlp = new TableRow.LayoutParams(width, TableRow.LayoutParams.MATCH_PARENT);
productPicture = new ImageView(this);
r = (TableRow) findViewById(row);
productPicture.setLayoutParams(tlp);
}
if (row==2) {
int productsPerBoard = 5;
int width = (tableLayout.getWidth()) / productsPerBoard;
TableRow.LayoutParams tlp = new TableRow.LayoutParams(width, TableRow.LayoutParams.MATCH_PARENT);
productPicture = new ImageView(this);
productPicture.setBackgroundResource(R.drawable.border);
r = (TableRow) findViewById(row);
productPicture.setLayoutParams(tlp);
}
if (row==3) {
int productsPerBoard = 3;
int width = (tableLayout.getWidth()) / productsPerBoard;
TableRow.LayoutParams tlp = new TableRow.LayoutParams(width, TableRow.LayoutParams.MATCH_PARENT);
productPicture = new ImageView(this);
r = (TableRow) findViewById(row);
productPicture.setLayoutParams(tlp);
}
if (row==4) {
int productsPerBoard = 7;
int width = tableLayout.getWidth() / productsPerBoard;
TableRow.LayoutParams tlp = new TableRow.LayoutParams(width, TableRow.LayoutParams.MATCH_PARENT);
productPicture = new ImageView(this);
r = (TableRow) findViewById(row);
productPicture.setLayoutParams(tlp);
}
productPicture.setBackgroundResource(R.mipmap.ic_launcher);
r.addView(productPicture);
Unfortunately every row has only two pictures. It always takes the width of the first board. My Output:
How can I add different numbers of ImageViews to each row?
I thought too complicated. Changing the LayoutParams solved my problem:
r = (TableRow) findViewById(shelfboard);
productPicture = new ImageView(this);
TableRow.LayoutParams tlp = new TableRow.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT, 1f);
tlp.setMargins(0, 20, 0, 50);
productPicture.setLayoutParams(tlp);

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].

If a ScrollView is the parent, it doesn't show a TableLayout into a RelativeLayout(Android)

I have this xml file:
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RelativeLayout android:id="#+id/conteiner_2"
android:layout_width="500px"
android:layout_height="500px"
android:background="#drawable/gradient_2">
</RelativeLayout>
</HorizontalScrollView>
</ScrollView>
And I am trying to create a TableLayout which if I delete the ScrollViews it appears, but when I let it like before the TableLayout isn't show.
This is my code:
TableLayout table = new TableLayout(this);
RelativeLayout.LayoutParams paramsTable = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
TableLayout.LayoutParams paramsRow = new TableLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,0,1.0f);
TableRow.LayoutParams paramsCol = new TableRow.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT,1.0f);
for(int row = 0 ; row < rows ; row++){
TableRow row = new TableRow(this);
row.setLayoutParams(paramsRow);
for(int col = 0 ; col < column ; col++){
TextView text = new TextView(this);
text.setLayoutParams(paramsCol);
text.setText(row + "-" + col);
row.addView(text);
}
table.addView(row);
}
tabla.setLayoutParams(paramsTable);
conteiner.addView(table);
Sorry for my poor English, I hope I made myself understood.
<<< EDIT >>>
I have to put another layout inside the RelativeLayout, and it works.
I have to put another layout inside the RelativeLayout, and it works.

Textview going out of screen in tablelayout

I am new to android and I have used a TableLayout and I am adding two columns of data into it from a string array. But my second column goes out of screen. It seems that all entries of my second column start from where the longest entry in the first column ends. How to make both the columns fit into the screen with like 50% width for both columns?
My code goes like this..
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.findia1);
TableLayout tl = (TableLayout) findViewById(R.id.tablay1);
for (int i = 0; i < s1.length; i += 2) {
TableRow tr1 = new TableRow(this);
tr1.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
TextView textview1 = new TextView(this);
textview1.setText(s1[i]);
tr1.addView(textview1);
TextView textview2 = new TextView(this);
textview2.setText(s1[i+1]);
tr1.addView(textview2);
tl.addView(tr1, new TableLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
}
xml code is
<?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" >
<TableLayout
android:id="#+id/tablay1"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
</TableLayout>
</LinearLayout>
Give weight android:layout_weight="1" for the TextView
i guess i got the answer.. i used
tl.setColumnShrinkable(0, true);
tl.setColumnShrinkable(1, true);
this does the job!

Categories

Resources