Signup tutorials for the beginners. In this tutorial you can see different types of signup pages in which date of birth is included , Signup using Facebook and other options as well for android. You can add this code in you application to get the signup functionality in your app. I will show you one of the Signup form below to show the validation functionality. You will also find the HttpPost and HttpGet method to use signup services you just have to set URL and call whatever service you want.
Here is the Activity Class
public class SignUpOne extends Activity implements OnClickListener{
private Button signUpButton;
private EditText dateOfBirthEditText, usernameEditText, passwordEditText,retypePassword,emailEditText,phoneEditText;
private Calendar cal;
private int day;
private int month;
private int year;
private DatePickerDialog alert;
private ArrayList<NameValuePair> nameValuePair;
//to display type of seprator in date
private String dateSeprater="/";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.signup_one);
cal = Calendar.getInstance();
day = cal.get(Calendar.DAY_OF_MONTH);
month = cal.get(Calendar.MONTH);
year = cal.get(Calendar.YEAR);
usernameEditText = (EditText) findViewById(R.id.username_editText);
passwordEditText = (EditText) findViewById(R.id.password_editText);
retypePassword = (EditText) findViewById(R.id.retype_password_editText);
emailEditText = (EditText) findViewById(R.id.email_editText);
phoneEditText = (EditText) findViewById(R.id.phone_editText);
dateOfBirthEditText = (EditText) findViewById(R.id.date_of_birth_editText);
signUpButton = (Button) findViewById(R.id.sign_up_button);
signUpButton.setOnClickListener(this);
// creating date picker in alert view
alert =new DatePickerDialog(this,datePickerListener,year, month, day);
dateOfBirthEditText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//to display date time picker
alert.show();
}
});
}
@Override
public void onClick(View view) {
// TODO Auto-generated method stub
if (view == signUpButton) {
// checking validation of all edittext at the time of signup
if(!usernameEditText.getText().toString().isEmpty()&&!passwordEditText.getText().toString().isEmpty()&&!emailEditText.getText().toString().isEmpty()&&!!emailEditText.getText().toString().contains("@"))
{//calling asynctask if all fields are validated
new SignUpUserPost().execute();
new SignUpUserGet().execute();
//Toast.makeText(SignUpOne.this, "success", Toast.LENGTH_LONG).show();
}
if(dateOfBirthEditText.getText().toString().isEmpty()){
dateOfBirthEditText.setError(getString(R.string.empty_filed));
dateOfBirthEditText.requestFocus();
}
if(!emailEditText.getText().toString().contains("@")||!emailEditText.getText().toString().contains("."))
{
emailEditText.setError(getString(R.string.invalid_email));
emailEditText.requestFocus();
}
if(emailEditText.getText().toString().isEmpty()){
emailEditText.setError(getString(R.string.empty_filed));
emailEditText.requestFocus();
}
if(passwordEditText.length()<6){
passwordEditText.setError(getString(R.string.password_length));
passwordEditText.requestFocus();
}
if(!passwordEditText.getText().toString().equals(retypePassword.getText().toString())){
retypePassword.setError(getString(R.string.password_unmatch));
retypePassword.requestFocus();
}
if(retypePassword.getText().toString().isEmpty()){
retypePassword.setError(getString(R.string.empty_filed));
retypePassword.requestFocus();
}
if(passwordEditText.getText().toString().isEmpty()){
passwordEditText.setError(getString(R.string.empty_filed));
passwordEditText.requestFocus();
}
if(usernameEditText.getText().toString().isEmpty()){
usernameEditText.setError(getString(R.string.empty_filed));
usernameEditText.requestFocus();
}
}
}
// this method is to create alert date picker
private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int selectedYear,
int selectedMonth, int selectedDay) {
dateOfBirthEditText.setText(selectedDay + dateSeprater + (selectedMonth + 1) + dateSeprater
+ selectedYear);
}
};
//Asynctask to call signup using post service
private class SignUpUserPost extends AsyncTask<String, Void, String> {
ProgressDialog dialog;
@Override
protected void onPreExecute() {
dialog = new ProgressDialog(SignUpOne.this);
dialog.setCancelable(false);
dialog.setMessage(SignUpOne.this.getResources().getString(R.string.please_wait));
dialog.show();
}
@Override
protected String doInBackground(String... httpMethod) {
// TODO Auto-generated method stub
try {
nameValuePair = new ArrayList<NameValuePair>(2);
nameValuePair.add(new BasicNameValuePair("Username", usernameEditText.getText().toString()));
nameValuePair.add(new BasicNameValuePair("Password", passwordEditText.getText().toString()));
nameValuePair.add(new BasicNameValuePair("email", emailEditText.getText().toString()));
if(!phoneEditText.getText().toString().isEmpty()){
nameValuePair.add(new BasicNameValuePair("phoneNumber", phoneEditText.getText().toString()));
}
nameValuePair.add(new BasicNameValuePair("DateOfBirth", dateOfBirthEditText.getText().toString()));
return WebServiceConnection.postData(ServerUrls.SIGNUP, nameValuePair);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
dialog.dismiss();
XMLParser parser = new XMLParser();
Document doc = parser.getDomElement(result);
NodeList nl = doc.getElementsByTagName(Constants.SIGNUP_RESPONSE);
Element element = (Element) nl.item(0);
}
}
//Asynctask to call signup using get service
private class SignUpUserGet extends AsyncTask<String, Void, String> {
ProgressDialog dialog;
@Override
protected void onPreExecute() {
dialog = new ProgressDialog(SignUpOne.this);
dialog.setCancelable(false);
dialog.setMessage(SignUpOne.this.getResources().getString(R.string.please_wait));
dialog.show();
}
@Override
protected String doInBackground(String... httpMethod) {
// TODO Auto-generated method stub
try {String url;
if(!phoneEditText.getText().toString().isEmpty()){
url =ServerUrls.SIGNUP+"Username"+usernameEditText.getText().toString()+"Password"+passwordEditText.getText().toString()+"email"+emailEditText.getText().toString()+"phoneNumber"+phoneEditText.getText().toString()+"DateOfBirth"+dateOfBirthEditText.getText().toString() ;
}
else{url =ServerUrls.SIGNUP+"Username"+usernameEditText.getText().toString()+"Password"+passwordEditText.getText().toString()+"email"+emailEditText.getText().toString()+"DateOfBirth"+dateOfBirthEditText.getText().toString() ;
}
return WebServiceConnection.GetData(url);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
dialog.dismiss();
XMLParser parser = new XMLParser();
Document doc = parser.getDomElement(result);
NodeList nl = doc.getElementsByTagName(Constants.SIGNUP_RESPONSE);
Element element = (Element) nl.item(0);
}
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
nameValuePair=null;
alert=null;
super.onDestroy();
}
}
Hope you will find it helpful.. :)
0 Comment(s)