Skip to content

Instantly share code, notes, and snippets.

@Reevan799
Created June 29, 2020 11:38
Show Gist options
  • Select an option

  • Save Reevan799/8076d7c14da85b9f338f1f3e500136d6 to your computer and use it in GitHub Desktop.

Select an option

Save Reevan799/8076d7c14da85b9f338f1f3e500136d6 to your computer and use it in GitHub Desktop.
MainActivity Java code
package com.gkquizadmin;
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.ProgressBar;
import android.widget.Toast;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
public class MainActivity extends AppCompatActivity {
private EditText email, password;
private ProgressBar progressBar;
private FirebaseAuth firebaseAuth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
email = findViewById(R.id.email);
password = findViewById(R.id.password);
Button login = findViewById(R.id.login);
progressBar = findViewById(R.id.progressBar);
firebaseAuth = FirebaseAuth.getInstance();
if (firebaseAuth.getCurrentUser() != null) {
//intent
return;
}
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (email.getText().toString().isEmpty()) {
email.setError("required!!");
return;
} else {
email.setError(null);
}
if (password.getText().toString().isEmpty()) {
password.setError("required!!");
return;
} else {
password.setError(null);
}
progressBar.setVisibility(View.VISIBLE);
firebaseAuth.signInWithEmailAndPassword(email.getText().toString(), password.getText().toString()).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
Toast.makeText(MainActivity.this, "Success", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, task.getException().toString(), Toast.LENGTH_SHORT).show();
// Toast.makeText(MainActivity.this, "Failed", Toast.LENGTH_SHORT).show();
}
progressBar.setVisibility(View.INVISIBLE);
}
});
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment