User Login and Registration Application In Andriod
Submitted by rinvizle on Tuesday, September 6, 2016 - 13:07.
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.
Script for the function of Login to create a session or start the activity through the main class form.
And for the Registration UI Design.
Registration function sessions script to create a data of users.
And for the session of the application from the login.
For Creating a Database Table name Users
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!
Sample Code
For the Login Form UI Design to be visible in the user.- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:orientation="vertical"
- android:layout_height="match_parent"
- android:padding="10dp"
- android:layout_width="match_parent"
- android:background="@color/colorPrimary">
- <TextView
- android:layout_width="wrap_content"
- android:text="HandyClass"
- android:layout_gravity="center_horizontal"
- android:textSize="30dp"
- android:padding="20dp"
- android:layout_marginTop="80dp"
- android:textColor="#fff"
- android:layout_height="wrap_content" />
- <TextView
- android:layout_width="wrap_content"
- android:text="Username"
- android:textColor="#fff"
- android:layout_height="wrap_content" />
- <EditText
- android:id="@+id/etUsername"
- android:layout_width="match_parent"
- android:layout_marginBottom="10dp"
- android:layout_height="wrap_content" />
- <TextView
- android:layout_width="wrap_content"
- android:text="Password"
- android:textColor="#fff"
- android:layout_height="wrap_content" />
- <EditText
- android:id="@+id/etPassword"
- android:layout_width="match_parent"
- android:inputType="textPassword"
- android:layout_marginBottom="10dp"
- android:layout_height="wrap_content" />
- android:id="@+id/bLogin"
- android:text="LOGIN"
- android:layout_marginBottom="10dp"
- android:layout_width="match_parent"
- android:layout_height="wrap_content" />
- <TextView
- android:id="@+id/tvRegisterLink"
- android:layout_width="wrap_content"
- android:text="Register Here"
- android:textStyle="bold"
- android:textColor="#fff"
- android:layout_gravity="center_horizontal"
- android:layout_height="wrap_content" />
- </LinearLayout>
- package com.example.rinvizle.myfirstapplication;
- private EditText etUsername, etPassword;
- private TextView tvRegisterLink;
- private DbHelper db;
- private Session session;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_login);
- db = new DbHelper(this);
- session = new Session(this);
- etUsername = (EditText) findViewById(R.id.etUsername);
- etPassword = (EditText) findViewById(R.id.etPassword);
- tvRegisterLink = (TextView) findViewById(R.id.tvRegisterLink);
- bLogin.setOnClickListener(this);
- tvRegisterLink.setOnClickListener(this);
- if(session.loggedin()){
- startActivity(new Intent(Login.this, MainActivity.class));
- finish();
- }
- }
- @Override
- switch (v.getId()){
- case R.id.bLogin:
- login();
- break;
- case R.id.tvRegisterLink:
- startActivity(new Intent(Login.this, Register.class));
- finish();
- break;
- default:
- }
- }
- private void login(){
- if (db.getUser(username,password)){
- startActivity(new Intent(Login.this, MainActivity.class));
- finish();
- }
- else{
- Toast.makeText(getApplicationContext(), "Wrong Username and Password",Toast.LENGTH_SHORT).show();
- }
- }
- }
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:orientation="vertical"
- android:layout_height="match_parent"
- android:padding="10dp"
- android:layout_width="match_parent"
- android:background="@color/colorPrimary">
- <TextView
- android:layout_width="wrap_content"
- android:text="HandyClass"
- android:layout_gravity="center_horizontal"
- android:textSize="30dp"
- android:padding="20dp"
- android:layout_marginTop="30dp"
- android:textColor="#fff"
- android:layout_height="wrap_content" />
- <TextView
- android:layout_width="wrap_content"
- android:text="Name"
- android:textColor="#fff"
- android:layout_height="wrap_content" />
- <EditText
- android:id="@+id/etName"
- android:layout_width="match_parent"
- android:layout_marginBottom="10dp"
- android:layout_height="wrap_content" />
- <TextView
- android:layout_width="wrap_content"
- android:text="Lastname"
- android:textColor="#fff"
- android:layout_height="wrap_content" />
- <EditText
- android:id="@+id/etLastName"
- android:layout_width="match_parent"
- android:layout_marginBottom="10dp"
- android:layout_height="wrap_content" />
- <TextView
- android:layout_width="wrap_content"
- android:text="Email"
- android:textColor="#fff"
- android:layout_height="wrap_content" />
- <EditText
- android:id="@+id/etEmail"
- android:layout_width="match_parent"
- android:layout_marginBottom="10dp"
- android:layout_height="wrap_content" />
- <TextView
- android:layout_width="wrap_content"
- android:text="Username"
- android:textColor="#fff"
- android:layout_height="wrap_content" />
- <EditText
- android:id="@+id/etUsername"
- android:layout_width="match_parent"
- android:layout_marginBottom="10dp"
- android:layout_height="wrap_content" />
- <TextView
- android:layout_width="wrap_content"
- android:text="Password"
- android:textColor="#fff"
- android:layout_height="wrap_content" />
- <EditText
- android:id="@+id/etPassword"
- android:layout_width="match_parent"
- android:inputType="textPassword"
- android:layout_marginBottom="10dp"
- android:layout_height="wrap_content" />
- android:id="@+id/bRegister"
- android:text="REGISTER"
- android:layout_width="match_parent"
- android:layout_height="wrap_content" />
- <TextView
- android:id="@+id/tvLogin"
- android:layout_width="wrap_content"
- android:text="Back To Login"
- android:textStyle="bold"
- android:textColor="#fff"
- android:layout_gravity="center_horizontal"
- android:layout_height="wrap_content" />
- </LinearLayout>
- package com.example.rinvizle.myfirstapplication;
- private EditText etName, etLastname, etEmail, etUsername, etPassword;
- private TextView tvLogin;
- private DbHelper db;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_register);
- db = new DbHelper(this);
- etName = (EditText) findViewById(R.id.etName);
- etLastname = (EditText) findViewById(R.id.etLastName);
- etEmail = (EditText) findViewById(R.id.etEmail);
- etUsername = (EditText) findViewById(R.id.etUsername);
- etPassword = (EditText) findViewById(R.id.etPassword);
- tvLogin = (TextView) findViewById(R.id.tvLogin);
- bRegister.setOnClickListener(this);
- tvLogin.setOnClickListener(this);
- }
- @Override
- switch(v.getId()){
- case R.id.bRegister:
- register();
- break;
- case R.id.tvLogin:
- startActivity(new Intent(Register.this, Login.class));
- finish();
- break;
- default:
- }
- }
- private void register(){
- if(username.isEmpty() && password.isEmpty()){
- displayToast("Username/Password Field Empty");
- }
- else {
- db.addUser(name, lastname, email, username,password);
- displayToast("User Registered");
- finish();
- }
- }
- Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();
- }
- }
- package com.example.rinvizle.myfirstapplication;
- public class Session {
- SharedPreferences prefs;
- SharedPreferences.Editor editor;
- Context ctx;
- this.ctx = ctx;
- editor = prefs.edit();
- }
- public void setLoggedin(boolean loggedin){
- editor.putBoolean("loggedInmode", loggedin);
- editor.commit();
- }
- public boolean loggedin(){
- return prefs.getBoolean("loggedInmode", false);
- }
- }
- package com.example.rinvizle.myfirstapplication;
- public class DbHelper extends SQLiteOpenHelper {
- public static final int DB_VERSION = 1;
- + COLUMN_ID + " INTEGER PRIMARY INT AUTOINCREMENT,"
- + COLUMN_NAME + " TEXT,"
- + COLUMN_LASTNAME + " TEXT,"
- + COLUMN_EMAIL + " TEXT,"
- + COLUMN_USERNAME + " TEXT,"
- + COLUMN_PASSWORD + " TEXT;";
- super(context, DB_NAME, null, DB_VERSION);
- }
- @Override
- public void onCreate(SQLiteDatabase db) {
- db.execSQL(CREATE_TABLE_USERS);
- }
- @Override
- public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
- db.execSQL("DROP TABLE IF EXIST " + USER_TABLE);
- onCreate(db);
- }
- SQLiteDatabase db = this.getWritableDatabase();
- ContentValues values = new ContentValues();
- values.put(COLUMN_NAME, name);
- values.put(COLUMN_LASTNAME, lastname);
- values.put(COLUMN_EMAIL, email);
- values.put(COLUMN_USERNAME, username);
- values.put(COLUMN_PASSWORD, password);
- long id = db.insert(USER_TABLE, null, values);
- db.close();
- Log.d(TAG, "User Added" + id);
- }
- COLUMN_USERNAME + " = " + "'"+username+"'" + " and " + COLUMN_PASSWORD + " = " + "'"+password+"'";
- SQLiteDatabase db = this.getReadableDatabase();
- cursor.moveToFirst();
- if (cursor.getCount() > 0){
- return true;
- }
- cursor.close();
- db.close();
- return false;
- }
- }
Add new comment
- 1290 views