User Login and Registration Application In Andriod

In this tutorial we will show you how to create a User Login and Registration Application In Android with database. This application is written in Java and Xml file. We create this Login and Registration to help other users or programmers creating their first application in their android projects.

Sample Code

For the Login Form UI Design to be visible in the user.
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     xmlns:tools="http://schemas.android.com/tools"
  4.     android:orientation="vertical"
  5.     android:layout_height="match_parent"
  6.     android:padding="10dp"
  7.     android:layout_width="match_parent"
  8.     android:background="@color/colorPrimary">
  9.  
  10.     <TextView
  11.         android:layout_width="wrap_content"
  12.         android:text="HandyClass"
  13.         android:layout_gravity="center_horizontal"
  14.         android:textSize="30dp"
  15.         android:padding="20dp"
  16.         android:layout_marginTop="80dp"
  17.         android:textColor="#fff"
  18.         android:layout_height="wrap_content" />
  19.  
  20.     <TextView
  21.         android:layout_width="wrap_content"
  22.         android:text="Username"
  23.         android:textColor="#fff"
  24.         android:layout_height="wrap_content" />
  25.  
  26.     <EditText
  27.         android:id="@+id/etUsername"
  28.         android:layout_width="match_parent"
  29.         android:layout_marginBottom="10dp"
  30.         android:layout_height="wrap_content" />
  31.  
  32.     <TextView
  33.         android:layout_width="wrap_content"
  34.         android:text="Password"
  35.         android:textColor="#fff"
  36.         android:layout_height="wrap_content" />
  37.  
  38.     <EditText
  39.         android:id="@+id/etPassword"
  40.         android:layout_width="match_parent"
  41.         android:inputType="textPassword"
  42.         android:layout_marginBottom="10dp"
  43.         android:layout_height="wrap_content" />
  44.    
  45.     <Button
  46.         android:id="@+id/bLogin"
  47.         android:text="LOGIN"
  48.         android:layout_marginBottom="10dp"
  49.         android:layout_width="match_parent"
  50.         android:layout_height="wrap_content" />
  51.  
  52.     <TextView
  53.         android:id="@+id/tvRegisterLink"
  54.         android:layout_width="wrap_content"
  55.         android:text="Register Here"
  56.         android:textStyle="bold"
  57.         android:textColor="#fff"
  58.         android:layout_gravity="center_horizontal"
  59.         android:layout_height="wrap_content" />
  60.  
  61. </LinearLayout>
Script for the function of Login to create a session or start the activity through the main class form.
  1. package com.example.rinvizle.myfirstapplication;
  2. public class Login extends AppCompatActivity implements View.OnClickListener {
  3.  
  4.     private Button bLogin;
  5.     private EditText etUsername, etPassword;
  6.     private TextView tvRegisterLink;
  7.     private DbHelper db;
  8.     private Session session;
  9.  
  10.     @Override
  11.     protected void onCreate(Bundle savedInstanceState) {
  12.         super.onCreate(savedInstanceState);
  13.         setContentView(R.layout.activity_login);
  14.  
  15.         db = new DbHelper(this);
  16.         session = new Session(this);
  17.         etUsername = (EditText) findViewById(R.id.etUsername);
  18.         etPassword = (EditText) findViewById(R.id.etPassword);
  19.         bLogin = (Button) findViewById(R.id.bLogin);
  20.         tvRegisterLink = (TextView) findViewById(R.id.tvRegisterLink);
  21.  
  22.         bLogin.setOnClickListener(this);
  23.         tvRegisterLink.setOnClickListener(this);
  24.  
  25.         if(session.loggedin()){
  26.             startActivity(new Intent(Login.this, MainActivity.class));
  27.             finish();
  28.         }
  29.     }
  30.  
  31.     @Override
  32.     public void onClick(View v) {
  33.         switch (v.getId()){
  34.             case R.id.bLogin:
  35.                 login();
  36.                 break;
  37.  
  38.             case R.id.tvRegisterLink:
  39.                 startActivity(new Intent(Login.this, Register.class));
  40.                 finish();
  41.                 break;
  42.             default:
  43.         }
  44.     }
  45.  
  46.     private void login(){
  47.         String username = etUsername.getText().toString();
  48.         String password = etPassword.getText().toString();
  49.  
  50.         if (db.getUser(username,password)){
  51.             startActivity(new Intent(Login.this, MainActivity.class));
  52.             finish();
  53.         }
  54.         else{
  55.             Toast.makeText(getApplicationContext(), "Wrong Username and Password",Toast.LENGTH_SHORT).show();
  56.         }
  57.     }
  58. }
