C C++ Java Data Structure Python JavaScript CSS Ajax PL/SQL PHP Puzzles C programs C++ programs Java programs

Home » Android

Android - Signup and Sign in (login) and Sign out (logout) with codes

In this Android application, we are going to learn how to create an application to manage signup (registering a user), sign in (login) and logout (sign out)? This is a complete tutorial with source codes.
Submitted by Manu Jemini, on December 07, 2017

Roughly every business application has this technique to identify a particular user by processing his credential. While, the mainstream applications are using third-party signature to reduce the cost significantly, having a hand on this concept still proves to be a gold coin.

Saying so, we must understand its working and then cut the process in part two, learn them and in the end join them.

If you are doing a business of any kind you will most likely need to handle your costumers separately. This is easy said than done. This made you keep the track of every user through-out the application. This is very easy if you use preferences.

The data flow in this technique is similar to websites.

  1. For the first time user have to create his own unique credentials: USER → CREDENTIALS → APPLICATION → SERVER → DATABASE → STORED
  2. Now the user can Log In to your application, while you can keep all track of activities of every user separately. This gives you enormous amount of control over the services you can provide to a particular user: USER → CREDENTIALS → APPLICATION → SERVER → DATABASE → VERIFICATION → APPLICATION

Now trick is simple but awesome. While VERIFICATION, if the result is positive, put the user’s id in session, if not just say INVALID PASSWORD. I will how to do this programmatically in the next part.

The Structure of the code will include a dummy server. I will not be showing you how to use database.

Now when you will finish learning this article you will learn about INTENT and Text Views. I will not implement the server.

The Nature of your application will dictate the shape of sign-up space, not the technique. Most of the time you want to give a log-in activity in that login page a link will referring to a sign-up or create an account activity. Well its up-to you how you decides to do this.

Your Sign-Up page will make to get the required information from user. This is up-to you again on what you want from the user. But one golden rule to follow is to take a single unique attribute like E-mail or Mobile-Number so that you can identify that user with the help of this thing.

For this example, I am asking user to submit three of things.

  1. Name
  2. Email
  3. Password

Note: It makes it easy to understand. Name can be similar but E-mail cannot.

Signup (Registration form)

Java file:

package com.example.hp.demo;

import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


public class SignUp extends AppCompatActivity {
    EditText _email,_password,_name;
    Button _register;
    Context ctx;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ctx = this;
        _email = (EditText)findViewById(R.id._email);
        _password = (EditText)findViewById(R.id._password);
        _name = (EditText)findViewById(R.id._name);
        _register = (Button) findViewById(R.id._register_btn);
        _register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String _email_ = String.valueOf(_email.getText());
                String _password_ = String.valueOf(_password.getText());
                String _name_ = String.valueOf(_name.getText());
                Toast.makeText(ctx, _name_+" "+_email_+" "+_password_,Toast.LENGTH_LONG).show();
            }
        });


    }
    public void login(View v){
        Intent i = new Intent(ctx, SignIn.class);
        startActivity(i);
    }
}

XML file

<?xml version="1.0" encoding="utf-8"?>
<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"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="numeric.test.MainActivity">


    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textEmailAddress"
        android:ems="10"
        android:id="@+id/_email"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="83dp"
        android:hint="Enter Email" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPassword"
        android:ems="10"
        android:id="@+id/_password"
        android:hint="Enter Password"
        android:layout_below="@+id/_email"
        android:layout_alignStart="@+id/_email"
        android:layout_marginTop="39dp" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Login"
        android:id="@+id/_login_btn"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="55dp"
        android:onClick="login" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Register "
        android:id="@+id/_register_btn"
        android:onClick="register"
        android:layout_below="@+id/_password"
        android:layout_alignStart="@+id/_login_btn"
        android:layout_marginTop="47dp" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:ems="10"
        android:id="@+id/_name"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:hint="Enter Name" />

</RelativeLayout>

Manifest file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.hp.demo">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Output

signup form in android

Login (sing in) form

You can pass data through it with 'putExtras()' method. If you want to pass your own object or models as to as then you must make an additional class.

Java file:

package com.example.hp.demo;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


public class SignIn extends AppCompatActivity {
    EditText _email,_password;
    Button _login_btn;
    Context ctx;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ctx = this;
        _email = (EditText)findViewById(R.id._email);
        _password = (EditText)findViewById(R.id._password);
        _login_btn = (Button) findViewById(R.id._login_btn);
        _login_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String _email_ = String.valueOf(_email.getText());
                String _password_ = String.valueOf(_password.getText());
                Toast.makeText(ctx, _email_+" "+_password_,Toast.LENGTH_LONG).show();
            }
        });
        
        
    }
    public void register(View v){
        Intent i = new Intent(ctx, SignUp.class);
        startActivity(i);
    }
}

XML file:

<?xml version="1.0" encoding="utf-8"?>
<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"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="numeric.test.MainActivity">


    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textEmailAddress"
        android:ems="10"
        android:id="@+id/_email"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="83dp"
        android:hint="Enter Email" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPassword"
        android:ems="10"
        android:id="@+id/_password"
        android:hint="Enter Password"
        android:layout_below="@+id/_email"
        android:layout_alignStart="@+id/_email"
        android:layout_marginTop="39dp" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Login"
        android:id="@+id/_login_btn"
        android:layout_below="@+id/_password"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="62dp" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Register "
        android:id="@+id/_register_btn"
        android:layout_below="@+id/_login_btn"
        android:layout_alignStart="@+id/_login_btn"
        android:layout_marginTop="47dp"
        android:onClick="register" />

</RelativeLayout>

Manifest file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.hp.demo">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Output

signup form in android








COMMENTS