QNA > W > What Is The Best Way To Display A Table Of Data In Android?

What is the best way to display a table of data in Android?

Hello,

Here is the java code to implement this.
First create a java file named ActivityMain.java and copy the contents into it.

Here is the ActivityMain.java file

package com.rajatpc.example;

import android.app.Activity;

import android.graphics.Color;

import android.graphics.Typeface;

import android.os.Bundle;

import android.widget.TableLayout;

import android.widget.TableRow;

import android.widget.TextView;

import android.widget.TableRow.LayoutParams;

public class ActivityMain extends Activity {

String companies[] = {"Google","Windows","iPhone","Nokia","Samsung",

"Google","Windows","iPhone","Nokia","Samsung",

"Google","Windows","iPhone","Nokia","Samsung"};

String os[] = {"Android","Mango","iOS","Symbian","Bada",

"Android","Mango","iOS","Symbian","Bada",

"Android","Mango","iOS","Symbian","Bada"};

TableLayout tl;

TableRow tr;

TextView companyTV,valueTV;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

tl = (TableLayout) findViewById(R.id.maintable);

addHeaders();

addData();

}

/** This function add the headers to the table **/

public void addHeaders(){

/** Create a TableRow dynamically **/

tr = new TableRow(this);

tr.setLayoutParams(new LayoutParams(

LayoutParams.FILL_PARENT,

LayoutParams.WRAP_CONTENT));

/** Creating a TextView to add to the row **/

TextView companyTV = new TextView(this);

companyTV.setText("Companies");

companyTV.setTextColor(Color.GRAY);

companyTV.setTypeface(Typeface.DEFAULT, Typeface.BOLD);

companyTV.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));

companyTV.setPadding(5, 5, 5, 0);

tr.addView(companyTV); // Adding textView to tablerow.

/** Creating another textview **/

TextView valueTV = new TextView(this);

valueTV.setText("Operating Systems");

valueTV.setTextColor(Color.GRAY);

valueTV.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));

valueTV.setPadding(5, 5, 5, 0);

valueTV.setTypeface(Typeface.DEFAULT, Typeface.BOLD);

tr.addView(valueTV); // Adding textView to tablerow.

// Add the TableRow to the TableLayout

tl.addView(tr, new TableLayout.LayoutParams(

LayoutParams.FILL_PARENT,

LayoutParams.WRAP_CONTENT));

// we are adding two textviews for the divider because we have two columns

tr = new TableRow(this);

tr.setLayoutParams(new LayoutParams(

LayoutParams.FILL_PARENT,

LayoutParams.WRAP_CONTENT));

/** Creating another textview **/

TextView divider = new TextView(this);

divider.setText("-----------------");

divider.setTextColor(Color.GREEN);

divider.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));

divider.setPadding(5, 0, 0, 0);

divider.setTypeface(Typeface.DEFAULT, Typeface.BOLD);

tr.addView(divider); // Adding textView to tablerow.

TextView divider2 = new TextView(this);

divider2.setText("-------------------------");

divider2.setTextColor(Color.GREEN);

divider2.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));

divider2.setPadding(5, 0, 0, 0);

divider2.setTypeface(Typeface.DEFAULT, Typeface.BOLD);

tr.addView(divider2); // Adding textView to tablerow.

// Add the TableRow to the TableLayout

tl.addView(tr, new TableLayout.LayoutParams(

LayoutParams.FILL_PARENT,

LayoutParams.WRAP_CONTENT));

}

/** This function add the data to the table **/

public void addData(){

for (int i = 0; i < companies.length; i++)

{

/** Create a TableRow dynamically **/

tr = new TableRow(this);

tr.setLayoutParams(new LayoutParams(

LayoutParams.FILL_PARENT,

LayoutParams.WRAP_CONTENT));

/** Creating a TextView to add to the row **/

companyTV = new TextView(this);

companyTV.setText(companies[i]);

companyTV.setTextColor(Color.RED);

companyTV.setTypeface(Typeface.DEFAULT, Typeface.BOLD);

companyTV.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));

companyTV.setPadding(5, 5, 5, 5);

tr.addView(companyTV); // Adding textView to tablerow.

/** Creating another textview **/

valueTV = new TextView(this);

valueTV.setText(os[i]);

valueTV.setTextColor(Color.GREEN);

valueTV.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));

valueTV.setPadding(5, 5, 5, 5);

valueTV.setTypeface(Typeface.DEFAULT, Typeface.BOLD);

tr.addView(valueTV); // Adding textView to tablerow.

// Add the TableRow to the TableLayout

tl.addView(tr, new TableLayout.LayoutParams(

LayoutParams.FILL_PARENT,

LayoutParams.WRAP_CONTENT));

}

}

}

Here is the activity_main.xml file

<?xml version="1.0" encoding="utf-8"?>

xmlns:android="http://schemas.android.com/apk/res/android"

android:id="@+id/LinearLayout01"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:scrollbars="none">

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:stretchColumns="0,1"

android:id="@+id/maintable" >

Now the Strings.xml file

<?xml version="1.0" encoding="utf-8"?>

Hello World, CreateTable!

CreateTableRows Dynamically RajatPc.com

Hope it might be help you.. :)

Thanks.

Di Seleta

Gli smartphone Nokia sono ancora indistruttibili? :: È possibile convertire programmi x86/x86_64 in versioni ARM?
Link utili