Hello everyone!
Today, I will be covering how to create the register page of your app using Android Studio.
First off, start a new project by clicking Files > New > New Project. Name the project RegisterTutorial or anything you choose.

Now choose the Phone and Tablet category, and set the minimum SDK you want for the lowest devices that can use your application. I’ve used the default API 15: Android 4.0.3 (Ice Cream Sandwich) which will run on 100% of devices.

Next, choose “Empty Activity” because we will be adding onto it.

Finally, name the new activity.

Wait until the project builds and synchronize, then navigate to the layout you created with the new Activity. Make sure it is ConstraintLayout because in this way, we can align the EditText fields and Buttons according to the phone’s screen.
Click on “Design,” delete the “Hello World” in the center and add in the desired number of EditText fields. I’ve used a total of 6 EditTexts. Click on the EditText and drag the left side to the left of the phone and the right side to the right of the phone, so that the EditText is centered. Do the same for the remaining five. Finally add in two buttons, one to register the information to store in the database, and the other to cancel the registration.
When you click on the edittexts, there should be a right popup of information for the edittext. Make sure to delete the “text” field of the EditText, then type in the desired text you want for the user to input in the “hint” field. Also, change the id name to distinguish and identify them later.

We’re almost done!! After aligning the EditTexts and Buttons, we can now move onto Java Coding! Are you ready?
Now move onto the “MainActivity.java” file. It should look like the below.

We are going to identify the EditText fields and Buttons that we created in the layout. Declare the EditText field and Button variables and assign them accordingly as below.

Now, we have to set what happens when the register button is clicked, or when the cancel button is clicked. Ideally, if register button is clicked, then we will register the inputted texts in the EditTexts, and if cancel is clicked, then we can just cancel the current activity. We’ll define register() and cancel() as functions of when each button is clicked.
In the register function, we want to get the information from the EditText fields. We also want to show a progress dialog and make it appear when the user clicks register so that the app is actually showing that the user is registering their information rather than them waiting. We can do this like the following:

We get the text from the EditText fields into strings, and then we trim it so that we remove white spaces.
Next, we have to save the information into some database. At this point, you have the option to choose your database. For simplicity, let’s use the built-in SQLite Database. To do so, we will have to create a new java class “DatabaseHelper”
Right click your project files (in my case it is com.tutorials.mouacoding.registertutorial) and select New > Java Class, and name it “DatabaseHelper”

Let’s fill in this class. Ready? Set? GO!
First we’ll define the database name, call it “User.db”, the table name “user_table” and each of the columns (ID, First name, last name, email, password, phone).
in the OnCreate method, we’ll use db.execSQL to create a table of the table name, and then the desired column order. In the onUpgrade method, we’ll drop the table if the table already exists.
Finally, we’ll define the insertData() function, setting the column values and finally using db.insert() to store the values into the SQLite Database. I’ve set long result = db.insert because the returned value is 1 if it was able to store, otherwise 0 if not.

We’re almost done!! Now, let’s call the insertData() in our register() function from the MainActivity. But first lets create a new activity so that once we finish registering, it will continue to the next activity of the app.

Okay, now onto business!! let’s insert into the database now. We’ll write the register() function as below.
First, we define the DatabaseHelper variable, and progress dialog. Then, we’ll use an if statement to check if the password and confirm password equal, if they are then we can continue inserting. call myDB.insert(string fields…) and then finally dismiss the progress dialog once the action is done. We can check if it inserted or not by setting the boolean declared earlier to true or false via the db.insert call() and if it is inserted correctly, then go to the next activity.
We also define the cancel button by simply finish() which finishes the current activity and should thus go back to the “Login Activity.”

Now we test everything so far! Looks like it is working.

