Create Login & Registration System In Android Studio By The Developer 2.0

How to Store data in MYSQL Database – PUBG APP – PART 2 – Registration Screen

Hello Guys this is the 2nd tutorial of making PUBG TOURNAMENT APP in Android Studio. Today in this Tutorial You can learn ,

  1. How to POST data to a URL
  2. How to Open another Screen on Button Click
  3. What is Constructor
  4. How to Store data in Mysql in PHP

Watch Tutorial Video Part 1 (Register):

How to Store data in MYSQL Database – PUBG APP – PART 2 – Registration Screen

Watch Tutorial Video Part 2 (Login):

Download register.php File:

Click on Below button and complete task showed in pop up to Download the File

Download login.php File:

Click on Below button and complete task showed in pop up to Download the File

All the Source Code are given Below..

File:  activity_signin.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".signin">

    <EditText
        android:id="@+id/username"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Enter User name"
        android:inputType="textPersonName"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.383" />

    <EditText
        android:id="@+id/password"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Enter password"
        android:inputType="textPassword"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/login"
        android:layout_width="210dp"
        android:layout_height="56dp"
        android:background="#00BFA5"
        android:onClick="doSignin"
        android:text="LOGIN"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.507"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.641" />

    <TextView
        android:id="@+id/signup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:onClick="goto_Signup"
        android:text="New user. Sign Up->"
        android:textColor="#00BFA5"
        android:textSize="24sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.542"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.844" />

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="190dp"
        android:layout_height="192dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.475"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0"
        app:srcCompat="@mipmap/ic_launcher_foreground"
        tools:ignore="VectorDrawableCompat" />
</android.support.constraint.ConstraintLayout>

File:  signin.java

package com.sanjay.pubgmaster;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class signin extends AppCompatActivity {
    EditText e_username, e_pass;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_signin);
        e_username = (EditText) findViewById(R.id.username);
        e_pass = (EditText) findViewById(R.id.password);
    }

    public void goto_Signup(View view) {
        startActivity(new Intent(signin.this, Signup.class));
    }

    public void doSignin(View view) {
        String method = "login";
        String username=e_username.getText().toString();
        String password=e_pass.getText().toString();

        if (username.matches("") || password.matches("") ) {
            Toast.makeText(getApplicationContext(), "Please fill all the filelds", Toast.LENGTH_SHORT).show();

        } else {

            Backgroundtask backgroundtask = new Backgroundtask(this);
            backgroundtask.execute(method,username,password);

        }
    }
}

File:  activity_signup.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Signup">

    <TextView
        android:id="@+id/textView"
        android:layout_width="222dp"
        android:layout_height="61dp"
        android:text="Registraion"
        android:textAlignment="center"
        android:textColor="#00BFA5"
        android:textSize="36sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.401"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.028" />

    <EditText
        android:id="@+id/user_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Enter PUBG ID"
        android:inputType="textPersonName"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.449"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.225" />

    <EditText
        android:id="@+id/referral"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Referral (Optional)"
        android:inputType="textPersonName"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.449"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.732" />

    <EditText
        android:id="@+id/email"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Enter Email"
        android:inputType="textEmailAddress"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.449"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.322" />

    <EditText
        android:id="@+id/phone"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Enter Phone Number"
        android:inputType="phone"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.449"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.426" />

    <EditText
        android:id="@+id/pass"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Pasword"
        android:inputType="textPassword"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.449"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.526" />

    <EditText
        android:id="@+id/con_pass"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Confirm Password"
        android:inputType="textPassword"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.449"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.63" />

    <EditText
        android:id="@+id/username"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Enter PUBG username"
        android:inputType="textPersonName"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.449"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.143" />

    <Button
        android:id="@+id/button"
        android:layout_width="215dp"
        android:layout_height="57dp"
        android:background="#00BFA5"
        android:onClick="doSignup"
        android:text="Register"
        android:textColor="#E1DEE4"
        android:textSize="18sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.877" />
</android.support.constraint.ConstraintLayout>

File:  Signup.java

package com.sanjay.pubgmaster;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class Signup extends AppCompatActivity {
    EditText et_username,et_userid,et_email,et_phone,et_pass,et_conpass,et_ref;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_signup);
        et_username=(EditText)findViewById(R.id.username);
        et_userid=(EditText)findViewById(R.id.user_id);
        et_email=(EditText)findViewById(R.id.email);
        et_phone = (EditText)findViewById(R.id.phone);
        et_pass=(EditText)findViewById(R.id.pass);
        et_conpass = (EditText)findViewById(R.id.con_pass);
        et_ref =(EditText)findViewById(R.id.referral);

    }
    public void doSignup(View v){
        String username = et_username.getText().toString();
        String pubg_id = et_userid.getText().toString();
        String email = et_email.getText().toString();
        String phone = et_phone.getText().toString();
        String pass = et_pass.getText().toString();
        String con_pass = et_conpass.getText().toString();
        String referral = et_ref.getText().toString();
        String method = "register";

        if(username.matches("") || pubg_id .matches("") || email.matches("") || phone.matches("") || pass.matches("") ||con_pass.matches("") )
        {
            Toast.makeText(getApplicationContext(),"Please fill all the fields",Toast.LENGTH_SHORT).show();
        }
        else if (!pass.equals(con_pass)) {
            Toast.makeText(getApplicationContext(),"Password are not same",Toast.LENGTH_SHORT).show();

        } else {

            Backgroundtask backgroundtask = new Backgroundtask(this);
            backgroundtask.execute(method,username,pubg_id,email,phone,pass,referral);

        }


    }
}

File:  Backgroundtask.java

package com.sanjay.pubgmaster;

import android.content.Context;
import android.os.AsyncTask;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;


public class Backgroundtask extends AsyncTask <String,Void,String> {
    Context ctx;

    Backgroundtask(Context ctx) {
        this.ctx = ctx;

    }

    @Override
    public String doInBackground(String... strings) {
        String reg_url = "http://quizeapp.000webhostapp.com/PUBG/register.php";
        String login_url = "http://quizeapp.000webhostapp.com/PUBG/login.php";
        String method = strings[0];
        if (method.equals("register")) {
            String username = strings[1];
            String pubg_id = strings[2];
            String email = strings[3];
            String phone = strings[4];
            String pass = strings[5];
            String referral = strings[6];

            try {
                URL url = new URL(reg_url);
                HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                httpURLConnection.setRequestMethod("POST");
                httpURLConnection.setDoOutput(true);
                OutputStream OS = httpURLConnection.getOutputStream();
                BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(OS, "UTF-8"));
                String data = URLEncoder.encode("user_name", "UTF-8") + "=" + URLEncoder.encode(username, "UTF-8") + "&" +
                        URLEncoder.encode("pubg_id", "UTF-8") + "=" + URLEncoder.encode(pubg_id, "UTF-8") + "&" +
                        URLEncoder.encode("email", "UTF-8") + "=" + URLEncoder.encode(email, "UTF-8") + "&" +
                        URLEncoder.encode("phone", "UTF-8") + "=" + URLEncoder.encode(phone, "UTF-8") + "&" +
                        URLEncoder.encode("pass", "UTF-8") + "=" + URLEncoder.encode(pass, "UTF-8") + "&" +
                        URLEncoder.encode("ref", "UTF-8") + "=" + URLEncoder.encode(referral, "UTF-8");
                bufferedWriter.write(data);
                bufferedWriter.flush();
                bufferedWriter.close();
                OS.close();
                InputStream IS = httpURLConnection.getInputStream();
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(IS));
                String Response = "";
                String line = "";
                while ((line = bufferedReader.readLine()) != null) {
                    Response += line;
                }
                bufferedReader.close();
                IS.close();
                //httpURLConnection.connect();
                httpURLConnection.disconnect();
                return Response;
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else if (method.equals("login")) {
            String l_username = strings[1];
            String l_password = strings[2];
            try {
                URL url = new URL(login_url);
                HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                httpURLConnection.setRequestMethod("POST");
                httpURLConnection.setDoOutput(true);
                OutputStream OS = httpURLConnection.getOutputStream();
                BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(OS, "UTF-8"));
                String data = URLEncoder.encode("user_name", "UTF-8") + "=" + URLEncoder.encode(l_username, "UTF-8") + "&" +
                        URLEncoder.encode("pass", "UTF-8") + "=" + URLEncoder.encode(l_password, "UTF-8");
                bufferedWriter.write(data);
                bufferedWriter.flush();
                bufferedWriter.close();
                OS.close();
                InputStream IS = httpURLConnection.getInputStream();
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(IS));
                String Response = "";
                String line = "";
                while ((line = bufferedReader.readLine()) != null) {
                    Response += line;
                }
                bufferedReader.close();

                IS.close();
                //httpURLConnection.connect();
                httpURLConnection.disconnect();
                return Response;
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return null;
    }


    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected void onProgressUpdate(Void... values) {
        super.onProgressUpdate(values);
    }

    @Override
    protected void onPostExecute(String result) {

            Toast.makeText(ctx, result, Toast.LENGTH_LONG).show();
    }

    }