0

Android Volley User Registration using PHP MySQL Example Tutorial

Android create user Sign Up form with Volley post request method and send EditText data to MySQL server.

Our in this tutorial we would going to create simple user Registration or Sign Up form using various EditText. We would transfer the EditText source value entered by User through android application straight to our online hosting server using HTTP URL. On server we have created a PHP script that would obtain the sent value and insert that data into MySQL database table.

There is a special quality in this script if user email entered by User is already exists on server then it would Echo a message and as a output response we would show that Echo message in our app using Toast message. That message would tell us whether the email is already exists or not.

If the Email entered by user is not present in MySQL table then it would show a message that ” User Registration Successfully ” . If the email is already exist the it would show message ” Email Already Exist, Please Enter Another Email. ” in app .

Follow the below steps for Android Volley User Registration :-

1. Create a database in PhpMyAdmin.

2. Create a new table in database name as User_Details_Table with four columns id, User_Email, User_Password, User_Full_Name. Set id as primary key.

 

3. Upload the below PHP script with your database configuration. In DatabaseConfig.php file you have to set your server PhpMyAdmin HostName, Host Password, Host User, Database Name.

Code for DatabaseConfig.php file.

<?php

//Define your host here.
$HostName = "localhost";

//Define your database username here.
$HostUser = "id302300_android_examples_data_user";

//Define your database password here.
$HostPass = "1234567890";

//Define your database name here.
$DatabaseName = "id302300_android_examples";

?>

Code for User-Registration.php file.

 <?php
if($_SERVER['REQUEST_METHOD']=='POST'){

include 'DatabaseConfig.php';

 $con = mysqli_connect($HostName,$HostUser,$HostPass,$DatabaseName);

 $Email = $_POST['User_Email'];
 
 $Password = $_POST['User_Password'];
 
 $Full_Name = $_POST['User_Full_Name'];

 $CheckSQL = "SELECT * FROM User_Details_Table WHERE User_Email='$Email'";
 
 $check = mysqli_fetch_array(mysqli_query($con,$CheckSQL));
 
 if(isset($check)){

 echo 'Email Already Exist, Please Enter Another Email.';

 }
else{ 
$Sql_Query = "INSERT INTO User_Details_Table (User_Email,User_Password,User_Full_Name) values ('$Email','$Password','$Full_Name')";

 if(mysqli_query($con,$Sql_Query))
{
 echo 'User Registration Successfully';
}
else
{
 echo 'Something went wrong';
 }
 }
}
 mysqli_close($con);
?>

 

4. Start a new android application development project.

5. Add internet permission in AndroidManifest.xml file.

<uses-permission android:name="android.permission.INTERNET"/>

 

6. Add compile ‘com.android.volley:volley:1.0.0’ library file in build.gradle(Module:app) file. So open it.

build-gradle

 

7. Add compile ‘com.android.volley:volley:1.0.0’ inside dependencies block.

 

8. Create Three EditText and one button in activity layout file.

<EditText
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/EditTextFullName"
    android:layout_below="@+id/textView"
    android:layout_marginTop="20dp"
    android:hint="Enter Your Full Name"
    android:gravity="center"/>

<EditText
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/EditTextEmail"
    android:layout_below="@+id/EditTextFullName"
    android:layout_marginTop="20dp"
    android:inputType="textEmailAddress"
    android:hint="Enter Your Email"
    android:gravity="center"/>

<EditText
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/EditTextPassword"
    android:layout_below="@+id/EditTextEmail"
    android:layout_marginTop="20dp"
    android:inputType="textPassword"
    android:hint="Enter Your Password"
    android:gravity="center"/>

<Button
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Click Here To Register"
    android:layout_below="@+id/EditTextPassword"
    android:id="@+id/ButtonRegister"
    android:layout_marginTop="20dp"/>

 

9. Create button, EditText, RequestQueue, String,Boolean, ProgressDialog and HttpUrl , button objects in MainActivity.java file.

// Creating EditText.
EditText FullName, Email, Password ;

// Creating button;
Button Register;

// Creating Volley RequestQueue.
RequestQueue requestQueue;

// Create string variable to hold the EditText Value.
String NameHolder, EmailHolder, PasswordHolder ;

// Creating Progress dialog.
ProgressDialog progressDialog;

// Storing server url into String variable.
String HttpUrl = "https://android-examples.000webhostapp.com/User-Registration.php";

