Friday 7 November 2014

SignUp and login as a Parse User with Parse.com(Parse sdk)


SignUp And Login Screen

   Hello friends,

Today I am going to started new series of code of Parse.com(Parse sdk) .It is a new open source sdk ,used by many android ,iphone apps.

Today I am going to share the code to signup and login with the help of Parse.com.

Parse.com is mainly a package collection of many Parse classes,which are used for many different functionalities ,one of these parse classes is Parse User class .It is used to register any user with parse table .

For getting started with Parse sdk read the tutorialAndroid Quick Start Guide
For more info please read android tutorial of Parse.com 

Now let's move to the actual topic of this blog:

For create account ,I am only sharing the java class as the xml is very easy

SignUpActivity.java

import com.constants.Alert;
import com.constants.ConnectionDetector;
import com.constants.FunctionsValue;
import com.parse.ParseException;
import com.parse.ParseFile;
import com.parse.ParseUser;
import com.parse.SaveCallback;
import com.parse.SignUpCallback;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class SignUpActivity extends Activity{

EditText edttxt_uname,edttxt_email,edttxt_password,edttxt_phone;
String str_uname,str_email,str_password,str_phone;
Button btn_submit;
ConnectionDetector cd;
ProgressDialog dlg;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.signup_layout);
inItUi();
inItAction();
}

private void inItAction() {
btn_submit.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
str_email = edttxt_email.getText().toString();
str_uname = edttxt_uname.getText().toString();
str_password = edttxt_password.getText().toString();
str_phone = edttxt_phone.getText().toString();
if (str_uname.equals("")) {
Alert.alertOneBtn(SignUpActivity.this,"Empty","Please enter your name!");
}
else if (str_email.equals("")) {
Alert.alertOneBtn(SignUpActivity.this,"Empty","Please enter your email!");
}
else if (str_password.equals("")) {
Alert.alertOneBtn(SignUpActivity.this,"Empty","Please enter your password!");
}
else if (str_phone.equals("")) {
Alert.alertOneBtn(SignUpActivity.this,"Empty","Please enter your phone number!");
}
else if (!FunctionsValue.checkEmail(str_email)) {
Alert.alertOneBtn(SignUpActivity.this,"Empty","Email is not valid!");
}
else if(!cd.isConnectingToInternet())
{
Alert.alertOneBtn(SignUpActivity.this, "No Internet Connection",
"You don't have internet connection.");

}
else
{
if (Build.VERSION.SDK_INT >= 11 ) {
  dlg = new ProgressDialog(SignUpActivity.this,AlertDialog.THEME_HOLO_LIGHT );
     } else {
        dlg = new ProgressDialog(SignUpActivity.this);
       }
       dlg.setMessage("Registering...");
       dlg.show();
signUp(str_uname, str_email, str_password,str_phone);

}

}
});
}

protected void signUp(String name, String email,
String password, String str_phone2) {
final ParseUser user = new ParseUser();

user.setUsername(name);
user.setPassword(password);
user.setEmail(email);
user.put("phone", str_phone2);


user.signUpInBackground(new SignUpCallback() {

 public void done(ParseException e) {
 dlg.dismiss();
   if (e == null) {
     signUpMsg("Account Created Successfully");

     startActivity(new Intent(getApplicationContext(),LoginActivity.class));
   } else {
    
    Log.d("error",">>"+e);
    signUpMsg("Something went wrong.Please try again later!!");
   }
 }
});

}
protected void signUpMsg(String msg) {
Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
}

private void inItUi() {
cd = new ConnectionDetector(getApplicationContext());
edttxt_email = (EditText)findViewById(R.id.edttxt_email_signuplayout);
edttxt_password = (EditText)findViewById(R.id.edttxt_password_signuplayout);
edttxt_phone = (EditText)findViewById(R.id.edttxt_phone_signuplayout);
edttxt_uname = (EditText)findViewById(R.id.edttxt_username_signuplayout);
btn_submit = (Button)findViewById(R.id.btn_submit_signuplayout);
}


}


In above code I have used some classes for showing alert,detect connection and save value in shared preference,you can changed it according to the requirement.

LoginActivity.java:

import com.constants.Alert;
import com.constants.ConnectionDetector;
import com.constants.PreferenceSetting;
import com.parse.LogInCallback;
import com.parse.ParseException;
import com.parse.ParseUser;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class LoginActivity extends Activity{
EditText edttxt_username,edttxt_password;
String str_uname,str_password;
Button btn_submit;
ConnectionDetector cd;
ProgressDialog dlg;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.login_layout);
inItUi();
inItAction();
}
private void inItAction() {
btn_submit.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
str_uname = edttxt_username.getText().toString();
str_password = edttxt_password.getText().toString();
if (str_uname.equals("")) {
Alert.alertOneBtn(LoginActivity.this,"Empty","Please enter your username!");

}
else if (str_password.equals("")) {
Alert.alertOneBtn(LoginActivity.this,"Empty","Please enter your password!");

}
else
{
boolean isInternetPresent = cd.isConnectingToInternet();
// check for Internet status
if (isInternetPresent) {
if (Build.VERSION.SDK_INT >= 11 ) {
  dlg = new ProgressDialog(LoginActivity.this,AlertDialog.THEME_HOLO_LIGHT );
     } else {
        dlg = new ProgressDialog(LoginActivity.this);
       }
       dlg.setMessage("Please wait...");
       dlg.show();
attemptLogin(str_uname,str_password);
     
} else {
// Internet connection is not present
// Ask user to connect to Internet
Alert.alertOneBtn(LoginActivity.this, "No Internet Connection",
"You don't have internet connection.");
}

}

}
});
}
protected void attemptLogin(String str_email2, String str_password2) {
ParseUser.logInInBackground(str_email2, str_password2, new LogInCallback() {
@Override
public void done(ParseUser user, ParseException e) {
dlg.dismiss();
if(e == null)
{
Log.d(">>>",">>"+user.getObjectId()+">>>"+user.getUsername());
PreferenceSetting.SaveLoginInfo(LoginActivity.this,user.getObjectId(),usergetUsername());
startActivity(new Intent(getApplicationContext(),HomeTabActivity.class));
}
else
loginUnSuccessful();
}
});
}

protected void loginUnSuccessful() {
    Alert.alertOneBtn(LoginActivity.this,"Login", "Username or Password is invalid.");
edttxt_username.setText("");
edttxt_password.setText("");

}
private void inItUi() {
cd = new ConnectionDetector(getApplicationContext());
edttxt_password = (EditText)findViewById(R.id.edttxt_password_loginlayout);
edttxt_username = (EditText)findViewById(R.id.edttxt_username_loginlayout);
btn_submit = (Button)findViewById(R.id.btn_submit_loginlayout);

}
}


The above two classes are used for signup and login with parse sdk.I will share more code related to parse sdk in next blogs.If you faced any difficulty to understand this code ,please mention in comment.


No comments:

Post a Comment

Advanced Kotlin Coroutines : Introduction

 Hi,  Today I am unwraping the topic in Kotin world i.e. Coroutine . If you want to get started with Kotlin coroutine and ease your daily de...