Main Activity (Login Page)

This is Where our Users Login into the App.


Here in this Activity  the Following are done:


  1. Login
  2. Navigation for Sign up Screen
  3. Navigation  for Forget Password Screen
  4. And Internally We check If User Logged in or Logged out.

MainActivity.java










  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package com.ramesh.taskearnerexample;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.CardView;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
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;
import com.google.firebase.auth.FirebaseUser;

public class MainActivity extends AppCompatActivity {

    private EditText Name;
    private EditText Password;
    private TextView Info;
    private Button Login;
    private CardView fpcard, nucard;
    private int counter = 5;
    private TextView userRegistration;
    private FirebaseAuth firebaseAuth;
    private ProgressDialog progressDialog;
    private TextView forgotPassword;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_main );



        Name = (EditText) findViewById( R.id.etName );
        Password = (EditText) findViewById( R.id.etPassword );
        Info = (TextView) findViewById( R.id.tvInfo );
        Login = (Button) findViewById( R.id.btnLogin );
        userRegistration = (TextView) findViewById( R.id.tvRegister );
        forgotPassword = (TextView) findViewById( R.id.tvForgotPassword );
        fpcard = (CardView) findViewById( R.id.FpCard );
        nucard = (CardView) findViewById( R.id.nucard ) ;

        Info.setText( "No of attempts remaining: 5" );

        firebaseAuth = FirebaseAuth.getInstance();
        progressDialog = new ProgressDialog( this );

        FirebaseUser user = firebaseAuth.getCurrentUser();

        if (user != null) {
            finish();
            startActivity( new Intent( MainActivity.this, SecondActivity.class ) );
        }

        Login.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                validate( Name.getText().toString(), Password.getText().toString() );
            }
        } );

        userRegistration.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity( new Intent( MainActivity.this, RegistrationActivity.class ) );
            }
        } );

        forgotPassword.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity( new Intent( MainActivity.this, PasswordActivity.class ) );
            }
        } );

        fpcard.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                startActivity( new Intent( MainActivity.this, PasswordActivity.class ) );

            }
        } );

        nucard.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                startActivity( new Intent( MainActivity.this, RegistrationActivity.class ) );

            }
        } );
    }

    private void validate(String userName, String userPassword) {

        progressDialog.setMessage( "Processing.... Please Wait" );
        progressDialog.show();

        firebaseAuth.signInWithEmailAndPassword( userName, userPassword ).addOnCompleteListener( new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {
                if (task.isSuccessful()) {
                    progressDialog.dismiss();
                    //Toast.makeText(MainActivity.this, "Login Successful", Toast.LENGTH_SHORT).show();
                    checkEmailVerification();
                } else {
                    Toast.makeText( MainActivity.this, "Login Failed", Toast.LENGTH_SHORT ).show();
                    counter--;
                    Info.setText( "No of attempts remaining: " + counter );
                    progressDialog.dismiss();
                    if (counter == 0) {
                        Login.setEnabled( false );
                    }
                }
            }
        } );


    }

    private void checkEmailVerification() {
        FirebaseUser firebaseUser = firebaseAuth.getInstance().getCurrentUser();
        Boolean emailflag = firebaseUser.isEmailVerified();

        startActivity( new Intent( MainActivity.this, SecondActivity.class ) );

//        if(emailflag){
//            finish();
//            startActivity(new Intent(MainActivity.this, SecondActivity.class));
//        }else{
//            Toast.makeText(this, "Verify your email", Toast.LENGTH_SHORT).show();
//            firebaseAuth.signOut();
//        }
    }

}
Powered by Blogger.