I am implementing a table layout and adding table rows to it dynamically. I want give space between these rows. may I know how to do it. I am also adding a textView to the tableRow. I am declaring the table layout in the xml file. I tried giving padding inside the table row. But that seems to work within the tableRows and not between tableRows. please help.
You need to set the top or bottom margin of rows.
Here is an example: Programmatically set margin for TableRow
To set the margin between rows in a table layout you need to use the following XML
<Tablelayout> android:stretchColumns="*" </Tablelayout >
Related
I want to show a table row with three columns (TextView) but i want to divide the table row in three equal parts.
This is easily possible by setting a layoutWeightSum in TableRow in XML and making layout_weight=1 for all theww TextView.
But i adding the table at run time through java and not by xml.
All i know is the TableRow.LayoutParameter do not provide any thing for weightSum. How can i do this?
TableRow.LayoutParams pmRow = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT);
You can achieve this by following code.
TableLayout layout = //...findViewbyid
layout.setStretchAllColumns(true);
And make width 0dp to all the children (views) of TableRow like below.
<TextView
android:id="#+id/txt_no1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="hi" />
Note
Above is XML layout design, but you can create run time code also. Its just for demonstration.
I've got you covered, mate!
myTableRow.setWeightSum(float weightSum);
use pmRow.span=int value
like pmRow.span=3;
Create a LinearLayout xml (horizontal) with the three required Items ( TextViews). To add the row item at runtime, inflate the above said layout, and set text values and add it.
I have 2 table layout . I want to hide the second layout and move the third layout to second position.
I'm hiding the the second layout and but don't know how to move the third layout to second position
TableLayout i=(TableLayout)findViewById(R.id.table2);
TableLayout i2=(TableLayout)findViewById(R.id.table3);
TableLayout i3=(TableLayout)findViewById(R.id.table4);
i.setVisibility(View.GONE);
i2.setVisibility(View.GONE);
i3.setVisibility(View.GONE);
TableLayout i5=(TableLayout)findViewById(R.id.table5);
i5.// Now i want to add this layout below to table layout R.id.table1 (the first table layout in this section)
Try setting
i2.setVisibility(View.INVISIBLE);
Use view.removeView(id) of the table layout then when u want use view.addView(id)
You can do it like, as you said you are using RelativeLayout, so make a child LinearLayout with vertical orientation, then add all the table layouts you want to add.
Bow if you want to add a tableLayout at runtime at the first position, then do:
linearLayout.removeAllViews();
Then add the required TableLayout(here, i5) and after that add the remaining Layouts.
Edit: Your code:
TableLayout i=(TableLayout)findViewById(R.id.table2);
TableLayout i2=(TableLayout)findViewById(R.id.table3);
TableLayout i3=(TableLayout)findViewById(R.id.table4);
i.setVisibility(View.GONE);
i2.setVisibility(View.GONE);
i3.setVisibility(View.GONE);
TableLayout i5=(TableLayout)findViewById(R.id.table5);
i5.// Now i want to add this layout below to table layout R.id.table1 (the first table layout in this section)
Are you going right, as i think your code should look like:
TableLayout i=(TableLayout)findViewById(R.id.table1);
TableLayout i2=(TableLayout)findViewById(R.id.table2);
TableLayout i3=(TableLayout)findViewById(R.id.table3);
i.setVisibility(View.GONE);
i2.setVisibility(View.GONE);
i3.setVisibility(View.GONE);
TableLayout i5=(TableLayout)findViewById(R.id.table5);
i5.// Now i want to add this layout below to table layout R.id.table1 (the first table layout in this section)
I suggested this because of your comment section after i5 in your above code.
I'm trying to fit a 20x20 table into a view using a programmatically built TableLayout. The setStretchAllColumns/setShrinkAllColumns methods work perfectly for squeezing all the columns in, but I haven't found a way to get all the rows in.
Is there a corresponding method for packing the rows in?
There aren't any setStretchAllColumns/setShrinkAllColumns for rows in a TableLayout. One thing that you could try(I haven't tested it) is to give your TableLayout a weigtSum of 20(with the method setWeightSum()) and then set the weight(the layout_weight attribute from a xml layout) of 1 for each of your 20 TableRows in the TableLayout.
I am trying to create a table row element where the first line contains a title followed by an ImageView that should be all of the way to the right. The TextView should fill the entire width of the phone except for the ImageView at the end.
The solution depends on some details of the circumstances.
If all the rows have the same format
If all the rows have the same column layout, then set android:stretchColumns="0" on the TableLayout. This will make the first column (index 0) stretch to fill any remaining space.
If the header has a different format from the remaining rows
If you have more columns or need a different column layout for the rest of the rows, then you need to do something different. I don't believe any single item can span multiple columns.
If only the positioning is important and you don't really need to span across multiple rows, you can use the android:layout_column attribute on each of the header items. The column number is 0-based, so the first column is 0. The Eclipse layout builder doesn't seem to present this attribute, but it will handle the attribute if you type it into the xml.
If you can't fit your elements inside the same columns the rest of the table uses, then the header doesn't belong in a TableRow. As suggested in another answer, you can use a RelativeLayout instead of the TableRow. Alternately, you could move the header outside of the table.
First, use a RelativeLayout for your row. Use the typical ImageView, give it an id, and use the XML attribute android:layout_alignParentRight="true". Next, use the TextView, use android:layout_toLeftOf="#id/myid" and make sure it's width is "fill_parent".
edit: code tags
On an Android layout, I'd like to have a set of rows, each with two TextViews. The leftmost column of TextViews should be right-aligned, just left of an imaginary centerline down the screen. The rightmost column should be left-aligned.
Examples of this can be seen at http://stuff.greenberg.org/ScopeCalc.htm
What's the best layout to use?
IMO, TableLayout would be a logical choice with appropriate use of colspan/rowspan.
You can also do this using LinearLayout, with the two sub-views of each row each getting 50% of the width.
Using GridLayout you can apply columnspan and rowspan properties to the views inside the grid.