Android table layout example

Three Files 


1) TableLayoutDatabase.java
2)tablelayoutitem.xml
3) tablelayoutexample.xml

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TableLayout;
import android.widget.TextView;

public class TableLayoutDatabase  extends Activity{
    private TableLayout tableLayout;
    View tableRow;

    @Override    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tablelayoutexample);
        tableLayout=(TableLayout)findViewById(R.id.tableLayout);

        for (int i=0;i<5;i++){
            tableRow = LayoutInflater.from(this).inflate(R.layout.tablelayoutitem,null,false);
            TextView history_display_no  = (TextView) tableRow.findViewById(R.id.history_display_no);
            TextView history_display_date  = (TextView) tableRow.findViewById(R.id.history_display_date);
            TextView history_display_orderid  = (TextView) tableRow.findViewById(R.id.history_display_orderid);
            TextView history_display_quantity  = (TextView) tableRow.findViewById(R.id.history_display_quantity);

            history_display_no.setText(""+(i+1));
            history_display_date.setText("2014-02-05");
            history_display_orderid.setText("S0"+(i+1));
            history_display_quantity.setText(""+(20+(i+1)));
            tableLayout.addView(tableRow);
        }

    }
}

2.)tablelayoutitem.xml

xml version="1.0" encoding="utf-8"?><TableRow xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:orientation="horizontal">

    <TextView        android:id="@+id/history_display_no"        android:layout_width="0dp"        android:layout_height="wrap_content"        android:layout_weight="0.25"        android:gravity="center"        android:textStyle="bold" />

    <TextView        android:layout_width="0dp"        android:layout_height="match_parent"        android:id="@+id/history_display_date"        android:layout_weight="0.25"        android:gravity="center"/>

    <TextView        android:id="@+id/history_display_orderid"        android:layout_width="0dp"        android:layout_height="match_parent"        android:layout_weight="0.25"        android:gravity="center"        android:lineSpacingExtra="12sp"        android:textColor="@android:color/holo_orange_dark" />
    <TextView        android:layout_width="0dp"        android:layout_height="match_parent"        android:id="@+id/history_display_quantity"        android:layout_weight="0.25"        android:gravity="center"/>

</TableRow>

3)tablelayoutexample.xml

xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/tableLayout"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:padding="5dp"
    android:orientation="vertical"
    >
</TableLayout>
Reactions:

0 comments:

Post a Comment