And for the Registration UI Design.
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     xmlns:tools="http://schemas.android.com/tools"
  4.     android:orientation="vertical"
  5.     android:layout_height="match_parent"
  6.     android:padding="10dp"
  7.     android:layout_width="match_parent"
  8.     android:background="@color/colorPrimary">
  9.  
  10.     <TextView
  11.         android:layout_width="wrap_content"
  12.         android:text="HandyClass"
  13.         android:layout_gravity="center_horizontal"
  14.         android:textSize="30dp"
  15.         android:padding="20dp"
  16.         android:layout_marginTop="30dp"
  17.         android:textColor="#fff"
  18.         android:layout_height="wrap_content" />
  19.  
  20.     <TextView
  21.         android:layout_width="wrap_content"
  22.         android:text="Name"
  23.         android:textColor="#fff"
  24.         android:layout_height="wrap_content" />
  25.  
  26.     <EditText
  27.         android:id="@+id/etName"
  28.         android:layout_width="match_parent"
  29.         android:layout_marginBottom="10dp"
  30.         android:layout_height="wrap_content" />
  31.  
  32.     <TextView
  33.         android:layout_width="wrap_content"
  34.         android:text="Lastname"
  35.         android:textColor="#fff"
  36.         android:layout_height="wrap_content" />
  37.  
  38.     <EditText
  39.         android:id="@+id/etLastName"
  40.         android:layout_width="match_parent"
  41.         android:layout_marginBottom="10dp"
  42.         android:layout_height="wrap_content" />
  43.  
  44.     <TextView
  45.         android:layout_width="wrap_content"
  46.         android:text="Email"
  47.         android:textColor="#fff"
  48.         android:layout_height="wrap_content" />
  49.  
  50.     <EditText
  51.         android:id="@+id/etEmail"
  52.         android:layout_width="match_parent"
  53.         android:layout_marginBottom="10dp"
  54.         android:layout_height="wrap_content" />
  55.  
  56.     <TextView
  57.         android:layout_width="wrap_content"
  58.         android:text="Username"
  59.         android:textColor="#fff"
  60.         android:layout_height="wrap_content" />
  61.  
  62.     <EditText
  63.         android:id="@+id/etUsername"
  64.         android:layout_width="match_parent"
  65.         android:layout_marginBottom="10dp"
  66.         android:layout_height="wrap_content" />
  67.  
  68.     <TextView
  69.         android:layout_width="wrap_content"
  70.         android:text="Password"
  71.         android:textColor="#fff"
  72.         android:layout_height="wrap_content" />
  73.  
  74.     <EditText
  75.         android:id="@+id/etPassword"
  76.         android:layout_width="match_parent"
  77.         android:inputType="textPassword"
  78.         android:layout_marginBottom="10dp"
  79.         android:layout_height="wrap_content" />
  80.  
  81.     <Button
  82.         android:id="@+id/bRegister"
  83.         android:text="REGISTER"
  84.         android:layout_width="match_parent"
  85.         android:layout_height="wrap_content" />
  86.  
  87.     <TextView
  88.         android:id="@+id/tvLogin"
  89.         android:layout_width="wrap_content"
  90.         android:text="Back To Login"
  91.         android:textStyle="bold"
  92.         android:textColor="#fff"
  93.         android:layout_gravity="center_horizontal"
  94.         android:layout_height="wrap_content" />
  95.  
  96. </LinearLayout>
Registration function sessions script to create a data of users.
  1. package com.example.rinvizle.myfirstapplication;
  2. public class Register extends AppCompatActivity implements View.OnClickListener {
  3.  
  4.     private Button bRegister;
  5.     private EditText etName, etLastname, etEmail, etUsername, etPassword;
  6.     private TextView tvLogin;
  7.     private DbHelper db;
  8.  
  9.     @Override
  10.     protected void onCreate(Bundle savedInstanceState) {
  11.         super.onCreate(savedInstanceState);
  12.         setContentView(R.layout.activity_register);
  13.  
  14.         db = new DbHelper(this);
  15.         etName = (EditText) findViewById(R.id.etName);
  16.         etLastname = (EditText) findViewById(R.id.etLastName);
  17.         etEmail = (EditText) findViewById(R.id.etEmail);
  18.         etUsername = (EditText) findViewById(R.id.etUsername);
  19.         etPassword = (EditText) findViewById(R.id.etPassword);
  20.         bRegister = (Button) findViewById(R.id.bRegister);
  21.         tvLogin = (TextView) findViewById(R.id.tvLogin);
  22.  
  23.         bRegister.setOnClickListener(this);
  24.         tvLogin.setOnClickListener(this);
  25.     }
  26.  
  27.     @Override
  28.     public void onClick(View v){
  29.         switch(v.getId()){
  30.             case R.id.bRegister:
  31.                 register();
  32.                 break;
  33.  
  34.             case R.id.tvLogin:
  35.                 startActivity(new Intent(Register.this, Login.class));
  36.                 finish();
  37.                 break;
  38.             default:
  39.         }
  40.     }
  41.  
  42.     private void register(){
  43.         String name = etName.getText().toString();
  44.         String lastname = etLastname.getText().toString();
  45.         String email = etEmail.getText().toString();
  46.         String username = etUsername.getText().toString();
  47.         String password = etPassword.getText().toString();
  48.         if(username.isEmpty() && password.isEmpty()){
  49.             displayToast("Username/Password Field Empty");
  50.         }
  51.         else {
  52.             db.addUser(name, lastname, email, username,password);
  53.             displayToast("User Registered");
  54.             finish();
  55.         }
  56.     }
  57.  
  58.     private void displayToast(String message){
  59.         Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();
  60.     }
  61. }
