Created
January 30, 2022 07:44
-
-
Save crazy-diya/ccd1fbac77b8b20253458456cb109f0d to your computer and use it in GitHub Desktop.
Create Firebase Login And Registation Form using Android Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<androidx.constraintlayout.widget.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=".LoginActivity" | |
android:padding="25dp"> | |
<TextView | |
android:id="@+id/textView" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_marginTop="52dp" | |
android:layout_marginEnd="100dp" | |
android:text="Login Page" | |
android:textSize="38sp" | |
app:layout_constraintEnd_toEndOf="parent" | |
app:layout_constraintTop_toTopOf="parent" /> | |
<EditText | |
android:id="@+id/email" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_marginTop="44dp" | |
android:ems="10" | |
android:inputType="textEmailAddress" | |
android:hint="email address" | |
app:layout_constraintTop_toBottomOf="@+id/textView" | |
tools:layout_editor_absoluteX="100dp" | |
tools:ignore="MissingConstraints" /> | |
<EditText | |
android:id="@+id/pass" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:ems="10" | |
android:inputType="textPassword" | |
android:hint="password" | |
app:layout_constraintBottom_toTopOf="@+id/log" | |
app:layout_constraintTop_toBottomOf="@+id/email" | |
app:layout_constraintVertical_bias="0.527" | |
tools:layout_editor_absoluteX="34dp" /> | |
<Button | |
android:id="@+id/log" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_marginBottom="340dp" | |
android:text="Login" | |
app:layout_constraintBottom_toBottomOf="parent" | |
app:layout_constraintEnd_toEndOf="parent" | |
app:layout_constraintHorizontal_bias="0.498" | |
app:layout_constraintStart_toStartOf="parent" /> | |
</androidx.constraintlayout.widget.ConstraintLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<androidx.constraintlayout.widget.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=".MainActivity" | |
android:padding="20dp"> | |
<TextView | |
android:id="@+id/login_form" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_marginTop="20dp" | |
android:text="Registration Form" | |
android:textSize="32sp" | |
app:layout_constraintEnd_toEndOf="parent" | |
app:layout_constraintStart_toStartOf="parent" | |
app:layout_constraintTop_toTopOf="parent" /> | |
<EditText | |
android:id="@+id/first_name" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_marginTop="28dp" | |
android:ems="10" | |
android:inputType="textPersonName" | |
android:hint="first name : " | |
app:layout_constraintStart_toStartOf="parent" | |
app:layout_constraintTop_toBottomOf="@+id/login_form" /> | |
<EditText | |
android:id="@+id/last_name" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_marginTop="32dp" | |
android:ems="10" | |
android:inputType="textPersonName" | |
android:hint="last name :" | |
app:layout_constraintStart_toStartOf="parent" | |
app:layout_constraintTop_toBottomOf="@+id/first_name" /> | |
<EditText | |
android:id="@+id/email_address" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_marginTop="32dp" | |
android:ems="10" | |
android:inputType="textEmailAddress" | |
android:hint="email address :" | |
app:layout_constraintStart_toStartOf="parent" | |
app:layout_constraintTop_toBottomOf="@+id/last_name" /> | |
<EditText | |
android:id="@+id/mobile_number" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_marginTop="48dp" | |
android:ems="10" | |
android:inputType="number" | |
android:hint="mobile number" | |
app:layout_constraintStart_toStartOf="parent" | |
app:layout_constraintTop_toBottomOf="@+id/email_address" /> | |
<EditText | |
android:id="@+id/password" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_marginTop="40dp" | |
android:ems="10" | |
android:inputType="textPassword" | |
android:hint="password" | |
app:layout_constraintStart_toStartOf="parent" | |
app:layout_constraintTop_toBottomOf="@+id/mobile_number" /> | |
<Button | |
android:id="@+id/button" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_marginStart="152dp" | |
android:layout_marginTop="48dp" | |
android:backgroundTint="#506783" | |
android:text="Register" | |
app:layout_constraintStart_toStartOf="parent" | |
app:layout_constraintTop_toBottomOf="@+id/password" /> | |
</androidx.constraintlayout.widget.ConstraintLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.login; | |
import androidx.annotation.NonNull; | |
import androidx.appcompat.app.AppCompatActivity; | |
import android.os.Bundle; | |
import android.view.View; | |
import android.widget.Button; | |
import android.widget.EditText; | |
import android.widget.Toast; | |
import com.google.firebase.database.DataSnapshot; | |
import com.google.firebase.database.DatabaseError; | |
import com.google.firebase.database.DatabaseReference; | |
import com.google.firebase.database.FirebaseDatabase; | |
import com.google.firebase.database.ValueEventListener; | |
public class LoginActivity extends AppCompatActivity { | |
EditText email, password; | |
Button login; | |
DatabaseReference databaseReference; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_login); | |
email = findViewById(R.id.email); | |
password = findViewById(R.id.pass); | |
login = findViewById(R.id.log); | |
databaseReference = FirebaseDatabase.getInstance().getReference("Login"); | |
login.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
String mail = email.getText().toString(); | |
String pass = password.getText().toString(); | |
System.out.println(mail + " " + pass); | |
databaseReference.addValueEventListener(new ValueEventListener() { | |
@Override | |
public void onDataChange(@NonNull DataSnapshot snapshot) { | |
for (DataSnapshot dataSnapshot : snapshot.getChildren()) { | |
// System.out.println(dataSnapshot.child("email_address").getValue().toString() + " " + dataSnapshot.child("password").getValue().toString()); | |
if (dataSnapshot.child("email_address").getValue().equals(mail) && dataSnapshot.child("password").getValue().equals(pass)) { | |
System.out.println(dataSnapshot.child("email_address").getValue().toString() + " " + dataSnapshot.child("password").getValue().toString()); | |
Toast.makeText(LoginActivity.this, "Successfully login!", Toast.LENGTH_LONG).show(); | |
// | |
} | |
} | |
} | |
@Override | |
public void onCancelled(@NonNull DatabaseError error) { | |
} | |
}); | |
} | |
}); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.login; | |
import androidx.appcompat.app.AppCompatActivity; | |
import android.content.Intent; | |
import android.os.Bundle; | |
import android.view.View; | |
import android.widget.Button; | |
import android.widget.EditText; | |
import android.widget.Toast; | |
import com.google.android.gms.tasks.OnSuccessListener; | |
import com.google.firebase.database.DatabaseReference; | |
import com.google.firebase.database.FirebaseDatabase; | |
public class MainActivity extends AppCompatActivity { | |
EditText first_name, last_name, email_address, mobile_number, password; | |
Button register_button; | |
DatabaseReference firebaseDatabase; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
first_name = findViewById(R.id.first_name); | |
last_name = findViewById(R.id.last_name); | |
email_address = findViewById(R.id.email_address); | |
mobile_number = findViewById(R.id.mobile_number); | |
password = findViewById(R.id.password); | |
register_button = findViewById(R.id.button); | |
firebaseDatabase = FirebaseDatabase.getInstance().getReference("Login"); | |
register_button.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
UserData userData = new UserData(); | |
userData.setFirst_name(first_name.getText().toString()); | |
userData.setLast_name(last_name.getText().toString()); | |
userData.setEmail_address(email_address.getText().toString()); | |
userData.setMobile_number(mobile_number.getText().toString()); | |
userData.setPassword(password.getText().toString()); | |
firebaseDatabase.push().setValue(userData).addOnSuccessListener(new OnSuccessListener<Void>() { | |
@Override | |
public void onSuccess(Void unused) { | |
Toast.makeText(MainActivity.this, "Successfully Registered!", Toast.LENGTH_SHORT).show(); | |
startActivity(new Intent(MainActivity.this,LoginActivity.class)); | |
} | |
}); | |
} | |
}); | |
} | |
} | |
class UserData { | |
String first_name, last_name, email_address, mobile_number, password; | |
public UserData() { | |
} | |
public UserData(String first_name, String last_name, String email_address, String mobile_number, String password) { | |
this.first_name = first_name; | |
this.last_name = last_name; | |
this.email_address = email_address; | |
this.mobile_number = mobile_number; | |
this.password = password; | |
} | |
public String getFirst_name() { | |
return first_name; | |
} | |
public void setFirst_name(String first_name) { | |
this.first_name = first_name; | |
} | |
public String getLast_name() { | |
return last_name; | |
} | |
public void setLast_name(String last_name) { | |
this.last_name = last_name; | |
} | |
public String getEmail_address() { | |
return email_address; | |
} | |
public void setEmail_address(String email_address) { | |
this.email_address = email_address; | |
} | |
public String getMobile_number() { | |
return mobile_number; | |
} | |
public void setMobile_number(String mobile_number) { | |
this.mobile_number = mobile_number; | |
} | |
public String getPassword() { | |
return password; | |
} | |
public void setPassword(String password) { | |
this.password = password; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment