0

I have connected an android application to a firebase server and successfully able to signup and login using firebase auth, but i want to display a welcome message in the Home page(this is the page that comes after logging in), how to do it?

This is my login.java page:

package com.example.user.smartkitchen;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
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 Login extends AppCompatActivity {

    public Button login,loginResetPass,RegisterLinkButton;
    private EditText UserNameTxt, PasswordTxt;
    private FirebaseAuth auth;
    //private ProgressBar progressBar;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        auth = FirebaseAuth.getInstance();

        if (auth.getCurrentUser() != null) {
            startActivity(new Intent(Login.this, MainActivity.class));
            finish();
        }
        setContentView(R.layout.activity_login);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        UserNameTxt = (EditText) findViewById(R.id.UserNameTxt);
        PasswordTxt = (EditText) findViewById(R.id.PasswordTxt);
        //progressBar = (ProgressBar) findViewById(R.id.progressBar);
        login = (Button) findViewById(R.id.login);
        loginResetPass = (Button) findViewById(R.id.loginResetPass);
        RegisterLinkButton = (Button) findViewById(R.id.RegisterLinkButton);

        //Get Firebase auth instance
        auth = FirebaseAuth.getInstance();

        RegisterLinkButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(Login.this, Register.class));
            }
        });
        loginResetPass.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(Login.this, forgotPassword.class));
            }
        });

        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String email = UserNameTxt.getText().toString();
                final String password = PasswordTxt.getText().toString();

                if (TextUtils.isEmpty(email)) {
                    Toast.makeText(getApplicationContext(), "Enter email address!", Toast.LENGTH_SHORT).show();
                    return;
                }

                if (TextUtils.isEmpty(password)) {
                    Toast.makeText(getApplicationContext(), "Enter password!", Toast.LENGTH_SHORT).show();
                    return;
                }

                //progressBar.setVisibility(View.VISIBLE);

                //authenticate user
                auth.signInWithEmailAndPassword(email, password)
                        .addOnCompleteListener(Login.this, new OnCompleteListener<AuthResult>() {
                            @Override
                            public void onComplete(@NonNull Task<AuthResult> task) {
                                // If sign in fails, display a message to the user. If sign in succeeds
                                // the auth state listener will be notified and logic to handle the
                                // signed in user can be handled in the listener.
                                //progressBar.setVisibility(View.GONE);
                                if (!task.isSuccessful()) {
                                    // there was an error
                                    if (password.length() < 6) {
                                        PasswordTxt.setError(getString(R.string.minimum_password));
                                    } else {
                                        Toast.makeText(Login.this, getString(R.string.auth_failed), Toast.LENGTH_LONG).show();
                                    }
                                } else {
                                    Intent intent = new Intent(Login.this, Home.class);
                                    startActivity(intent);
                                    finish();
                                }
                            }
                        });
            }
        });

    }
}

This is my register.java page:

package com.example.user.smartkitchen;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
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 Register extends AppCompatActivity {

    public Button Register1;
    public ImageButton registerBackButton;
    private EditText EmailTxtRegister, PasswordTxtRegister;
    private Button Register, ForgotPassButton;
    private FirebaseAuth auth;
    private ProgressBar progressBar;


    public void GoBacktoMain(){
        registerBackButton = (ImageButton) findViewById(R.id.registerBackButton);
        registerBackButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent toy = new Intent(Register.this,MainActivity.class);
                startActivity(toy);
            }
        });

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        auth = FirebaseAuth.getInstance();

        Register = (Button) findViewById(R.id.Register);
        EmailTxtRegister = (EditText) findViewById(R.id.EmailTxtRegister);
        PasswordTxtRegister = (EditText) findViewById(R.id.PasswordTxtRegister);
        ForgotPassButton = (Button) findViewById(R.id.ForgotPassButton);
        progressBar = (ProgressBar) findViewById(R.id.progressBar);

        ForgotPassButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(Register.this, forgotPassword.class));
            }
        });

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

                String email = EmailTxtRegister.getText().toString().trim();
                String password = PasswordTxtRegister.getText().toString().trim();

                if (TextUtils.isEmpty(email)) {
                    Toast.makeText(getApplicationContext(), "Enter email address!", Toast.LENGTH_SHORT).show();
                    return;
                }

                if (TextUtils.isEmpty(password)) {
                    Toast.makeText(getApplicationContext(), "Enter password!", Toast.LENGTH_SHORT).show();
                    return;
                }

                if (password.length() < 6) {
                    Toast.makeText(getApplicationContext(), "Password too short, enter minimum 6 characters!", Toast.LENGTH_SHORT).show();
                    return;
                }

                progressBar.setVisibility(View.VISIBLE);
                //create user
                auth.createUserWithEmailAndPassword(email, password)
                        .addOnCompleteListener(Register.this, new OnCompleteListener<AuthResult>() {
                            @Override
                            public void onComplete(@NonNull Task<AuthResult> task) {
                                Toast.makeText(Register.this, "createUserWithEmail:onComplete:" + task.isSuccessful(), Toast.LENGTH_SHORT).show();
                                progressBar.setVisibility(View.GONE);
                                // If sign in fails, display a message to the user. If sign in succeeds
                                // the auth state listener will be notified and logic to handle the
                                // signed in user can be handled in the listener.
                                if (!task.isSuccessful()) {
                                    Toast.makeText(Register.this, "Authentication failed." + task.getException(),
                                            Toast.LENGTH_SHORT).show();
                                } else {
                                    startActivity(new Intent(Register.this, Home.class));
                                    finish();
                                }
                            }
                        });

            }
        });
        GoBacktoMain();
    }
    @Override
    protected void onResume() {
        super.onResume();
        progressBar.setVisibility(View.GONE);

    }


}

This is the page in which I need to display the hello message:

package com.example.user.smartkitchen;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;

public class Home extends AppCompatActivity {

    public ImageButton BudgetButton;

 public ImageButton IngredientsButton;

    public ImageButton GasButton;

    public ImageButton HomeBackButton;


    public void EnterIngredientsFromHome(){
        IngredientsButton = (ImageButton) findViewById(R.id.IngredientsButton);
        IngredientsButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent toy = new Intent(Home.this,Ingredients.class);
                startActivity(toy);
            }
        });


    }

    public void EnterGasFromHome(){
        GasButton = (ImageButton) findViewById(R.id.GasButton);
        GasButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent toy = new Intent(Home.this,Gas.class);
                startActivity(toy);
            }
        });


    }

    public void EnterBudgetFromHome(){
        BudgetButton = (ImageButton) findViewById(R.id.BudgetButton);
        BudgetButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent toy = new Intent(Home.this,Budget.class);
                startActivity(toy);
            }
        });


    }

    public void GoBacktoRegister(){
        HomeBackButton = (ImageButton) findViewById(R.id.HomeBackButton);
        HomeBackButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent toy = new Intent(Home.this,MainActivity.class);
                startActivity(toy);
            }
        });

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        EnterIngredientsFromHome();
        EnterBudgetFromHome();
        EnterGasFromHome();
        GoBacktoRegister();
    }
}

Pls help someone

0 Answers0