FullStack Android Tutorial – Android Login and Registration Tutorial with PHP MySQL

GameTutorialCenter.com
To learn software development, you must first understand the foundation of Programming Language before you can start learning software development.
Want to understand (OOD) Object Oriented Design Concept should know Java more

Java is a very popular Programming Language in banking, finance and financial technology industries because it provides security and is very useful for building overloaded programs that can handle large amounts of data.
How many Android phones are there? 2.5 billion !!!! How many Android apps are there? 2.9 million !!!!
Java is the most popular Programming Language today, and most Android mobile applications are developed in Java
The average monthly salary of Java Programmer and Android Developer is about
Master the most popular technologies and equip enough knowledge to stay competitive in the rapidly changing technology market.
The first step in programming, whether it is mobile phone development or artificial intelligence programs, the establishment of a website Programming is a technology that must be mastered.
People who want to know the concept of (OOD) Object Oriented Design
Suitable for anyone interested in joining the IT industry. Suitable for parents to understand the progress of children. Suitable for project managers to understand and communicate with IT people

Use real application code examples to teach Java programming, Android mobile phone programs The most basic Object, Register, Login

 

What is Java

 

public class MyClass {
public static void main(String[] args) {
System.out.println("Hello World");
}
}

Creating Web Services

Why Web Services

Our Android app need the PHP API web service that  between server and the application, so the app can access the database. 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.

 

Creating Database

  • We need to following database table.

CREATE TABLE `users` (
`id` int(11) NOT NULL,
`username` varchar(200) NOT NULL,
`email` varchar(200) NOT NULL,
`password` text NOT NULL,
`password_str` varchar(100) DEFAULT NULL,
`first_name` varchar(50) DEFAULT NULL,
`last_name` varchar(50) DEFAULT NULL,
`description` varchar(100) DEFAULT NULL,
`dateofbirth` date NOT NULL,
`gender` varchar(6) NOT NULL,
`user_counter` int(20) NOT NULL DEFAULT '0',
`admin` int(10) NOT NULL DEFAULT '0',
`trainer` int(10) NOT NULL DEFAULT '0',
`registerdate` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Connecting to the Database

  • we need to create a database connection file named DbConnect.php. We will use this file to connect to the database. So write the following code inside.

 


<?php
$servername = "localhost";
$username = "username";
$password = "password";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>

Building PHP API Web Services

  • 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.

 

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.

 

Creating a New Android Project

  • Now create an android project with Empty Activity. I created a project named AndroidTutorial.
  • Once the project is 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.

 


package com.myclassstudio.album;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import android.content.Intent;
import android.view.View;
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.Toast;
import android.widget.ProgressBar;
import android.widget.RadioButton;
import android.widget.RadioGroup;

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

import com.android.volley.AuthFailureError;
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.JsonObjectRequest;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONObject;
import org.json.JSONException;

public class MainActivity extends AppCompatActivity {

EditText editTextUsername, editTextEmail, editTextPassword;
RadioGroup radioGroupGender;
ProgressBar progressBar;

public static final String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";

RequestQueue queue;
JsonObjectRequest request;
Map<String, String> map = new HashMap<String, String>();

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

progressBar = (ProgressBar) findViewById(R.id.progressBar);

//if the user is already logged in we will directly start the profile activity
if (SharedPrefManager.getInstance(this).isLoggedIn()) {
finish();
startActivity(new Intent(this, ProfileActivity.class));
return;
}

editTextUsername = (EditText) findViewById(R.id.editTextUsername);
editTextEmail = (EditText) findViewById(R.id.editTextEmail);
editTextPassword = (EditText) findViewById(R.id.editTextPassword);
radioGroupGender = (RadioGroup) findViewById(R.id.radioGender);

findViewById(R.id.buttonRegister).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//if user pressed on button register
//here we will register the user to server
registerUser();
}
});

findViewById(R.id.textViewLogin).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//if user pressed on login
//we will open the login screen
finish();
startActivity(new Intent(MainActivity.this, LoginActivity.class));
}
});
}

private void registerUser() {
final String username = editTextUsername.getText().toString().trim();
final String email = editTextEmail.getText().toString().trim();
final String password = editTextPassword.getText().toString().trim();

final String gender = ((RadioButton) findViewById(radioGroupGender.getCheckedRadioButtonId())).getText().toString();

//first we will do the validations

if (TextUtils.isEmpty(username)) {
editTextUsername.setError("Please enter username");
editTextUsername.requestFocus();
return;
}

if (TextUtils.isEmpty(email)) {
editTextEmail.setError("Please enter your email");
editTextEmail.requestFocus();
return;
}

if (!android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches()) {
editTextEmail.setError("Enter a valid email");
editTextEmail.requestFocus();
return;
}

if (TextUtils.isEmpty(password)) {
editTextPassword.setError("Enter a password");
editTextPassword.requestFocus();
return;
}

StringRequest stringRequest = new StringRequest(Request.Method.POST, URLs.URL_REGISTER,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
progressBar.setVisibility(View.GONE);

try {
//converting response to json object
JSONObject obj = new JSONObject(response);

//if no error in response
if (!obj.getBoolean("error")) {
Toast.makeText(getApplicationContext(), obj.getString("message"), Toast.LENGTH_SHORT).show();

//getting the user from the response
JSONObject userJson = obj.getJSONObject("user");

//creating a new user object
User user = new User(
userJson.getInt("id"),
userJson.getString("username"),
userJson.getString("email"),
userJson.getString("gender"),
userJson.getInt("user_counter")
);

//storing the user in shared preferences
SharedPrefManager.getInstance(getApplicationContext()).userLogin(user);

//starting the profile activity
finish();
startActivity(new Intent(getApplicationContext(), ProfileActivity.class));
} else {
Toast.makeText(getApplicationContext(), obj.getString("message"), Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_SHORT).show();
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("username", username);
params.put("email", email);
params.put("password", password);
params.put("gender", gender);
return params;
}
};

VolleySingleton.getInstance(this).addToRequestQueue(stringRequest);

}

/** Called when the user taps the Send button */
public void sendMessage(View view) {

// the request queue
queue = Volley.newRequestQueue(this);

// the parameters for the php
// map.put(KEY, VALUE);
map.put("name", "test");

// the JSON request
// JsonObjectRequest(METHOD, URL, JSONOBJECT(PARAMETERS), OK_LISTENER, ERROR_LISTENER);
request = new JsonObjectRequest(
Request.Method.POST, // the request method
"http://myclassstudio.com/album/postjoson.php", // the URL
new JSONObject(map), // the parameters for the php
new Response.Listener<JSONObject>() { // the response listener
@Override
public void onResponse(JSONObject response){
// here you parse the json response
}
},
new Response.ErrorListener() { // the error listener
@Override
public void onErrorResponse(VolleyError error) {

/* here you can warn the user that there
was an error while trying to get the json
information from the php */
}
});

// executing the quere to get the json information
queue.add(request);

}

}

 

 

 

Completing Login Part

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

Android Login and Registration Source Code Download

  • You can get my source code from below. You need to pay me a like, share or tweet to get the source code. The source code contains the Android Studio Project, PHP Files and Database File.

US$156

Buy and Download Full Tutorial and Source Code

 
GameTutorialCenter.com

Yoga Booking APP Source Code

Yoga Booking APP Source Code


FullStack Android Tutorial

jobboard


Learning Java – Get a job as Android Developer

jobboard


Be the first to comment

Leave a Reply