And for the session of the application from the login.
  1. package com.example.rinvizle.myfirstapplication;
  2. public class Session {
  3.     SharedPreferences prefs;
  4.     SharedPreferences.Editor editor;
  5.     Context ctx;
  6.  
  7.     public Session(Context ctx){
  8.         this.ctx = ctx;
  9.         prefs = ctx.getSharedPreferences("myfirstapp", Context.MODE_PRIVATE);
  10.         editor = prefs.edit();
  11.     }
  12.  
  13.     public void setLoggedin(boolean loggedin){
  14.         editor.putBoolean("loggedInmode", loggedin);
  15.         editor.commit();
  16.     }
  17.  
  18.     public boolean loggedin(){
  19.         return prefs.getBoolean("loggedInmode", false);
  20.     }
  21. }
For Creating a Database Table name Users
  1. package com.example.rinvizle.myfirstapplication;
  2. public class DbHelper extends SQLiteOpenHelper {
  3.     public static final String TAG = DbHelper.class.getSimpleName();
  4.     public static final String DB_NAME = "myfirstapp.db";
  5.     public static final int DB_VERSION = 1;
  6.  
  7.     public static final String USER_TABLE = "users";
  8.     public static final String COLUMN_ID = "_id";
  9.     public static final String COLUMN_NAME = "name";
  10.     public static final String COLUMN_LASTNAME = "lastname";
  11.     public static final String COLUMN_EMAIL = "email";
  12.     public static final String COLUMN_USERNAME = "username";
  13.     public static final String COLUMN_PASSWORD = "password";
  14.  
  15.     public static final String CREATE_TABLE_USERS = "CREATE TABLE " + USER_TABLE + "{"
  16.             + COLUMN_ID + " INTEGER PRIMARY INT AUTOINCREMENT,"
  17.             + COLUMN_NAME + " TEXT,"
  18.             + COLUMN_LASTNAME + " TEXT,"
  19.             + COLUMN_EMAIL + " TEXT,"
  20.             + COLUMN_USERNAME + " TEXT,"
  21.             + COLUMN_PASSWORD + " TEXT;";
  22.  
  23.     public DbHelper(Context context){
  24.         super(context, DB_NAME, null, DB_VERSION);
  25.     }
  26.  
  27.     @Override
  28.     public void onCreate(SQLiteDatabase db) {
  29.         db.execSQL(CREATE_TABLE_USERS);
  30.     }
  31.  
  32.     @Override
  33.     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  34.         db.execSQL("DROP TABLE IF EXIST " + USER_TABLE);
  35.         onCreate(db);
  36.     }
  37.  
  38.     public void addUser(String name, String lastname, String email, String username, String password){
  39.         SQLiteDatabase db = this.getWritableDatabase();
  40.  
  41.         ContentValues values = new ContentValues();
  42.         values.put(COLUMN_NAME, name);
  43.         values.put(COLUMN_LASTNAME, lastname);
  44.         values.put(COLUMN_EMAIL, email);
  45.         values.put(COLUMN_USERNAME, username);
  46.         values.put(COLUMN_PASSWORD, password);
  47.  
  48.         long id = db.insert(USER_TABLE, null, values);
  49.         db.close();
  50.  
  51.         Log.d(TAG, "User Added" + id);
  52.     }
  53.  
  54.     public boolean getUser(String username, String password){
  55.         String selectQuery = "select + from " + USER_TABLE + " where " +
  56.                 COLUMN_USERNAME + " = " + "'"+username+"'" + " and " + COLUMN_PASSWORD + " = " + "'"+password+"'";
  57.  
  58.         SQLiteDatabase db = this.getReadableDatabase();
  59.         Cursor cursor = db.rawQuery(selectQuery, null);
  60.         cursor.moveToFirst();
  61.         if (cursor.getCount() > 0){
  62.             return true;
  63.         }
  64.         cursor.close();
  65.         db.close();
  66.  
  67.         return false;
  68.     }
  69. }
And for more forms to be added just download the file from the download button below. Hope that you learn from this tutorial. Don't forget to Like and Share. Enjoy Coding!

Add new comment