Sign Up form in Android app is very popular.. so in this blog we see how to design a simple sign up form with email validation, password length, confirm password matches with password and the 10 digit phone number.
1. MainActivity.java file
package com.example.signupform;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
EditText e,e1,e2,e3;
Button b;
String email, emailPattern, password, cpassword, pnumber;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b = (Button)findViewById(R.id.btn);
e = (EditText)findViewById((R.id.ed1));
e1 = (EditText)findViewById((R.id.ed2));
e2 = (EditText)findViewById((R.id.ed3));
e3 = (EditText)findViewById((R.id.ed4));
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String email =e.getText().toString().trim();
String password =e1.getText().toString().trim();
String cpassword =e2.getText().toString().trim();
String pnumber =e3.getText().toString().trim();
String emailPattern = “[a-zA-Z0-9._-]+@[a-z]+\.+[a-z]+”;
if(email.isEmpty()||password.isEmpty()||cpassword.isEmpty()||pnumber.isEmpty()){
Toast.makeText(getApplicationContext(),”Fill all details”,Toast.LENGTH_LONG).show();
}
else if(!email.matches(emailPattern)){
Toast.makeText(getApplicationContext(),”invalid email”,Toast.LENGTH_SHORT).show();
}
else if(!password.matches(cpassword)){
Toast.makeText(getApplicationContext(),”password didn’t match”,Toast.LENGTH_SHORT).show();
}
else if(pnumber.length()!= 10) {
Toast.makeText(getApplicationContext(),”enter 10 digit number”,Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(getApplicationContext(),”Sign up successfully”,Toast.LENGTH_LONG).show();
}
}
});
}
}
2. activity_main.xml file
<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout 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”
android:orientation=”vertical”
tools:context=”.MainActivity”>
<EditText
android:id=”@+id/ed1″
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:hint=”Email Id”
android:layout_marginTop=”5dp”
android:textColorHint=”@color/colorAccent”
android:inputType=”textPersonName” />
<EditText
android:id=”@+id/ed2″
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:ems=”10″
android:hint=”Password”
android:layout_marginTop=”5dp”
android:textColorHint=”@color/colorAccent”
android:inputType=”textPersonName” />
<EditText
android:id=”@+id/ed3″
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:ems=”10″
android:hint=”Confirm Password”
android:layout_marginTop=”5dp”
android:textColorHint=”@color/colorAccent”
android:inputType=”textPersonName” />
<EditText
android:id=”@+id/ed4″
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:ems=”10″
android:hint=”Phone number”
android:layout_marginTop=”5dp”
android:textColorHint=”@color/colorAccent”
android:inputType=”textPersonName” />
<Button
android:id=”@+id/btn”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:background=”#302727″
android:text=”Sign Up”
android:textSize=”20dp”
android:layout_marginTop=”20dp”
android:textColor=”#FFEB3B” />
</LinearLayout>