Login Request Example in Android

2

This example shows how to create and handle login form in android.

Algorithm:

1.) Create a new project by File-> New -> Android Project name it LoginRequestExample.

2.) Write following into main.xml:


<RelativeLayout 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" >

    <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:stretchColumns="1">
     
<TableRow> 
 <TextView 
 android:text="User Name: " 
 android:id="@+id/TextView01" 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content">
 </TextView>
 
 <EditText 
 android:text="" 
 android:id="@+id/txtUname" 
 android:layout_width="fill_parent" 
 android:layout_height="wrap_content">
 </EditText>
</TableRow>


<TableRow>
 <TextView 
 android:text="Password: " 
 android:id="@+id/TextView02" 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content">
 </TextView>


 <EditText 
 android:text="" 
 android:id="@+id/txtPwd" 
 android:layout_width="fill_parent" 
 android:layout_height="wrap_content"
 android:password="true">
 </EditText>
</TableRow>


<TableRow>
 <Button
 android:text="Cancel" 
 android:id="@+id/btnCancel" 
 android:layout_width="fill_parent" 
 android:layout_height="wrap_content">
 </Button>


 <Button
 android:text="Login" 
 android:id="@+id/btnLogin" 
 android:layout_width="fill_parent" 
 android:layout_height="wrap_content">
 </Button>


</TableRow>


</TableLayout>

</RelativeLayout>

3.) Run for output.

Steps:

1.) Create a project named LoginRequestExample and set the information as stated in the image.

Build Target: Android 4.0
Application Name: LoginRequestExample
Package Name: com. example. LoginRequestExample
Activity Name: LoginRequestExample
Min SDK Version: 11

2.) Open LoginRequestExample.java file and write following code there:

package com.example.loginrequestexample;

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

public class LoginRequestExample extends Activity {

	EditText txtUserName;
	 EditText txtPassword;
	 Button btnLogin;
	 Button btnCancel;
	 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        txtUserName=(EditText)this.findViewById(R.id.txtUname);
        txtPassword=(EditText)this.findViewById(R.id.txtPwd);
        btnLogin=(Button)this.findViewById(R.id.btnLogin);
        btnLogin=(Button)this.findViewById(R.id.btnLogin);
        btnLogin.setOnClickListener(new OnClickListener() {
   
   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    
    if((txtUserName.getText().toString()).equals(txtPassword.getText().toString())){
           Toast.makeText(LoginRequestExample.this, "Login Successful",Toast.LENGTH_LONG).show();
          } else{
           Toast.makeText(LoginRequestExample.this, "Invalid Login",Toast.LENGTH_LONG).show();
          }
    
   }
  });       
    }
     
} 

3.) Compile and build the project.

Output

  • neal

    nicely explained. thanks

  • saisushma

    i am getting an error that main cannot be resolved or its not a field.login cannot be resolved or its a field.txtPassword cannot be resolved or its not a field….can some one help me out as soon as possible thank u

Proudly designed by Theme Junkie.