简体   繁体   中英

Register button not working on clicked

I am creating an android application where when in the front page i have a register button and a sign in button.Whenever i click on the sign in button it shows a dialog box but whenever i click on the register button it doesn't work.And i have checked the logcat also it doesn't throw any error.So please can anyone help me to figure out the error?

MainActivity.java

package com.example.vishal.uberclone;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;

import com.example.vishal.uberclone.Model.User;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.rengwuxian.materialedittext.MaterialEditText;

import uk.co.chrisjenx.calligraphy.CalligraphyConfig;
import uk.co.chrisjenx.calligraphy.CalligraphyContextWrapper;

public class MainActivity extends AppCompatActivity{

Button btnSignIn,btnRegister;
RelativeLayout rootLayout;

FirebaseAuth auth;
FirebaseDatabase db;
DatabaseReference users;

@Override
protected void attachBaseContext(Context newBase) {
    super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
}

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

    CalligraphyConfig.initDefault(new CalligraphyConfig.Builder()
            .setDefaultFontPath("fonts/Arkhip_font.ttf")
            .setFontAttrId(R.attr.fontPath)
            .build());
    setContentView(R.layout.activity_main);

    auth = FirebaseAuth.getInstance();
    db=FirebaseDatabase.getInstance();
    users = db.getReference("Users");

    btnRegister = (Button)findViewById(R.id.btnRegister);
    btnSignIn = (Button) findViewById(R.id.btnSignIn);
    rootLayout =(RelativeLayout) findViewById(R.id.rootLayout);

    btnRegister.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            showRegisterDialog();
        }
    });

    btnSignIn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            showLoginDialog();
        }
    });

}

private void showRegisterDialog() {
    final AlertDialog.Builder dialog = new AlertDialog.Builder(this);
    dialog.setTitle("REGISTER");
    dialog.setMessage("Please use email to register");

    LayoutInflater inflater = LayoutInflater.from(this);
    View register_layout = inflater.inflate(R.layout.layout_register, null);

    final MaterialEditText edtEmail = register_layout.findViewById(R.id.edtEmail);
    final MaterialEditText edtPassword = register_layout.findViewById(R.id.edtPassword);
    final MaterialEditText edtName = register_layout.findViewById(R.id.edtName);
    final MaterialEditText edtPhone = register_layout.findViewById(R.id.edtPhone);

    dialog.setView(register_layout);

    //Set button
    dialog.setPositiveButton("REGISTER", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            dialogInterface.dismiss();

            if (TextUtils.isEmpty(edtEmail.getText().toString())) {
                Snackbar.make(rootLayout, "Please enter email address", Snackbar.LENGTH_SHORT).show();

                return;
            }

            if (TextUtils.isEmpty(edtPassword.getText().toString())) {
                Snackbar.make(rootLayout, "Please enter password", Snackbar.LENGTH_SHORT).show();

                return;
            }

            if (edtPassword.getText().toString().length() < 6) {
                Snackbar.make(rootLayout, "Password too short!!!", Snackbar.LENGTH_SHORT).show();

                return;
            }

            if (TextUtils.isEmpty(edtName.getText().toString())) {
                Snackbar.make(rootLayout, "Please enter your name", Snackbar.LENGTH_SHORT).show();

                return;
            }

            if (TextUtils.isEmpty(edtPhone.getText().toString())) {
                Snackbar.make(rootLayout, "Please enter phone number", Snackbar.LENGTH_SHORT).show();

                return;
            }

            auth.createUserWithEmailAndPassword(edtEmail.getText().toString(),edtPassword.getText().toString())
                    .addOnSuccessListener(new OnSuccessListener<AuthResult>() {
                        @Override
                        public void onSuccess(AuthResult authResult) {
                            //Save user to db

                            User user = new User();
                            user.setEmail(edtEmail.getText().toString());
                            user.setName(edtName.getText().toString());
                            user.setPhone(edtPhone.getText().toString());
                            user.setPassword(edtPassword.getText().toString());

                            //Use email to key
                            users.child(user.getEmail())
                                    .setValue(user)
                                    .addOnSuccessListener(new OnSuccessListener<Void>() {
                                        @Override
                                        public void onSuccess(Void aVoid) {
                                            Snackbar.make(rootLayout,"Registered successfully",Snackbar.LENGTH_SHORT)
                                                    .show();

                                        }
                                    })

                                    .addOnFailureListener(new OnFailureListener() {
                                        @Override
                                        public void onFailure(@NonNull Exception e) {
                                            Snackbar.make(rootLayout,"Failed"+e.getMessage(),Snackbar.LENGTH_SHORT)
                                                    .show();
                                        }
                                    });



                        }
                    });






                            dialog.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    dialogInterface.dismiss();
                                }
                            });

                            dialog.show();

                        }
                    });
        }


