Welcome to this Android tutorial. In this tutorial, I will show you how to create a login and register application in Android. This is vital in every application where the user is required to create an account and login to the app. We will store our data in MySQL database. To interact with the database, we will use a server-side language called PHP.

I hope you have XAMPP installed on your laptop to run PHP scripts. If not, please google how to install and use XAMPP. You will get XAMPP from Apachefriends.org

Once your server is up and running, we will now create a simple PHP API to handle data from our Login and Registration Android App. Data transmission will be done in JSON format using volley Android. Volley is an HTTP library that makes networking for Android apps easier and faster.

Without much further ado, let’s start coding the login and register app.

Database Setup

First things first, let's create our database schema. Create a database with the name login_register. Then create a table with name users.

The above code creates a table called users. The first column is the primary key and will increment automatically. Then we have firstname and lastname columns to store the name is the user. We have a unique username column, and this means that two users cannot have the same username in our table. We also have an email column, which is also unique, and we shall store users' emails so that we can send emails to them. Then we have a password column, which will be hashed using the MD5() algorithm.

PHP scripts

We need PHP scripts to handle login and registration from Android app. We now create three scripts to handle this.

First script: Database Connection

Open your favorite IDE and create a PHP project and then create a file called db.php.

We shall open a connection to our database using PHP PDO. For installation, details read PHP PDO.

Third Script: Login

After a user has been registered in the database, we can now try to log in. Now create a new file called login.php. We shall use the same database connection script, db.php, to read users from the database, and authenticate them.

4.Next, we shall create a class to store user details in shared preferences. This class will help us maintain the login session. Many available login and register tutorials do not show how to manage the login session, so I found it important to show you in this article.

A SharedPreferences object points to a file containing key-value pairs and provides simple methods to read and write them. So in our case, we shall store key-value pairs for user information.

So create a class called SharedPrefrencesHelper.java and paste the following code:

The above class has four fields to store the first name, last name, username, and email of the user. After a successful login, user data will be retrieved from the database by PHP code, and this class will store this data using the appropriate setter methods. When the user logs out, we shall simply remove data from shared preferences. We shall use getter methods to retrieve information from shared preferences.

Android Main Activity

Let's now create mainActivity.java and activity_main.xml files. When the user launches the app, it will first check if the user is logged in by reading information from the shared preferences. If there is data, then the user is logged, and if there is no data, then the user is not logged and must be directed to the login activity.

In strings.xml, we have defined a parameter called URL, which holds the base URL of our PHP project. When using the volley library, we send a string request. This request contains method type, ie, either GET or POST, URL (endpoint or of PHP login and register API), and data that will be processed by PHP.

The user will first fill all the provided fields and click the register button. When the button is clicked, registerAction() method is executed. Here we check if all fields are filled before posting the values to PHP. This is known as client-side validation.

If all fields are filled, we use StringRequest class to send the data to PHP. When data is sent through HTTP call using volley and JSON response is received, the compiler executes onResponse() method. This is where we get the response and read the data returned from our PHP register code. Here we run a very important line, rQueue.getCache().clear(); It will prevent volley from making HTTP calls more than one time. So ensure you have this line here.

If the client(Android) and server (PHP) communicate without errors, we expect JSON response with a key (success) with value "0" or "1". If the value of success is "1," then the registration was successful, and we direct the user to login activity. If the value is "0", it means there was an error, and we display the message to the user.

If an error occurs during the communication, the compiler runs onError() method and it tells us the kind of the error.

We have two fields, one for the username or email and another one for password. Then we have a button and a link for new users to register into the system.

When a user clicks the button, the app checks whether the username or email and password are filled. Then make HTTP call using volley to verify and authenticate this user.

When the HTTP call is made, and the JSON response is returned, the onRespsonse() method is executed. Kindly don't forget to clear the cache after the response by using rQueue.getCache().clear(); On successful response, we expect the JSON object to have a key (success) with either "0" or "1". If the value is "0," then the user gave wrong details. He must try to login with new credentials.

If the value is "1", the details were corrected, and the login was a success, so the server returned user's details as they are in the database. Here we read these values and store them in shared preferences using the following lines of code.