Most of the Android applications need to manage users. The first thing most of the apps ask during the launch is to either login or register as a member. App developers make use of back-end servers and remote databases to store user data so that all user details can be accessed from one place and data can be synced with the application, whenever the user logs in.

In this tutorial, I will discuss on how we can implement simple login and registration in Android. I will make use of PHP for backend scripting and MySQL database for storing the data.

In my previous article on Connecting Android App to Remote Database, I have explained in detail about connecting to a remote database using  PHP and MySQL. You can go through the article to have a basic understanding of how to set up WAMP server and creating the database.

Creating MySQL Database and Table

1. Login to phpMyAdmin (http://localhost/phpmyadmin/) in your browser and create a new database named androiddeft (if not present already).

2. Run the below SQL to create the table named member:

Scripting Backend PHP

1. Open www folder of WAMP server and create a new folder named member. This folder will house all the PHP files used in this tutorial.

2. Now create a sub-folder named db inside member folder.

3. Create a PHP file called db_connect.php and add the following code. This will help in establishing the connection to the MySQL database. You may update database details accordingly.

4. Create a file named functions.php inside the member folder. This contains functions for checking whether the user exists not, functions for generating password salt and hashes. Here I have made use of random salt generator so that a unique salt and hash is generated for each user even if 2 people share the same password. This adds an additional level of security and prevents hackers from making use of reverse lookup tables.

5. Create another PHP file called register.php. This takes username, password and full name as parameters checks whether the username is already taken. If not, it registers the user and prints a success response. If any errors occur, then it sets it in the response.

6. Finally create a file for login, namely login.php. Here we compare the stored hash with the hash generated from the password entered by the user using the stored salt. If password matches then we set a success response and a failure response otherwise.

Creating Android Project

Now we will see how can we make use of the PHP login and register API’s we have created. We will be making use of Volley library for performing network calls.

1. Create a project in Android Studio with the name Login and Registration. Name the activity as LoginActivity.

2. Add Volley dependency to build.gradle file of app module:

3. Open AndroidManifest.xml and Internet permission:

4. Create a POJO class named User to hold the user details:

5. Update the string resources to be used in this project in string.xml:

6. Create a class named SessionHandler and update it with the following code. This helps in handling user sessions by performing actions like logging in the user, fetching user details, logging out user etc. Here I have made use of shared preferences to maintain the user session. Also here we have a method to check if the user is already logged in so that he can be redirected to dashboard screen.

I have set a session expiry time of 7 days so that user gets logged out after 7 days of login. You can update this value based on your need.

7. Create a new XML file inside the drawable folder with the name ic_lock_outline_white.xml and update it with the following SVG code. This is a vector graphics icon used to display lock symbol in the login screen

8. Update the activity_login.xml (this is the activity_main.xml file generated while creating the project) file with the below code: