Android TableLayout




Android TableLayout


1. Static TableLayout Creation in Android
2. Dynamic TableLayout Creation in Android


1. Static TableLayout Creation in Android
Step 1 : Select File -> New -> Project -> Android Application Project (or) Android Project. Fill the forms and click "Finish" button. If you have any doubt regarding create a new project Click Here.

Step 2 : Open res -> layout -> activity_main.xml (or) main.xml and add following code :

<TableLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TableRow
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
   
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/text1"
            android:text="Username"
            android:textSize="15dp"
            android:paddingLeft="20dp"
            android:paddingRight="10dp"
            android:paddingTop="10dp"
            android:paddingBottom="10dp"
            />
   
        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/text2"
            android:hint="Enter Username"
            android:textSize="15dp"
            android:paddingLeft="20dp"
            android:paddingRight="20dp"
            android:paddingTop="10dp"
            android:paddingBottom="10dp"
            />
   
    </TableRow>

    <TableRow >
   
        <TextView />
   
    </TableRow>

    <TableRow
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
   
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/text3"
            android:text="Password"
            android:textSize="15dp"
            android:paddingLeft="20dp"
            android:paddingRight="10dp"
            android:paddingTop="10dp"
            android:paddingBottom="10dp"
            />
   
        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/text4"
            android:hint="Enter Password"
            android:textSize="15dp"
            android:paddingLeft="20dp"
            android:paddingRight="20dp"
            android:paddingTop="10dp"
            android:paddingBottom="10dp"
            android:inputType="textPassword"
            />
   
   
    </TableRow>

    <TableRow >
   
        <TextView />
   
    </TableRow>

    <TableRow
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
   
         <TextView />
   
        <Button
            android:id="@+id/login"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Login"
            android:textSize="15dp"
            />
   
    </TableRow>

</TableLayout>


Step 3 : Open src -> package -> MainActivity.java and add following code :

package com.gudivada.hemanthsomaraju;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {

Button login;
EditText name, pwd;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
   
        login = (Button) findViewById(R.id.login);
        name = (EditText) findViewById(R.id.text2);
        pwd = (EditText) findViewById(R.id.text4);
   
        login.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {
// TODO Auto-generated method stub

if(name.getText().toString().equals("Hemanth") && pwd.getText().toString().equals("Somaraju"))
{
Toast.makeText(getBaseContext(), "Login Successfully !", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(getBaseContext(), "Login Failure \n\n Try Again",
Toast.LENGTH_SHORT).show();
}
}
});
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}


Step 4 : Open AndroidManifest.xml and add following code :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.gudivada.hemanthsomaraju"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.gudivada.hemanthsomaraju.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Step 5 : Our output will be like this :


 





2. Dynamic TableLayout Creation in Android

Step 1 : Select File -> New -> Project -> Android Application Project (or) Android Project. Fill the forms and click "Finish" button. If you have any doubt regarding create a new project Click Here.

Step 2 : Open src -> package -> MainActivity.java and add following code :

package com.gudivada.hemanthsomaraju;

import android.os.Bundle;
import android.app.Activity;
import android.text.InputType;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

TableLayout TL;
TableRow tr1, tr2, tr3;
TextView t1, t2, dummy;
EditText et1, et2;
Button login;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
   
        TL = new TableLayout(MainActivity.this);
   
        tr1 = new TableRow(MainActivity.this);
        tr2 = new TableRow(MainActivity.this);
        tr3 = new TableRow(MainActivity.this);
   
        t1 = new TextView(MainActivity.this);
        t2 = new TextView(MainActivity.this);
        dummy = new TextView(MainActivity.this);
   
        et1 = new EditText(MainActivity.this);
        et2 = new EditText(MainActivity.this);
   
        login = new Button(MainActivity.this);
   
        t1.setText("Username");
        t1.setTextSize(15);
        t1.setPadding(20, 20, 20, 20);
        tr1.addView(t1);
   
        et1.setHint("Enter Username");
        et1.setTextSize(15);
        et1.setPadding(20, 20, 20, 20);
        tr1.addView(et1);
   
        t2.setText("Password");
        t2.setTextSize(15);
        t2.setPadding(20, 20, 20, 20);
        tr2.addView(t2);
   
        et2.setHint("Enter Password");
        et2.setTextSize(15);
        et2.setPadding(20, 20, 20, 20);
        et2.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
        tr2.addView(et2);
   
        tr3.addView(dummy);
   
        login.setText("Username");
        login.setTextSize(15);
        login.setPadding(20, 20, 20, 20);
        tr3.addView(login);
   
        TL.addView(tr1);
        TL.addView(tr2);
        TL.addView(tr3);
   
        setContentView(TL);
   
        login.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {
// TODO Auto-generated method stub

if(et1.getText().toString().equals("Hemanth") && et2.getText().toString().equals("Somaraju"))
{
Toast.makeText(getBaseContext(), "Login Successfully !", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(getBaseContext(), "Login Failure \n\n Try Again",
Toast.LENGTH_SHORT).show();
}
}
});
    }
}


Step 3 : Open AndroidManifest.xml and add following code :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.gudivada.hemanthsomaraju"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.gudivada.hemanthsomaraju.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>


Step 4 : Our output will be like this :