Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MainActivity extends AppCompatActivity {
- MaterialEditText edtNewUser, edtNewPassword, edtNewEmail; //sign up
- MaterialEditText edtUser, edtPassword; //for sign in
- Button btnSignUp, btnSignIn;
- FirebaseDatabase database;
- DatabaseReference users;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- //Firebase
- database= FirebaseDatabase.getInstance();
- if(){
- users = database.getReference("Students");
- }
- else {
- users = database.getReference("Teachers");
- }
- edtUser = (MaterialEditText)findViewById(R.id.edtUser);
- edtPassword = (MaterialEditText)findViewById(R.id.edtPassword);
- btnSignIn = (Button)findViewById(R.id.btn_sign_in);
- btnSignUp = (Button)findViewById(R.id.btn_sign_up);
- btnSignUp.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- showSignUpDialog();
- }
- });
- btnSignIn.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- signIn(edtUser.getText().toString(), edtPassword.getText().toString());
- }
- });
- }
- private void signIn(final String user,final String pwd) {
- users.addListenerForSingleValueEvent(new ValueEventListener() {
- @Override
- public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
- if(dataSnapshot.child(user).exists())
- {
- if(!user.isEmpty())
- {
- User login = dataSnapshot.child(user).getValue(User.class);
- if(login.getPassword().equals(pwd))
- {
- Toast.makeText(MainActivity.this, "Login ok!", Toast.LENGTH_SHORT).show();
- }
- else
- {
- Toast.makeText(MainActivity.this, "Wrong password", Toast.LENGTH_SHORT).show();
- }
- }
- else
- {
- Toast.makeText(MainActivity.this, "Please enter your user name", Toast.LENGTH_SHORT).show();
- }
- }
- else
- {
- Toast.makeText(MainActivity.this, "User is not existing!", Toast.LENGTH_SHORT).show();
- }
- }
- @Override
- public void onCancelled(@NonNull DatabaseError databaseError) {
- }
- });
- }
- private void showSignUpDialog() {
- AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);
- alertDialog.setTitle("Sign up");
- alertDialog.setMessage("Please fill full information");
- LayoutInflater inflater = this.getLayoutInflater();
- View sign_up_layout = inflater.inflate(R.layout.sign_up_layout, null);
- edtNewUser = (MaterialEditText)sign_up_layout.findViewById(R.id.edtNewUserName);
- edtNewPassword = (MaterialEditText)sign_up_layout.findViewById(R.id.edtNewPassword);
- edtNewEmail = (MaterialEditText)sign_up_layout.findViewById(R.id.edtNewEmail);
- alertDialog.setView(sign_up_layout);
- alertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialogInterface, int i) {
- dialogInterface.dismiss();
- }
- });
- alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialogInterface, int i) {
- final User user = new User(edtNewUser.getText().toString(), edtNewPassword.getText().toString(), edtNewEmail.getText().toString());
- users.addListenerForSingleValueEvent(new ValueEventListener() {
- @Override
- public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
- if(dataSnapshot.child(user.getUserName()).exists()){
- Toast.makeText(MainActivity.this, "User already exists!", Toast.LENGTH_SHORT).show();
- }
- else {
- users.child(user.getUserName()).setValue(user);
- Toast.makeText(MainActivity.this, "User registration success!", Toast.LENGTH_SHORT).show();
- }
- }
- @Override
- public void onCancelled(@NonNull DatabaseError databaseError) {
- }
- });
- dialogInterface.dismiss();
- }
- });
- alertDialog.show();
- }
- }
- ****************************************
- package com.cristi.logintest.Model;
- public class User {
- private String userName;
- private String password;
- private String email;
- public User(){
- }
- public User(String userName, String password, String email) {
- this.userName = userName;
- this.password = password;
- this.email = email;
- }
- public String getUserName() {
- return userName;
- }
- public void setUserName(String userName) {
- this.userName = userName;
- }
- public String getPassword() {
- return password;
- }
- public void setPassword(String password) {
- this.password = password;
- }
- public String getEmail() {
- return email;
- }
- public void setEmail(String email) {
- this.email = email;
- }
- }
- *************************************
- <?xml version="1.0" encoding="utf-8"?>
- <android.support.constraint.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"
- android:background="@color/colorPrimary"
- tools:context=".MainActivity">
- <RelativeLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- app:cardElevation="4dp"
- android:layout_margin="8dp"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintLeft_toLeftOf="parent"
- app:layout_constraintRight_toRightOf="parent"
- app:layout_constraintTop_toTopOf="parent" >
- <android.support.v7.widget.CardView
- android:id="@+id/info_login"
- xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_margin="8dp"
- app:cardElevation="4dp">
- <LinearLayout
- android:padding="16dp"
- android:orientation="vertical"
- android:layout_width="match_parent"
- android:layout_height="wrap_content">
- <com.rengwuxian.materialedittext.MaterialEditText
- android:id="@+id/edtUser"
- android:hint="User name"
- android:textColorHint="@color/colorPrimary"
- android:textColor="@color/colorPrimary"
- android:textSize="24sp"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- app:met_baseColor="@color/colorPrimary"
- app:met_floatingLabel="highlight"
- app:met_primaryColor="@color/colorPrimary"
- app:met_singleLineEllipsis="true"
- />
- <com.rengwuxian.materialedittext.MaterialEditText
- android:id="@+id/edtPassword"
- android:hint="Password"
- android:textColorHint="@color/colorPrimary"
- android:textColor="@color/colorPrimary"
- android:textSize="24sp"
- android:inputType="textPassword"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- app:met_baseColor="@color/colorPrimary"
- app:met_floatingLabel="highlight"
- app:met_primaryColor="@color/colorPrimary"
- app:met_singleLineEllipsis="true"
- />
- </LinearLayout>
- </android.support.v7.widget.CardView>
- <LinearLayout
- android:layout_below="@id/info_login"
- android:orientation="horizontal"
- android:weightSum="2"
- android:layout_margin="8dp"
- android:layout_width="match_parent"
- android:layout_height="wrap_content">
- <Button
- android:id="@+id/btn_sign_up"
- android:text="Sign up"
- style="@style/Widget.AppCompat.Button.Colored"
- android:layout_weight="1"
- android:layout_width="0dp"
- android:layout_height="wrap_content" />
- <Button
- android:id="@+id/btn_sign_in"
- android:text="Sign in"
- style="@style/Widget.AppCompat.Button.Colored"
- android:layout_weight="1"
- android:layout_width="0dp"
- android:layout_height="wrap_content" />
- </LinearLayout>
- </RelativeLayout>
- </android.support.constraint.ConstraintLayout>
- **************************************
- <?xml version="1.0" encoding="utf-8"?>
- <android.support.v7.widget.CardView
- xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_margin="8dp"
- app:cardElevation="4dp">
- <LinearLayout
- android:orientation="vertical"
- android:layout_width="match_parent"
- android:layout_height="wrap_content">
- <com.rengwuxian.materialedittext.MaterialEditText
- android:id="@+id/edtNewUserName"
- android:hint="User name"
- android:textColorHint="@color/colorPrimary"
- android:textColor="@color/colorPrimary"
- android:textSize="24sp"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- app:met_baseColor="@color/colorPrimary"
- app:met_floatingLabel="highlight"
- app:met_primaryColor="@color/colorPrimary"
- app:met_singleLineEllipsis="true"
- />
- <com.rengwuxian.materialedittext.MaterialEditText
- android:id="@+id/edtNewPassword"
- android:hint="Password"
- android:textColorHint="@color/colorPrimary"
- android:textColor="@color/colorPrimary"
- android:textSize="24sp"
- android:inputType="textPassword"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- app:met_baseColor="@color/colorPrimary"
- app:met_floatingLabel="highlight"
- app:met_primaryColor="@color/colorPrimary"
- app:met_singleLineEllipsis="true"
- />
- <com.rengwuxian.materialedittext.MaterialEditText
- android:id="@+id/edtNewEmail"
- android:hint="Email"
- android:textColorHint="@color/colorPrimary"
- android:textColor="@color/colorPrimary"
- android:textSize="24sp"
- android:inputType="textEmailAddress"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- app:met_baseColor="@color/colorPrimary"
- app:met_floatingLabel="highlight"
- app:met_primaryColor="@color/colorPrimary"
- app:met_singleLineEllipsis="true"
- />
- </LinearLayout>
- </android.support.v7.widget.CardView>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement