Android Studio Login And Register Tutorial

package

net
.
simplifiedlearning
.
simplifiedcoding;

import


android
.
content
.
Intent;

import


android
.
os
.
AsyncTask;

import


android
.
os
.
Bundle;

import


android
.
support
.
v7
.
app
.
AppCompatActivity;

import


android
.
text
.
TextUtils;

import


android
.
view
.
View;

import


android
.
widget
.
EditText;

import


android
.
widget
.
ProgressBar;

import


android
.
widget
.
RadioButton;

import


android
.
widget
.
RadioGroup;

import


android
.
widget
.
Toast;

import


org
.
json
.
JSONException;

import


org
.
json
.
JSONObject;

import


java
.
util
.
HashMap;

public


class


MainActivity


extends


AppCompatActivity

{


EditText

editTextUsername,

editTextEmail,

editTextPassword;


RadioGroup

radioGroupGender;


@Override


protected

void

onCreate(Bundle

savedInstanceState)
{


super
.
onCreate(savedInstanceState);


setContentView(R
.
layout
.
activity_main);


//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;

}


//if it passes all the validations


class

RegisterUser

extends

AsyncTask
<
Void,

Void,

String
>

{


private

ProgressBar

progressBar;


@Override


protected

String

doInBackground(Void

voids)
{


//creating request handler object


RequestHandler

requestHandler

=

new

RequestHandler();


//creating request parameters


HashMap
<
String,

String
>


params

=

new

HashMap
<
>();


params
.
put(“username”,

username);


params
.
put(“email”,

email);


params
.
put(“password”,

password);


params
.
put(“gender”,

gender);


//returing the response


return

requestHandler
.
sendPostRequest(URLs
.
URL_REGISTER,

params);

}


@Override


protected

void

onPreExecute()
{


super
.
onPreExecute();


//displaying the progress bar while user registers on the peladen


progressBar

=
(ProgressBar)

findViewById(R
.
id
.
progressBar);


progressBar
.
setVisibility(View
.
VISIBLE);

}


@Override


protected

void

onPostExecute(String


s)
{


super
.
onPostExecute(s);


//hiding the progressbar after completion


progressBar
.
setVisibility(View
.
GONE);


try

{


//converting response to json object


JSONObject

obj

=

new

JSONObject(s);


//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”)

);


//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(),

“Some error occurred”,

Toast
.
LENGTH_SHORT).show();

}

}

catch

(JSONException


e)
{


e
.
printStackTrace();

}

}

}


//executing the async task


RegisterUser

ru

=

new

RegisterUser();


ru
.
execute();

}

}

Source: https://www.simplifiedcoding.net/android-login-and-registration-tutorial/