Friday 30 December 2022

Android Login and Registration Tutorial with PHP MySQL

 In this android login and registration tutorial we will create a very simple application that will contain 3 screens. The User Registration Screen, User Login Screen and the User Profile Screen.

Here for the server side part we will be using PHP and MySQL with our XAMPP (you can use wamp or lamp as well) server.

So without wasting time lets start our Android Login and Registration tutorial. 

Android Login and Registration Video Tutorial

  • Feel boring reading long posts? More comfortable watching video tutorials? Here is a complete playlist for this post.
  • And if you are ok with text content then lets move ahead.

Creating Web Services

The first phase of this tutorial covers creating web services for User Signup and Login.  Now you may be thinking that why we need to create web services?

Why Web Services

The problem here is our application cannot access our server directly. We need some medium between server and the application so that our application can access the database residing in our web server. For this we use Web Services often called APIs, ore more specifically a RESTful API. Now here I am not discussing very much about Rest APIs or Web Services. You can check the below tutorials if you want to explore these things further.

  1. Build REST API using Laravel Lumen for your Application
  2. PHP Restful API Framework SLIM to Create REST API – 1

In this post we will not be using any framework to build our APIs. We will use the basic php only.

But for creating any kind of API you must know about JSON, and if you are completely unaware about JSON then you should go through this seven minute quick video first. Here you will understand everything about JSON.

Creating Database

  • We need to following database table.

database table

  • So first you create a new database in your PhpMyAdmin (localhost/phpmyadmin).
  • Inside the database we create the above shown table. And for this you can use the following query.

  • When you will execute the above given query in your database you will get the following table.
database structure
Database Table
  • Now we have our database table where we will store the user data. Now lets create a PHP Project for our Web Services.

Creating a new PHP Project

  • Go to the htdocs folder, and create a new folder there. (This folder is actually our project). You can use an IDE like PHP Storm to create a new project but remember create it inside htdocs only (c:/xampp/htdocs). Here I am using Sublime Text.
  • Now the first step is the database connection.

Connecting to the Database

  • So inside your project folder create a new php file named DbConnect.php. We will use this file to connect to the database. So write the following code inside.

  • To confirm that the above script is working, you should open it in your browser (localhost/YourProjectFolder/DbConnect.php). If you are seeing nothing in the browser that means it is working fine.

Building Web Services

  • Now here comes the main part. For the current application we need Web Services for two operation, the first one is User Registration and the second one is User Login.
  • So for this create a new php file named Api.php. Inside this file we will handle all the API calls. Currently we have only two the User Registration and User Login.
  • So write the following code inside Api.php.

Parameter Validations

  • We also need to validate that the required parameters are available or not. For this at the bottom in the same file (Api.php), create the following function.

User Registration

  • Now lets complete the user registration. So inside switch where we need to perform the registration write the following code.

  • Now you can test the registration using a REST Client (I am here using POSTMAN).

user registration

  • You see the user registration is working fine. Now lets create the User Login API.

User Login

  • After registration user will login to the application. So again inside switch where we need to define the login write the following code.

  • Now you can test the login part as well.

login api

The Complete Api Code

  • This is the complete code of my Api.php file.

  • So we are finished creating with APIs. Now lets move to android side. But before moving ahead if you need you can download my API code.

Android Login and Registration

Now here comes the android part. The first thing we will do on the android side is the register operation. So lets create a new Android Studio Project.

Creating a New Android Project

  • Now create an android project with Empty Activity. I created a project named SimplifiedCoding.
  • Once the project is completely loaded we will create two more activities in our project. As we will have 3 screens so first create all the 3 screens. The first screen is created by default we need to create the other two. For this right click on your package go to new -> activity -> EmptyActivity. Do this two times and create LoginActivity and ProfileActivity.
  • So finally we have MainActivity for registration, LoginActivity for login and ProfileActivity for the user profile that we will show after login.

Designing User Interface

  • First we will start designing all the screens. We have 3 Activities so lets start with designing the registration screen which is activity_main.xml (res->layout->activity_main.xml).

Registration Screen

  • For registration screen (activity_main.xml) we will design the screen as shown below.

android registration

  • To create the above UI you can use the xml code given below.

Login Screen

  • Now we will design the login screen (activity_login.xml) as below.

user login screen

  • And for the above design you can use the following xml code.

Profile Screen

  • Now lastly I have following for the profile screen.

profile screen

  • The layout is very simple and can be built using the following xml code.

  • Now we have done designing all the screens. Lets add functionalities now.

Creating Helper Classes

  • We will need some classes to organize our code. So lets create all the helper classes first.

User Model Class

  • First we will create a class to store our user. For this create a new class named user and write the following code.

SharedPreferences Manager Class

  • When the user registers or log in we will create a login session using SharedPreferences. So to make it simple we will create a separate class that will handle the SharedPreferences operations.
  • So create a new class named SharedPrefManager.java and write the following code.

Class to store Web Service URls

  • We also need to define the URLs that will be called for the user registration and login. Remember using localhost will not work. You need to identify the IP.
  • Make sure you are using the correct IP address and your device where you are testing the application can access your server. If your server is not accessible from your device then it will not work. 
  • Check this video from 6:00 to learn how you can find your IP and how you can use your real device to test your application. 
  • So create a class named URLs.java and write the following code.

  • Remember in the above code you need to change the IP according to your system.

Class for Sending http Request

  • As we need to send POST Request to our Web Service URL. So to make it simple lets create a separate class that will handle the Request.
  • So I am here creating a class named RequestHandler.java.

  • Now thats all for the helper classes. Lets code the app functionalities now.

Adding Internet Permission in the Manifest

  • As we are going to perform network operation. We have to add internet permission in the AndroidManifest.xml file.

  • You also need to add usesCleartextTraffic=”true” attribute in your manifest file.  We use this attribute when we want our app to communicate with http protocol. Because by default it blocks the protocols that are not https.

Completing the Registration Part

  • Now come inside MainActivity.java to finish the registration.

  • Now you can test the registration part it should work.

android user registration

  • So it is working fine. But we need to code the Profile Activity now.

Completing Profile Part

  • Now come inside ProfileActivity.java and write the following code.

  • Now run your application again.

user profile activity

  • You see now the profile is working fine. You can press on the logout button and it will log you out. Now the only thing remaining is the Login Screen.

Completing Login Part

  • Come inside LoginActivity.java and write the following code.

  • Now we have coded all the screen.You can test the login.

successful login

  • As you can see it is working absolutely fine.

Android Login and Registration Source Code Download

  • I tried my best to explain the things but if you are still having problems then you can get my source code from below. You need to pay me a like, share or tweet to get the source code 😛 (LOL). The source code contains the Android Studio Project, PHP Files and Database File.


No comments:

Post a Comment