Email and password authentication using Firebase on Whats App clone

Shailendar Kurkoti
2 min readMay 21, 2021

Step 1 : Create UI for authentication

<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:background="@color/grayBackground"
android:gravity="center"
android:orientation="vertical"
android:padding="15dp"
tools:context=".signup_activity"
>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="30dp"
android:gravity="center_horizontal"
android:orientation="vertical"
>

<ImageView
android:id="@+id/imageView3"
android:layout_width="100dp"
android:layout_height="100dp"
app:srcCompat="@drawable/ic_whatsapp"
/>

<EditText
android:id="@+id/txtUsername"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:layout_marginBottom="10dp"
android:layout_weight="1"
android:background="@color/white"
android:ems="10"
android:hint="Name"
android:inputType="textPersonName"
android:padding="9dp"
/>

<EditText
android:id="@+id/txtEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_weight="1"
android:background="@color/white"
android:ems="10"
android:hint="Email"
android:inputType="textEmailAddress"
android:padding="9dp"
/>

<EditText
android:id="@+id/txtPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_weight="1"
android:background="@color/white"
android:ems="10"
android:hint="Password"
android:inputType="textPassword"
android:padding="9dp"
/>

<Button
android:id="@+id/btnSignUp"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:padding="15dp"
android:text="Sign up"
app:cornerRadius="4dp"
/>

<TextView
android:id="@+id/lblAlreadyHaveAccount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:gravity="right"
android:padding="3dp"
android:text="Already have account"
/>

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="30dp"
android:layout_marginEnd="30dp"
android:orientation="horizontal"
>

<Button
android:id="@+id/btnGoogle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="4dp"
android:layout_weight="1"
android:backgroundTint="@color/white"
android:drawableLeft="@drawable/ic_search"
android:padding="15dp"
android:text="Google"
android:textColor="@color/black"
app:cornerRadius="4dp"
/>

<Button
android:id="@+id/btnFacebook"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="4dp"
android:layout_weight="1"
android:backgroundTint="@color/fb_color"
android:drawableLeft="@drawable/ic_facebook__2_"
android:padding="15dp"
android:text="Facebook"
app:cornerRadius="4dp"
/>

</LinearLayout>

<TextView
android:id="@+id/lblSignUp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:text="Sign up with phone"
android:textColor="@color/colorPrimary"
android:textSize="18sp"
/>

</LinearLayout>

Step 2: Connect firebase to android (firebase authentication and Realtime database)

Step 3 : Add dependencies to gradle

plugins {
id 'com.android.application'
id 'com.google.gms.google-services'
}

android {
buildFeatures {
viewBinding true
}
compileSdkVersion 30
buildToolsVersion "30.0.3"

defaultConfig {
applicationId "exm.shailendra.whatsapp"
minSdkVersion 21
targetSdkVersion 30
versionCode 1
versionName "1.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}

dependencies {

implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.3.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
implementation platform('com.google.firebase:firebase-bom:28.0.1')
implementation 'com.google.firebase:firebase-auth'
implementation 'com.google.firebase:firebase-database'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}

Step 3: JAVA Code

public class signup_activity extends AppCompatActivity {
ActivitySignupBinding binding;
private FirebaseAuth auth;
private FirebaseDatabase database;
ProgressDialog progressDialog;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivitySignupBinding.inflate(getLayoutInflater());
View view = binding.getRoot();
setContentView(view);
getSupportActionBar().hide();

auth = FirebaseAuth.getInstance();
database = FirebaseDatabase.getInstance();

progressDialog = new ProgressDialog(signup_activity.this);
progressDialog.setTitle("Creating Account");
progressDialog.setMessage("We are creating your account");

binding.btnSignUp.setOnClickListener(v -> {
progressDialog.show();
auth.createUserWithEmailAndPassword(binding.txtEmail.getText().toString(),
binding.txtPassword.getText().toString())
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NotNull Task<AuthResult> task) {
progressDialog.dismiss();

if (task.isSuccessful()){
Toast.makeText(signup_activity.this, "User created successfully",
Toast.LENGTH_SHORT).show();
User user = new User(binding.txtUsername.getText().toString(), binding
.txtEmail.getText().toString(),binding.txtPassword.getText().toString());

String id = task.getResult().getUser().getUid();
database.getReference().child("Users").child(id).setValue(user);


}
else {
Toast.makeText(signup_activity.this, task.getException().getMessage(), Toast.LENGTH_SHORT).show();
}

}
});
});
}
}

--

--