Login Request Example in Android

14

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

Did you enjoy this article?
Share
the
Love
Get Free Updates

14 Responses

  1. Your tutorials are very simple and superb

    Thank you

  2. DEEPAK GOYAL says:

    Thanks

  3. Vijaya says:

    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.

  4. Miduna says:

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

  5. Micho says:

    Package Name: com. example. LoginRequestExample without space!

  6. Dennis says:

    can u tel me the code for creating user id

  7. Suha says:

    great tutorial ..thank you :)

  8. Peter says:

    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”

  9. Ahmed Ali says:

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

  10. Ronin says:

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

  11. fazlan says:

    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

  12. fazlan says:

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

  13. neal says:

    nicely explained. thanks

  14. saisushma says:

    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


eight + 7 =

Proudly designed by Theme Junkie.