Skip to content
September 21, 2010 / Daniel Freeman

Dr Android Answers: Row and Column layouts

Hi,
I wish to have the resource to programme the row and column layout.  Further, for the information updated, how can I host the data and let my application to retrieve the information or data?  Thanks for your concern.
Looking forward for your feedbacks.
WC

There are two common ways to display information as rows and columns.  The first is the GridLayout that I described in the previous post.  Or there’s a TableLayout, which imposes a row and column format on the contents.

For example:-

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="*" >
  <TableRow>
    <TextView android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="abcedfghij" />
    <TextView android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="klmn" />
  </TableRow>
  <TableRow>
    <TextView android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="opqr" />
    <TextView android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="stuvwxyz" />
  </TableRow>
</TableLayout>

Inside the <TableLayout>, we define each row’s contents inside <TableRow> tags.  Notice the … android:stretchColumns=”*”.  This means pad all the columns out so that the table takes up all the horizontal space.

But suppose that you don’t know how many rows your table has.  If your application is downloading data, you might now know how many rows you need to generate.  In this case, you might dynamically create your table in Java.

package com.danielfreeman.android;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.ViewGroup;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class HelloDynamicTable extends Activity {

	protected static final String[][] DATA = {{"Fred","Bloggs","32"},{"Jane","Doh","28"},{"Peter","Smith","35"}};

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        TableLayout table = new TableLayout(this);
        table.setLayoutParams(new TableLayout.LayoutParams(
        		TableLayout.LayoutParams.FILL_PARENT,
        		TableLayout.LayoutParams.WRAP_CONTENT)
        );
        table.setStretchAllColumns(true);

        for (String[] record : DATA) {

        	TableRow row = new TableRow(this);
        	row.setLayoutParams(new TableRow.LayoutParams(
            		TableRow.LayoutParams.FILL_PARENT,
            		TableRow.LayoutParams.WRAP_CONTENT)
            );

            for (String field : record) {
            	TextView column = new TextView(this);
            	column.setLayoutParams(new TableRow.LayoutParams(
            			TableRow.LayoutParams.FILL_PARENT,
            			TableRow.LayoutParams.WRAP_CONTENT)
                );
            	column.setText(field);
            	row.addView(column);
            }

            table.addView(row);
        }

        setContentView(table);
    }
}

You’ve asked about hosting data too. Do you mean on the server?  On the course, you will have learnt about obtaining and parsing XML data from the server.  My groups did an exercise using the SAX parser.  I think Mark’s group looked at the DOM parser.  In your application, are you also uploading changes, and storing them in a database on the server?  What exactly will your application do?

Perhaps this RSS reader tutorial will help:

http://www.helloandroid.com/tutorials/newsdroid-rss-reader

Advertisements

2 Comments

Leave a Comment
  1. W / Sep 22 2010 1:04 pm

    I need to update the data on weekly basis.
    May i know where the data can be stored?
    My apps is about the daily consumer good pricing.

    • Daniel Freeman / Sep 23 2010 3:03 am

      You’d save this kind of data on the server.

      If you’re looking for free hosting at the moment, I use http://www.byethost.com/ . And I’d recommend them.

      There are two approaches. You could just edit and upload an XML file every month. Or you could utilise a database, like mySQL. If you have no preference, I’d go with PHP for implementing your server-side code. It’s free, and easy to learn.

To discuss MadComponents/MC3D, join the Facebook group!

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: