adding layoutWeightSum in a table row through java - android

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.

Related

DataGrid Android Studio horizontal vertical scroll

I am looking for solution where could dispay large data with horizontal and vetical scroll. Something like this https://www.syncfusion.com/blogs/post/new-datagrid-for-xamarinios-and-xamarinandroid.aspx. I would like to change number of columns and change the cell values.
Do you have please something like this? When it be a free i will be really happy. Thank you
You can create it by custom TableLayout. And the good news is you can add both side scrolling mechanism in TableLayout. You have to first set a TableLayout xml. For both side scrolling you have to add first ScrollView then HorizontalScrlollView then your TableLayout.
Now you you can create row and column. You can create it dynamically because you (may be) dont know how many column has required. So you have to create custom TableRow and cell and you can easily add your tableLayout .
TableRow row= new TableRow(this);
TableRow.LayoutParams lp = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT);
row.setLayoutParams(lp);
// then create your text view and add it row. and last add row in your table layout
This is just example. You can create your own data grid library
And you can some ready mate library to show data grid in table view like your reference. :
https://github.com/evrencoskun/TableView
https://github.com/ISchwarz23/SortableTableView

Adding TableRow Dynamically to TableLayout in xml

Although this looks a lot repeated question but first time for me. I searched all over and could not get the result and ended up posting here.
I am creating a table dynamically of which the TableLayout part is written in xml part.
<RelativeLayout
android:layout_width="70dp"
android:layout_height="40dp"
android:background="#color/colorPrimaryDark"
android:id="#+id/componentA">
<TableLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:gravity="center"
android:id="#+id/tl_componentA">
</TableLayout>
</RelativeLayout>
I created an object for the table
TableLayout tableLayoutA;
tableLayoutA= (TableLayout)view.findViewById(R.id.tl_componentA);
Now I tried to add a row dynamically from here onwards as
createTableRowColumn(tableLayoutA);
Functions Related are
private void createTableRowColumn(TableLayout tableLayout){
TableRow tableRow1= new TableRow(this.context);
tableRow1.setBackgroundColor(getResources().getColor(R.color.colorAccent));
setTableRowColumnProperty(tableRow1);
tableLayout.addView(tableRow1);
}
private void setTableRowColumnProperty(TableRow tableRow){
TableRow.LayoutParams layoutParams= new TableRow.LayoutParams(70, 40);
tableRow.setLayoutParams(layoutParams);
}
I did all this and nothing showed me in the emulater. But when I gave same structure in xml mannually then thing was working well.
For this reason i tried something to figure out
Toast.makeText(getContext(), tableLayoutA.getHeight()+"", Toast.LENGTH_LONG).show();
In toast it was showing me 0. I could not get why, although I have fixed the size for tableLayoutA in the xml itself.
A table in real life needs at least one row and one column.
You didn't see the table, because you only created a row, but, there were no column.
You need to add a column to the row for something to be visible.
Try this :
TextView label_date = new TextView(this);
label_date.setText("DATE");
label_date.setTextColor(Color.WHITE);
label_date.setPadding(5, 5, 5, 5);
tableRow.addView(label_date);// add the column to the table row here
tableLayout.addView(tableRow, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
You can replace the textView with whatever the value you want it to be.
tableLayout.addView(tableRow, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
Here what happens is, you say, add a view into tableLayout and you tell which view it is, and you also say the width and height of the view should wrap it's contents.
Also, specifying 40,70 is not a great idea, what happens when you have varying screen sizes. Furthermore, use a library to handle dynamic view addition and removal. You don't need to reinvent the wheel and save a lot of time and effort. For table views, https://github.com/EsotericSoftware/tablelayout might be a good one (not sure). Another question is, is table view what you are looking for? Make sure you are not mistaking recyclerView for table views, I say this because I don't understand your use case.
Useful link : https://technotzz.wordpress.com/2011/11/04/android-dynamically-add-rows-to-table-layout/

How to keep the width of the Controls in the TableRow column

I have a tablerow in which i put in a textview with width==50dp;But when i add another tablerow which contains a textview with width==100dp,then the first textview has to be stretched to 100dp.
How can keep the width of the first textview's width?
Use android:stretchColumns properties .
To Know more Check out Link
You can also try this in layout for both textviews:
android:layout_width="0dp"
android:layout_weight="1"
This will give equal width to both the rows.

Manage View of a Table Row at runtime

In my application i am creating and displaying a table at runtime. The table is developed in an Activity class. Everything working fine, only issue is the view of the table. There are four textviews in a row in the table. they are shown adjacently without seperation. How could i make a proper view with a definite space between textviews.
text1.setText("24/02/2011"); //TextView text
text2.setText("Andy"); //TextView text
text3.setText("3"); //TextView text
text4.setText("Repeat"); //TextView text
tableRow.addView(text1); //TableRow tableRow
tableRow.addView(text2); //TableRow tableRow
tableRow.addView(text3); //TableRow tableRow
tableRow.addView(text4); //TableRow tableRow
dueTL.addView(tableRow); //TableLayout dueTL
It display the data like : 24/02/2011Andy3Repeat
You can add weight to each TextView, also be sure that your TableView has fill_parent in its width. Also check the padding and margins of the TextViews
I hope this helps
If for some reason setting weights doesn't work for your situation, take a look at padding, paddingBottom, paddingLeft, paddingRight, and paddingTop in http://developer.android.com/reference/android/widget/TextView.html
You could either manually add a space to the strings in "setText" or you could set a right margin, if the surrounding ViewGroup supports it, on the TextViews either in XML (layout_marginRight) or via Code (LayoutParams).

Android dynamically give space between tablerows

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 >

Categories

Resources