Boolean CheckEditText ;

 

10. Assign ID’s to all of them.

// Assigning ID's to EditText.
FullName = (EditText) findViewById(R.id.EditTextFullName);

Email = (EditText) findViewById(R.id.EditTextEmail);

Password = (EditText) findViewById(R.id.EditTextPassword);

// Assigning ID's to Button.
Register = (Button) findViewById(R.id.ButtonRegister);

// Creating Volley newRequestQueue .
requestQueue = Volley.newRequestQueue(MainActivity.this);

// Assigning Activity this to progress dialog.
progressDialog = new ProgressDialog(MainActivity.this);

 

11. Creating method to check EditText is empty or not .

public void CheckEditTextIsEmptyOrNot(){

    // Getting values from EditText.
    NameHolder = FullName.getText().toString().trim();
    EmailHolder = Email.getText().toString().trim();
    PasswordHolder = Password.getText().toString().trim();

    // Checking whether EditText value is empty or not.
    if(TextUtils.isEmpty(NameHolder) || TextUtils.isEmpty(EmailHolder) || TextUtils.isEmpty(PasswordHolder))
    {

        // If any of EditText is empty then set variable value as False.
        CheckEditText = false;

    }
    else {

        // If any of EditText is filled then set variable value as True.
        CheckEditText = true ;
    }
}

 

12. Adding click listener to button.

Register.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {

    }
});

 

13. Creating UserRegistration() method.

public void UserRegistration(){

    // Showing progress dialog at user registration time.
    progressDialog.setMessage("Please Wait, We are Inserting Your Data on Server");
    progressDialog.show();

    // Creating string request with post method.
    StringRequest stringRequest = new StringRequest(Request.Method.POST, HttpUrl,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String ServerResponse) {

                    // Hiding the progress dialog after all task complete.
                    progressDialog.dismiss();

                    // Showing Echo Response Message Coming From Server.
                    Toast.makeText(MainActivity.this, ServerResponse, Toast.LENGTH_LONG).show();
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError volleyError) {

                    // Hiding the progress dialog after all task complete.
                    progressDialog.dismiss();

                    // Showing error message if something goes wrong.
                    Toast.makeText(MainActivity.this, volleyError.toString(), Toast.LENGTH_LONG).show();
                }
            }) {
        @Override
        protected Map<String, String> getParams() {

            // Creating Map String Params.
            Map<String, String> params = new HashMap<String, String>();

            // Adding All values to Params.
            // The firs argument should be same sa your MySQL database table columns.
            params.put("User_Email", EmailHolder);
            params.put("User_Password", PasswordHolder);
            params.put("User_Full_Name", NameHolder);

            return params;
        }

    };

}

 

14. Calling RequestQueue to call the stringRequest method in UserRegistration() function.

// Creating RequestQueue.
RequestQueue requestQueue = Volley.newRequestQueue(MainActivity.this);

// Adding the StringRequest object into requestQueue.
requestQueue.add(stringRequest);

 

15. Calling UserRegistration() and CheckEditTextIsEmptyOrNot() method inside the button click scope with IF else condition So if the EditText is empty then it simply shows a toast message on screen.

// Adding click listener to button.
Register.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {

        CheckEditTextIsEmptyOrNot();

        if(CheckEditText){

            UserRegistration();

        }
        else {

            Toast.makeText(MainActivity.this, "Please fill all form fields.", Toast.LENGTH_LONG).show();

        }

    }
});

 Complete source code :

Code for MainActivity.java file.

package com.android_examples.volleyuserregistration_android_examplescom;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Toast;
import android.widget.EditText;
import android.widget.Button;

import java.util.HashMap;
import java.util.Map;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

public class MainActivity extends AppCompatActivity {

    // Creating EditText.
    EditText FullName, Email, Password ;

    // Creating button;
    Button Register;

    // Creating Volley RequestQueue.
    RequestQueue requestQueue;

    // Create string variable to hold the EditText Value.
    String NameHolder, EmailHolder, PasswordHolder ;

    // Creating Progress dialog.
    ProgressDialog progressDialog;

    // Storing server url into String variable.
    String HttpUrl = "https://android-examples.000webhostapp.com/User-Registration.php";

    Boolean CheckEditText ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Assigning ID's to EditText.
        FullName = (EditText) findViewById(R.id.EditTextFullName);

        Email = (EditText) findViewById(R.id.EditTextEmail);

        Password = (EditText) findViewById(R.id.EditTextPassword);

        // Assigning ID's to Button.
        Register = (Button) findViewById(R.id.ButtonRegister);

        // Creating Volley newRequestQueue .
        requestQueue = Volley.newRequestQueue(MainActivity.this);

        // Assigning Activity this to progress dialog.
        progressDialog = new ProgressDialog(MainActivity.this);

        // Adding click listener to button.
        Register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                CheckEditTextIsEmptyOrNot();

                if(CheckEditText){

                    UserRegistration();

                }
                else {

                    Toast.makeText(MainActivity.this, "Please fill all form fields.", Toast.LENGTH_LONG).show();

                }

            }
        });

    }

    public void UserRegistration(){

        // Showing progress dialog at user registration time.
        progressDialog.setMessage("Please Wait, We are Inserting Your Data on Server");
        progressDialog.show();

        // Creating string request with post method.
        StringRequest stringRequest = new StringRequest(Request.Method.POST, HttpUrl,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String ServerResponse) {

                        // Hiding the progress dialog after all task complete.
                        progressDialog.dismiss();

                        // Showing Echo Response Message Coming From Server.
                        Toast.makeText(MainActivity.this, ServerResponse, Toast.LENGTH_LONG).show();
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {

                        // Hiding the progress dialog after all task complete.
                        progressDialog.dismiss();

                        // Showing error message if something goes wrong.
                        Toast.makeText(MainActivity.this, volleyError.toString(), Toast.LENGTH_LONG).show();
                    }
                }) {
            @Override
            protected Map<String, String> getParams() {

                // Creating Map String Params.
                Map<String, String> params = new HashMap<String, String>();

                // Adding All values to Params.
                // The firs argument should be same sa your MySQL database table columns.
                params.put("User_Email", EmailHolder);
                params.put("User_Password", PasswordHolder);
                params.put("User_Full_Name", NameHolder);

                return params;
            }

        };

        // Creating RequestQueue.
        RequestQueue requestQueue = Volley.newRequestQueue(MainActivity.this);

        // Adding the StringRequest object into requestQueue.
        requestQueue.add(stringRequest);

    }


    public void CheckEditTextIsEmptyOrNot(){

        // Getting values from EditText.
        NameHolder = FullName.getText().toString().trim();
        EmailHolder = Email.getText().toString().trim();
        PasswordHolder = Password.getText().toString().trim();

        // Checking whether EditText value is empty or not.
        if(TextUtils.isEmpty(NameHolder) || TextUtils.isEmpty(EmailHolder) || TextUtils.isEmpty(PasswordHolder))
        {

            // If any of EditText is empty then set variable value as False.
            CheckEditText = false;

        }
        else {

            // If any of EditText is filled then set variable value as True.
            CheckEditText = true ;
        }
    }

}

Code for activity_main.xml layout 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"
    android:layout_margin="20dp"
    tools:context="com.android_examples.volleyuserregistration_android_examplescom.MainActivity">


<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Volley User Registration Form"
    android:textSize="20dp"
    android:textStyle="bold"
    android:id="@+id/textView"
    android:layout_centerHorizontal="true"
    android:gravity="center"/>

    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/EditTextFullName"
        android:layout_below="@+id/textView"
        android:layout_marginTop="20dp"
        android:hint="Enter Your Full Name"
        android:gravity="center"/>

    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/EditTextEmail"
        android:layout_below="@+id/EditTextFullName"
        android:layout_marginTop="20dp"
        android:inputType="textEmailAddress"
        android:hint="Enter Your Email"
        android:gravity="center"/>

    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/EditTextPassword"
        android:layout_below="@+id/EditTextEmail"
        android:layout_marginTop="20dp"
        android:inputType="textPassword"
        android:hint="Enter Your Password"
        android:gravity="center"/>

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Click Here To Register"
        android:layout_below="@+id/EditTextPassword"
        android:id="@+id/ButtonRegister"
        android:layout_marginTop="20dp"/>

</RelativeLayout>

Code for AndroidManifest.xml file.

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

    <uses-permission android:name="android.permission.INTERNET"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        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>

Screenshots :

Registration

Download Source Code

Leave a Reply

Your email address will not be published. Required fields are marked *