In my Android application there is a screen with some data to be displayed in table format. This table will have around 5 columns and at least 50 rows.
How do I do I create a table in android?
I searched through and everybody seems to recommend to use TableLayout and textView for each cell. Using this technique seems to be a bit difficult as there are lots of textView Components.
Is there any other solution to this?
TableLayout lTableLayout;
lTableLayout = (TableLayout)findViewById(R.id.tblayout);
for(int i=0;i<10;i++){
TableRow tr1 = new TableRow(this);
tr1.setPadding(0, 5, 0, 0);
lTableLayout.addView(tr1, LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
TextView tv11 = new TextView(this);
tv11.setPadding(2, 0, 0, 0);
tv11.setTextSize(12);
tr1.addView(tv11,LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); // Can give width and height in numbers also.
}
These will create 10 rows and 1 textview in each row in taken table layout.
take one table layout in your xml file like below :
<TableLayout
android:id="#+id/tblayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</TableLayout>
Use ListView with Custom adapter , create five textview's in listview item as columns..
create listitem like this
row.xml
<?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">
<TextView android:text="TextView" android:id="#+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
<TextView android:text="TextView" android:id="#+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
<TextView android:text="TextView" android:id="#+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
<TextView android:text="TextView" android:id="#+id/textView4" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
<TextView android:text="TextView" android:id="#+id/textView5" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
</LinearLayout>
Its not necessary to user TableLayout.
you can user Custom ListView And set header of ListView as your Column name
To create custom listview see this example
Related
I am writing an Android app that requires me to programmatically add a TextView to a TableRow which is, in turn, added to a TableLayout. For the design to look proper, the TextView width must be the same as the TableRow width. The problem I am having is that even though I have attempted to set the width of the TextView to match using MATCH_PARENT, it is not taking effect.
Here is a sample of the Java that I am trying to use to do this:
final TableRow myTableRow = new TableRow(getApplicationContext());
final TextView myTextView = new TextView(getApplicationContext());
myTextView.setWidth(TableRow.LayoutParams.MATCH_PARENT);
myTextView.setPadding(5, 0, 5, 0);
myTextView.setTextColor(Color.BLACK);
myTextView.setTextSize(30);
myTextView.setBackgroundColor(Color.RED);
myTextView.setText("1");
myTableRow.addView(myTextView);
myTableLayout.addView(myTableRow); // Assume 'myTableLayout' has already been declared.
Here is the XML for myTableLayout:
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/myTableLayout">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/tableRowOne">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="#+id/textViewOne"
android:layout_weight="1"
android:textSize="40sp"
android:paddingStart="5dp"
android:paddingEnd="5dp" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/tableRowTwo">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="#+id/textViewTwo"
android:textColor="#000000"
android:textSize="30sp"
android:paddingEnd="5dp"
android:paddingStart="5dp"
android:layout_weight="1" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/tableRowThree">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textViewThree"
android:textColor="#000000"
android:textSize="20sp"
android:paddingEnd="5dp"
android:paddingStart="5dp"
android:layout_weight="1" />
</TableRow>
</TableLayout>
Does anyone have any idea what I could be doing wrong?
Here is a picture of the way the layout appears. The code posted here is not identical to the actual app code, but it is enough to show the problem. Specifically, the code that was removed concerns the replies. In the example code posted as part of the question, only one TextView is shown in the Java code, but in the actually project, there are two.
Layout Example Image
There are several ways you can handle this.
TableLayout and TableRow both extends linearlayout, so maybe you need to set Orientation.
you could set the layout params(margin params) like this
layout_width = 0dp ;
layout_weight = 1 ;
The best way to give height and width property to a view is to use Layout Parameters. most of times (almost all the times for me) setting width and height directly wont work.
TableRow.LayoutParams mParam=new TableRow.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
// set layout parameter to view
textView.setLayoutParams(mParam);
I am attempting to display data from the on-board SQLite database within a table in the Android UI, but it isn't working as it should. Here is what I have:
activity_main.xml
<TableLayout
android:id="#+id/data_table"
android:stretchColumns="3"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableRow>
<TextView android:layout_column="1" android:text="id" android:layout_width="0dp" android:layout_weight="1" />
<TextView android:layout_column="2" android:text="firstname" android:layout_width="0dp" android:layout_weight="1" />
<TextView android:layout_column="3" android:text="lastname" android:layout_width="0dp" android:layout_weight="1" />
</TableRow>
</TableLayout>
MainActivity.java
List<People> listOfPeople = databaseHelper.getAllPeople();
TableLayout myTableLayout = (TableLayout) findViewById(R.id.data_table);
for(People person : listOfPeople){
TableRow newTableRow = new TableRow(this);
TableRow.LayoutParams rowParams = new TableRow.LayoutParams();
newTableRow.setLayoutParams(rowParams);
TextView idTV = new TextView(this);
idTV.setLayoutParams(new TableRow.LayoutParams(1));
idTV.setText(person.getId());
TextView firstnameTV = new TextView(this);
firstnameTV.setLayoutParams(new TableRow.LayoutParams(2));
firstnameTV.setText(person.getFirstname());
TextView lastnameTV = new TextView(this);
lastnameTV.setLayoutParams(new TableRow.LayoutParams(3));
lastnameTV.setText(person.getLastname());
newTableRow.addView(idTV);
newTableRow.addView(firstnameTV);
newTableRow.addView(lastnameTV);
myTableLayout.addView(newTableRow);
}
The table and data is displayed, but the data within the rows I built from the database are all smooshed together and unreadable. If I were to compare this to web development, it looks like all the data are in one cell of the table row.
I am very new at Android development (former JSP developer), and what I have here, I have tried to piece together from Google searches. I need to use a TableLayout so a grid layout (or any other layout) will not do.
EDIT:
I added some background color to my "cells" and discovered they are in different cells. The issue, apparently, is that they are not distributing themselves correctly as I am not sure how to add "layout_width" and "layout_weight" to the cells within Java. At least, I guess that is what the issue is.
Just change your xml to this:
<TableLayout
android:id="#+id/data_table" android:stretchColumns="1"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableRow>
<TextView android:layout_column="1" android:layout_weight="1" android:text="id" />
<TextView android:layout_column="2" android:layout_weight="1" android:text="firstname" />
<TextView android:layout_column="3" android:layout_weight="1" android:text="lastname" />
</TableRow>
Make sure to also set the layout_weight to 1, for all the TextViews added by java code.
I really need help
I have a ResultSet as a result of select query from MySQL DB. how can I display it in table layout?? I have setup a layout for tableview
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical">
<EditText
android:id="#+id/myFilter"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/some_hint" />
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:padding="10dp"
android:text="#string/some_text" android:textSize="20sp" />
<TableLayout
android:id="#+id/producttablelayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:dividerPadding="#dimen/activity_horizontal_margin"
android:scrollbars="horizontal|vertical"
android:showDividers="none|beginning|middle|end" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="top|bottom|left|right"
android:paddingTop="#dimen/activity_horizontal_margin" >
<TextView
android:id="#+id/ttt1"
android:text="Column 1"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/ttt2"
android:text="Column 12"
android:textAppearance="?android:attr/textAppearanceLarge" />
</TableRow>
</TableLayout>
</LinearLayout>
I want to be able to add ResultSet data into it. Something like:
super.onCreate(savedInstanceState);
String s = null;
setContentView(R.layout.activity_list_all_products);
// Show the Up button in the action bar.
setupActionBar();
context333=this;
TableLayout myTable = (TableLayout)findViewById(R.id.producttablelayout);
try
{
connect2 = DriverManager.getConnection(LogonActivity.url, LogonActivity.user, LogonActivity.password);
statement2 = connect2.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
preparedStatement2 = connect2.prepareStatement("select article_code,article_desc from products limit 4");
resultSet2=preparedStatement2.executeQuery();
resultSet2.beforeFirst();
while (resultSet2.next()) {
how to add next record here to the table layout??
}
Please help
You can create a new Views (it seems you are populating your table rows with TextViews) and populate them with the results from your query as you wish. Then you can create a new TableRow with a constructor (see the documentation).
Add the views to to table row instnace using:
tableRowInstance.addView(viewInstance);
and then add the tableRow to the tableLayout the same way:
myTable.addView(tableRowInstance)
TableLayouts and TableRows are both subclasses of View. You should do some research on ViewGroup, View, and how they are structured. This link provides nice visualizations.
I need to create a dinamic table which will be fit with rows of data. The rows I want will have an specific form, like this:
So, to get it, I used relativeLayouts. The problem is that I first tried it with an .xml to see what parameters will have the differents views that it forms, and I get it, but now I cant replicate it in the code.
Here is my code:
TableLayout linkRoutesTable = (TableLayout) findViewById(R.id.layoutLinkRoutes);
//ROW:
TableRow linkRouteRow = new TableRow(this);
linkRouteRow.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
linkRouteRow.setId(i)
//RELATIVELAYOUT:
float px0 = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1, r.getDisplayMetrics());
RelativeLayout relativeLayout = new RelativeLayout(this);
relativeLayout.setLayoutParams(new RelativeLayout.LayoutParams(0, (int)px0)));
//CONTENT:
// BUTTON: ...
// NUMBER:...
// ADDRESS:...
// NAME: ...
relativeLayout.addView(number);
relativeLayout.addView(name);
relativeLayout.addView(address);
relativeLayout.addView(button);
linkRouteRow.addView(relativeLayout);
linkRouteTable.addView(linkRouteRow);
linkRoutesTable.refreshDrawableState();
It doesn't show anything...
The .xml show ok, here it is:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center" android:orientation="vertical">
<TableRow>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="0dp"
android:layout_weight="1"
android:id="#+id/layoutLinkRoutes" >
<Button android:text="Button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="#+id/button1" android:layout_alignParentTop="true" android:layout_alignParentRight="true"></Button>
<TextView android:textStyle="bold" android:textSize="45dp" android:text="1" android:id="#+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:layout_marginLeft="22dp"></TextView>
<TextView android:text="TextView" android:id="#+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="#+id/button1" android:layout_toRightOf="#+id/textView1" android:layout_marginLeft="16dp"></TextView>
<TextView android:text="TextView" android:id="#+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:layout_alignParentTop="true" android:layout_alignLeft="#+id/textView3"></TextView>
</RelativeLayout>
</TableRow>
What Am I doing wrong in the code? I need some help, I tried everything.
If I dont use the relativeLayouts and the rows, it shows something but bad... so the problem is the rows and the relativelayout
Really thanks...
You don't have to define the layout in code to fill a table dynamically. You can infalte a layout which you define in xml like a normal layout.
So you define your table row layout one time in a xml file and infalte it multiple times into your table layout.
Check this answer.
I want to add a new row below previous ones. I mean I have a UI containing 2 text boxes and 1 submit button. When I fill the text box and press submit, the content of text box is added below the previous one. Is this done with listview or tableview?
Please give suggestions to me and how to implement that technique. And after item is added in listview and when I click on any of list position, it performs different-2 activity. And I am new in Android.
TextView exampleTextView = (TextView) findViewById(R.id.field1);
exampleTextView.setText("wedwqe");
TableLayout table = (TableLayout)findViewById(R.id.table);
TableRow tr = (TableRow) findViewById(R.id.row);
tr.addView(exampleTextView);
// table.removeView(tr);
table.addView(tr);
And the XML file is:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/table"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center"
android:background="#DDDDDD"
android:stretchColumns="1" >
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/row">
<TextView android:id="#+id/field1"
android:text=" "
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dip"
android:textStyle="bold"
android:textSize="18dip"
/>
<TextView android:id="#+id/field2"
android:padding="3dip"
android:text=" "
android:textSize="18dip"
android:gravity="right"
/>
</TableRow>
</TableLayout>
You can use addView(View child, int index) that you can find in here
I hope this helps