Simple Event handling Android application.

Post date: Aug 6, 2011 11:12:58 AM

Hi,

Life is so thrilling, we don't know what will happen next. Why I am saying this means, now my career has been changed into desktop developer to mobile developer. So now I started to learning about android application. This is my first and simplest android login application. I hope it will help you to understand the event handling.

1. In Eclipse File->New->Others->Android Project ->Project Name is HelloAndroidActivity

2. Just open your main.xml file and past the following code

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

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

    android:orientation="vertical"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:weightSum="1">

    <TableLayout android:layout_width="fill_parent"    android:layout_height="fill_parent" android:id="@+id/tableLayout1"

     android:stretchColumns="1">

        <TableRow >

            <TextView android:text="UserName"   android:maxLength="50" android:id="@+id/lblUsername" >

            </TextView>

            <EditText android:hint="Enter Password" android:id="@+id/txtUserName">

                <requestFocus></requestFocus>

            </EditText>

        </TableRow>       

        <TableRow >

            <TextView android:text="Password"  android:id="@+id/lblPassword" ></TextView>

         <EditText  android:inputType="textPassword" android:hint="Enter Password" android:maxLength="20" android:id="@+id/txtPassword"></EditText>

        </TableRow>

              

        <TableRow android:id="@+id/tableRow3" android:baselineAligned="true">

            <Button android:id="@+id/btnLogin" android:bufferType="spannable" android:text="Login" android:clickable="true"></Button>         

         <TextView android:visibility="invisible" android:text="Result" android:id="@+id/lblResult" android:textColor="@color/RED" ></TextView>

        </TableRow>  

    </TableLayout>   

</LinearLayout>

 3.Past this code into Main class

import android.app.Activity;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;

import android.widget.TextView;

public class HelloAndroidActivity extends Activity {

    /** Called when the activity is first created. */

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        //Add click event to login button

        Button btnLogin = (Button) findViewById(R.id.btnLogin);

        btnLogin.setOnClickListener(new View.OnClickListener(){

 

     @Override

     public void onClick(View v) {

            // TODO Auto-generated method stub

            doLogin();

           

     }       

        });

    }

           private void doLogin() {

            // TODO Auto-generated method stub

            EditText txtusername = (EditText) findViewById(R.id.txtUserName);

            EditText txtPassword = (EditText)findViewById(R.id.txtPassword);

            TextView lblUser= (TextView) findViewById(R.id.lblResult);

            if(txtusername.getText().toString().equalsIgnoreCase("muruga") && txtPassword.getText().toString().equalsIgnoreCase("great")){

           

                  lblUser.setVisibility(1);

                  lblUser.setText("Successfully Logged in");

            }else{

                  lblUser.setVisibility(1);

                  lblUser.setText("Login failed!");

            }

           

      }

   

}