That’s it!! Check out the next tutorial as I show you how to check if the the database is inserted correctly!
Thanks and Happy Coding!
MainActivity.java
package com.tutorials.mouacoding.registertutorial;
import android.app.ProgressDialog;
import android.content.Intent;
import android.support.annotation.NonNull;
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 firstName,
lastName,
email,
password,
confirmPassword,
phone;
Button registerButton,
cancelButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
firstName = (EditText) findViewById(R.id.firstname);
lastName = (EditText) findViewById(R.id.lastname);
email = (EditText) findViewById(R.id.email);
password = (EditText) findViewById(R.id.password);
confirmPassword = (EditText) findViewById(R.id.confirmpassword);
phone = (EditText) findViewById(R.id.phone);
registerButton = (Button) findViewById(R.id.registerbutton);
cancelButton = (Button) findViewById(R.id.cancel);
// If register button is clicked, then register the information.
registerButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
register();
}
});
// If cancel button is clicked, then cancel.
cancelButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
cancel();
}
});
}
public void register() {
String sFirstName, sLastName, sEmail, sPassword, sConfirmPassword, sPhone;
boolean isInserted = false;
ProgressDialog progressDialog = new ProgressDialog(this);
DatabaseHelper myDb = new DatabaseHelper(this);
sFirstName = firstName.getText().toString().trim();
sLastName = lastName.getText().toString().trim();
sEmail = email.getText().toString().trim();
sPassword = password.getText().toString().trim();
sConfirmPassword = confirmPassword.getText().toString().trim();
sPhone = phone.getText().toString().trim();
progressDialog.setMessage("Registering Please Wait...");
progressDialog.show();
//Check if password equals confirm password, if so then store the data
if(String.valueOf(password.getText().toString()).equals(confirmPassword.getText().toString())
&& firstName.getText().toString()!=null) {
isInserted = myDb.insertData(sFirstName,
sLastName,
sEmail,
sPassword,
sPhone);
//Register complete, dismiss the dialog.
progressDialog.dismiss();
}
if(isInserted == true) {
Toast.makeText(MainActivity.this, "Data Inserted", Toast.LENGTH_LONG).show();
Intent intent = new Intent(MainActivity.this, NextActivity.class);
startActivity(intent);
}
else
Toast.makeText(MainActivity.this,"Data not Inserted",Toast.LENGTH_LONG).show();
}
public void cancel() {
finish(); //Simply finish the activity if cancelled.
}
}
activity_main.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="com.tutorials.mouacoding.registertutorial.MainActivity">
<EditText
android:id="@+id/firstname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:ems="10"
android:hint="First Name"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/appname" />
<TextView
android:id="@+id/appname"
android:layout_width="209dp"
android:layout_height="33dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:text="App Name"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
tools:layout_editor_absoluteY="16dp" />
<EditText
android:id="@+id/lastname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:ems="10"
android:hint="Last Name"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/firstname" />
<EditText
android:id="@+id/email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:ems="10"
android:hint="Email"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/lastname" />
<EditText
android:id="@+id/password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:ems="10"
android:hint="Password"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/email" />
<EditText
android:id="@+id/confirmpassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:ems="10"
android:hint="Confirm Password"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/password" />
<EditText
android:id="@+id/phone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:ems="10"
android:hint="Phone"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/confirmpassword" />
<Button
android:id="@+id/registerbutton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="Register"
app:layout_constraintTop_toBottomOf="@+id/phone"
tools:layout_editor_absoluteX="93dp" />
<Button
android:id="@+id/cancel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="Cancel"
app:layout_constraintTop_toBottomOf="@+id/registerbutton"
tools:layout_editor_absoluteX="148dp" />
</android.support.constraint.ConstraintLayout>
DatabaseHelper.java
package com.tutorials.mouacoding.registertutorial;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "User.db";
public static final String TABLE_NAME = "user_table";
public static final String COL_1 = "ID";
public static final String COL_2 = "FIRSTNAME";
public static final String COL_3 = "LASTNAME";
public static final String COL_4 = "EMAIL";
public static final String COL_5 = "PASSWORD";
public static final String COL_6 = "PHONE";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT," + "FIRSTNAME TEXT, " + "LASTNAME TEXT, " + "EMAIL TEXT," +
"PASSWORD TEXT," + "PHONE TEXT);");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
onCreate(db);
}
public boolean insertData(String aFirstName,String aLastName, String aEmail, String aPassword, String aPhone) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_2,aFirstName);
contentValues.put(COL_3,aLastName);
contentValues.put(COL_4,aEmail);
contentValues.put(COL_5,aPassword);
contentValues.put(COL_6,aPhone);
long result = db.insert(TABLE_NAME,null ,contentValues);
if(result == -1)
return false;
else
return true;
}
public Cursor getAllData() {
SQLiteDatabase db = this.getWritableDatabase();
Cursor res = db.rawQuery("select * from "+TABLE_NAME,null);
return res;
}
public boolean updateData(String id,String name,String surname,String marks) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_1,id);
contentValues.put(COL_2,name);
contentValues.put(COL_3,surname);
contentValues.put(COL_4,marks);
db.update(TABLE_NAME, contentValues, "ID = ?",new String[] { id });
return true;
}
public Integer deleteData (String id) {
SQLiteDatabase db = this.getWritableDatabase();
return db.delete(TABLE_NAME, "ID = ?",new String[] {id});
}
}