private void showLoginDialog() {

    final AlertDialog.Builder dialog = new AlertDialog.Builder(this);
    dialog.setTitle("SIGN IN");
    dialog.setMessage("Please use email to sign in");

    LayoutInflater inflater = LayoutInflater.from(this);
    View login_layout = inflater.inflate(R.layout.layout_login,null);

    final MaterialEditText edtEmail = login_layout.findViewById(R.id.edtEmail);
    final MaterialEditText edtPassword = login_layout.findViewById(R.id.edtPassword);

    dialog.setView(login_layout);

    dialog.setPositiveButton("SIGN IN", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    dialogInterface.dismiss();

                    if (TextUtils.isEmpty(edtEmail.getText().toString())) {
                        Snackbar.make(rootLayout, "Please enter email address", Snackbar.LENGTH_SHORT).show();

                        return;
                    }

                    if (TextUtils.isEmpty(edtPassword.getText().toString())) {
                        Snackbar.make(rootLayout, "Please enter password", Snackbar.LENGTH_SHORT).show();

                        return;
                    }

                    if (edtPassword.getText().toString().length() < 6) {
                        Snackbar.make(rootLayout, "Password too short!!!", Snackbar.LENGTH_SHORT).show();

                        return;
                    }

                    //Login
                    auth.signInWithEmailAndPassword(edtEmail.getText().toString(),edtPassword.getText().toString())
                            .addOnSuccessListener(new OnSuccessListener<AuthResult>() {
                                @Override
                                public void onSuccess(AuthResult authResult) {
                                    startActivity(new Intent(MainActivity.this,Welcome.class));
                                    finish();
                                }
                            }).addOnFailureListener(new OnFailureListener() {
                        @Override
                        public void onFailure(@NonNull Exception e) {
                            Snackbar.make(rootLayout,"Failed"+e.getMessage(),Snackbar.LENGTH_SHORT).show();
                        }
                    });

                }
            });

    dialog.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            dialogInterface.dismiss();
        }
    });

            dialog.show();

        }
}

User.java

package com.example.vishal.uberclone.Model;

public class User {

private String email,password,phone,name;

public User(){

}

public User(String email, String password, String name, String phone){
    this.email = email;
    this.password = password;
    this.name = name;
    this.phone = phone;
}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

public String getPassword() {
    return password;
}

public void setPassword(String password) {
    this.password = password;
}

public String getPhone() {
    return phone;
}

public void setPhone(String phone) {
    this.phone = phone;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}
}

Welcome.java

package com.example.vishal.uberclone;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class Welcome extends AppCompatActivity {

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

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:id="@+id/rootLayout"
android:background="@drawable/background"
tools:context="com.example.vishal.uberclone.MainActivity">


<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_marginTop="30dp"
    android:layout_centerHorizontal="true"
    android:gravity="center_horizontal">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="U B E R"
        android:textSize="36sp"
        android:textAlignment="center"
        android:textColor="@android:color/white" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="PARTNER"
        android:textAlignment="center"
        android:textColor="@android:color/white"
        android:textSize="14sp" />



</LinearLayout>

<TextView
    android:id="@+id/text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="20dp"
    android:text="LOOKING FOR THE RIDER APP?"
    android:textAlignment="center"
    android:textColor="@color/bottomText"
    android:textSize="12sp" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_margin="16dp"
    android:layout_above="@+id/text">

    <Button
        android:layout_width="0dp"
        android:layout_weight="1"
        android:textColor="@android:color/white"
        android:background="@drawable/btn_signn_in_background"
        android:layout_height="wrap_content"
        android:layout_marginRight="6dp"
        android:id="@+id/btnSignIn"
        android:text="SIGN IN"/>

    <Button
        android:id="@+id/btnRegister"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="6dp"
        android:layout_weight="1"
        android:background="@drawable/btn_register_background"
        android:text="REGISTER"
        android:textColor="@color/btnRegister" />

</LinearLayout>

</RelativeLayout>

layout_login.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:cardElevation="10dp">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_margin="20dp">

    <com.rengwuxian.materialedittext.MaterialEditText
        android:id="@+id/edtEmail"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:hint="Email"
        android:inputType="textEmailAddress"
        android:textColor="@color/colorPrimary"
        android:textColorHint="@color/colorPrimary"
        android:textSize="20sp"
        app:met_baseColor="@color/colorPrimary"
        app:met_singleLineEllipsis="true"
        app:met_floatingLabel="highlight"
        app:met_primaryColor="@color/colorPrimary"/>

    <com.rengwuxian.materialedittext.MaterialEditText
        android:id="@+id/edtPassword"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:hint="Password"
        android:inputType="textPassword"
        android:textColor="@color/colorPrimary"
        android:textColorHint="@color/colorPrimary"
        android:textSize="20sp"
        app:met_baseColor="@color/colorPrimary"
        app:met_singleLineEllipsis="true"
        app:met_floatingLabel="highlight"
        app:met_primaryColor="@color/colorPrimary"/>

</LinearLayout>

</android.support.v7.widget.CardView>

layout_register.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:cardElevation="10dp">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_margin="20dp">

    <com.rengwuxian.materialedittext.MaterialEditText
        android:id="@+id/edtEmail"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:hint="Email"
        android:inputType="textEmailAddress"
        android:textColor="@color/colorPrimary"
        android:textColorHint="@color/colorPrimary"
        android:textSize="20sp"

        app:met_baseColor="@color/colorPrimary"
        app:met_singleLineEllipsis="true"
        app:met_floatingLabel="highlight"
        app:met_primaryColor="@color/colorPrimary"/>

    <com.rengwuxian.materialedittext.MaterialEditText
        android:id="@+id/edtPassword"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:hint="Password"
        android:inputType="textPassword"
        android:textColor="@color/colorPrimary"
        android:textColorHint="@color/colorPrimary"
        android:textSize="20sp"

        app:met_baseColor="@color/colorPrimary"
        app:met_singleLineEllipsis="true"
        app:met_floatingLabel="highlight"
        app:met_primaryColor="@color/colorPrimary"/>

    <com.rengwuxian.materialedittext.MaterialEditText
        android:id="@+id/edtName"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:hint="Name"
        android:inputType="text"
        android:textColor="@color/colorPrimary"
        android:textColorHint="@color/colorPrimary"
        android:textSize="20sp"

        app:met_baseColor="@color/colorPrimary"
        app:met_singleLineEllipsis="true"
        app:met_floatingLabel="highlight"
        app:met_primaryColor="@color/colorPrimary"/>

    <com.rengwuxian.materialedittext.MaterialEditText
        android:id="@+id/edtPhone"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:hint="Phone"
        android:inputType="phone"
        android:textColor="@color/colorPrimary"
        android:textColorHint="@color/colorPrimary"
        android:textSize="20sp"

        app:met_baseColor="@color/colorPrimary"
        app:met_singleLineEllipsis="true"
        app:met_floatingLabel="highlight"
        app:met_primaryColor="@color/colorPrimary"/>

</LinearLayout>

</android.support.v7.widget.CardView>

You are calling dialog.show(); inside OnClickListener from positiveButton. You must call it outside it:

private void showRegisterDialog() {
    final AlertDialog.Builder dialog = new AlertDialog.Builder(this);
    dialog.setTitle("REGISTER");
    dialog.setMessage("Please use email to register");

    LayoutInflater inflater = LayoutInflater.from(this);
    View register_layout = inflater.inflate(R.layout.layout_register, null);

    final MaterialEditText edtEmail = register_layout.findViewById(R.id.edtEmail);
    final MaterialEditText edtPassword = register_layout.findViewById(R.id.edtPassword);
    final MaterialEditText edtName = register_layout.findViewById(R.id.edtName);
    final MaterialEditText edtPhone = register_layout.findViewById(R.id.edtPhone);

    dialog.setView(register_layout);

    //Set button
    dialog.setPositiveButton("REGISTER", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            dialogInterface.dismiss();

            if (TextUtils.isEmpty(edtEmail.getText().toString())) {
                Snackbar.make(rootLayout, "Please enter email address", Snackbar.LENGTH_SHORT).show();

                return;
            }

            if (TextUtils.isEmpty(edtPassword.getText().toString())) {
                Snackbar.make(rootLayout, "Please enter password", Snackbar.LENGTH_SHORT).show();

                return;
            }

            if (edtPassword.getText().toString().length() < 6) {
                Snackbar.make(rootLayout, "Password too short!!!", Snackbar.LENGTH_SHORT).show();

                return;
            }

            if (TextUtils.isEmpty(edtName.getText().toString())) {
                Snackbar.make(rootLayout, "Please enter your name", Snackbar.LENGTH_SHORT).show();

                return;
            }

            if (TextUtils.isEmpty(edtPhone.getText().toString())) {
                Snackbar.make(rootLayout, "Please enter phone number", Snackbar.LENGTH_SHORT).show();

                return;
            }

            auth.createUserWithEmailAndPassword(edtEmail.getText().toString(),edtPassword.getText().toString())
                    .addOnSuccessListener(new OnSuccessListener<AuthResult>() {
                        @Override
                        public void onSuccess(AuthResult authResult) {
                            //Save user to db

                            User user = new User();
                            user.setEmail(edtEmail.getText().toString());
                            user.setName(edtName.getText().toString());
                            user.setPhone(edtPhone.getText().toString());
                            user.setPassword(edtPassword.getText().toString());

                            //Use email to key
                            users.child(user.getEmail())
                                    .setValue(user)
                                    .addOnSuccessListener(new OnSuccessListener<Void>() {
                                        @Override
                                        public void onSuccess(Void aVoid) {
                                            Snackbar.make(rootLayout,"Registered successfully",Snackbar.LENGTH_SHORT)
                                                    .show();

                                        }
                                    })

                                    .addOnFailureListener(new OnFailureListener() {
                                        @Override
                                        public void onFailure(@NonNull Exception e) {
                                            Snackbar.make(rootLayout,"Failed"+e.getMessage(),Snackbar.LENGTH_SHORT)
                                                    .show();
                                        }
                                    });



                        }
                    });
        }
    });

    dialog.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            dialogInterface.dismiss();
        }
    });

    dialog.show();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM