Login Request Example in Android

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.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

About the Author

Leave a Reply 14 comments

Roney K Joseph - November 16, 2012 Reply

Your tutorials are very simple and superb

Thank you

DEEPAK GOYAL - November 24, 2012 Reply

Thanks

Vijaya - December 11, 2012 Reply

My Requirement is Login Example, I wanna enter constant username and password for login, if any one enter with empty it will also login to page. But i dont want to login with empty. Please seggest me.

Miduna - December 17, 2012 Reply

How we can Login with constant name as “admin” both username and password, in this example we can login with empty also

Micho - January 28, 2013 Reply

Package Name: com. example. LoginRequestExample without space!

Dennis - February 14, 2013 Reply

can u tel me the code for creating user id

Suha - April 26, 2013 Reply

great tutorial ..thank you :)

Peter - July 23, 2013 Reply

came @ the right time! Thanks! Although i am still not clean yet. Pls how do i correct d error “the method Onclick(View) from the type new View.OnclickListener(){} is never used locally”

Ahmed Ali - July 28, 2013 Reply

I wish to see real example of accessing remote database, and login using actual username and password!

Ronin - October 28, 2013 Reply

You have an error in LoginRequestExample activity source code, at line 25. I think it’s copy/paste issue..

fazlan - November 6, 2013 Reply

bro can you tell me how can i transfer this via bluetooth to another bluetooth device(well the device is arduino ) would be a great help

fazlan - November 6, 2013 Reply

Bro how can i send this user name and password via bluetooth for another bluetooth device(Other device arduino )

neal - November 21, 2013 Reply

nicely explained. thanks

saisushma - November 27, 2013 Reply

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

Leave a Reply: