MainActivity
package com.arman.sqlite;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
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.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import com.arman.sqlite.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;
public class MainActivity extends AppCompatActivity {
Button loginIn, signIn;
RelativeLayout relativeLayout;
FirebaseAuth auth;
FirebaseDatabase db;
DatabaseReference users;
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
loginIn = findViewById(R.id.button_login);
signIn = findViewById(R.id.button_registration);
relativeLayout = findViewById(R.id.rootlayout);
auth = FirebaseAuth.getInstance();
db = FirebaseDatabase.getInstance();
users = db.getReference("Users");
loginIn.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
showLoginDialog();
}
});
signIn.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
showregisterDialog();
}
});
}
private void showLoginDialog() {
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setTitle("SIGN IN ");
dialog.setCancelable(false);
// dialog.setMessage("Please Use Email to Sign In");
LayoutInflater inflater = LayoutInflater.from(this);
View login_layout = inflater.inflate(R.layout.login, null);
final MaterialEditText editEmail = login_layout.findViewById(R.id.edit_email);
final MaterialEditText editPassword = login_layout.findViewById(R.id.edit_password);
dialog.setView(login_layout);
//set Button dialog.setPositiveButton("SIGN IN", new DialogInterface.OnClickListener() {
@Override public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
;
//Check validation if (TextUtils.isEmpty(editEmail.getText().toString())) {
Snackbar.make(relativeLayout, "Please enter email address", Snackbar.LENGTH_SHORT).show();
return;
}
if (TextUtils.isEmpty(editPassword.getText().toString())) {
Snackbar.make(relativeLayout, "Please enter Password", Snackbar.LENGTH_SHORT).show();
return;
}
if (editPassword.getText().toString().length() < 6) {
Snackbar.make(relativeLayout, "Password too short !!!", Snackbar.LENGTH_SHORT).show();
return;
}
//Login auth.signInWithEmailAndPassword(editEmail.getText().toString(), editPassword.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(relativeLayout, "Failed" + e.getMessage(), Snackbar.LENGTH_SHORT)
.show();
}
});
}
});
dialog.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
@Override public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
dialog.show();
}
private void showregisterDialog() {
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setCancelable(false);
dialog.setTitle("Register Your Account! ");
// dialog.setMessage("User Registration");
LayoutInflater inflater = LayoutInflater.from(this);
View register_layout = inflater.inflate(R.layout.register, null);
final MaterialEditText editEmail = register_layout.findViewById(R.id.edit_email);
final MaterialEditText editPassword = register_layout.findViewById(R.id.edit_password);
final MaterialEditText editName = register_layout.findViewById(R.id.edit_name);
final MaterialEditText editPhone = register_layout.findViewById(R.id.edit_phone);
dialog.setView(register_layout);
//set Button dialog.setPositiveButton("REGISTER", new DialogInterface.OnClickListener() {
@Override public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
//Check validation if (TextUtils.isEmpty(editEmail.getText().toString())) {
Snackbar.make(relativeLayout, "Please enter email address", Snackbar.LENGTH_SHORT).show();
return;
}
if (TextUtils.isEmpty(editPhone.getText().toString())) {
Snackbar.make(relativeLayout, "Please enter Phone Number", Snackbar.LENGTH_SHORT).show();
return;
}
if (TextUtils.isEmpty(editPassword.getText().toString())) {
Snackbar.make(relativeLayout, "Please enter Password", Snackbar.LENGTH_SHORT).show();
return;
}
if (editPassword.getText().toString().length() < 6) {
Snackbar.make(relativeLayout, "Password too short !!!", Snackbar.LENGTH_SHORT).show();
return;
}
//Register new Account auth.createUserWithEmailAndPassword(editEmail.getText().toString(), editPassword.getText().toString())
.addOnSuccessListener(new OnSuccessListener<AuthResult>() {
@Override public void onSuccess(AuthResult authResult) {
//Save user to db User user = new User();
user.setEmail(editEmail.getText().toString());
user.setName(editName.getText().toString());
user.setPassword(editPassword.getText().toString());
user.setPhone(editPhone.getText().toString());
//Use email to key users.child(FirebaseAuth.getInstance().getCurrentUser().getUid())
.setValue(user)
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override public void onSuccess(Void aVoid) {
Snackbar.make(relativeLayout, "Register SuccessFully", Snackbar.LENGTH_SHORT).show();
}
})
.addOnFailureListener(new OnFailureListener() {
@Override public void onFailure(@NonNull Exception e) {
Snackbar.make(relativeLayout, "Failed " + e.getMessage(), Snackbar.LENGTH_SHORT).show();
}
});
}
})
.addOnFailureListener(new OnFailureListener() {
@Override public void onFailure(@NonNull Exception e) {
Snackbar.make(relativeLayout, "Failed " + e.getMessage(), Snackbar.LENGTH_SHORT).show();
}
});
}
});
dialog.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
@Override public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
dialog.show();
}
}
User ( Model )
package com.arman.sqlite.Model;
public class User {
private String email,password,name,phone;
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 getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
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:background="@drawable/backg" android:id="@+id/rootlayout" android:layout_height="match_parent" tools:context=".MainActivity">
<LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent">
<TextView android:gravity="center" android:id="@+id/textView" android:textSize="25sp" android:textAllCaps="true" android:layout_margin="20sp" android:textColor="#0CB4D6" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Kapse Uber" />
<LinearLayout
android:layout_marginTop="100sp" android:layout_marginBottom="100sp" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="250sp">
<ImageView android:layout_width="match_parent" android:layout_height="wrap_content" android:scaleType="fitXY" android:src="@drawable/uber" />
</LinearLayout>
<LinearLayout android:gravity="center" android:layout_marginTop="20sp" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content">
<Button android:layout_marginRight="10sp" android:layout_marginEnd="10sp" android:text="Login" android:background="#009688" android:id="@+id/button_login" android:layout_width="wrap_content" android:layout_height="wrap_content" />
<Button android:id="@+id/button_registration" android:layout_width="120dp" android:layout_height="wrap_content" android:layout_marginLeft="10sp" android:background="#CDDC39" android:text="Registration" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
Login .XML
<?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="match_parent" app:cardElevation="10dp">
<LinearLayout android:orientation="vertical" android:layout_margin="20sp" android:layout_width="match_parent" android:layout_height="wrap_content">
<com.rengwuxian.materialedittext.MaterialEditText android:id="@+id/edit_email" android:hint="Email:" android:textSize="20sp" android:textColor="#2196F3" android:textColorHint="#4CAF50" android:layout_width="match_parent" android:layout_height="wrap_content" />
<com.rengwuxian.materialedittext.MaterialEditText android:id="@+id/edit_password" android:hint="Password:" android:textSize="20sp" android:textColor="#2196F3" android:textColorHint="#4CAF50" android:layout_width="match_parent" android:layout_height="wrap_content" />
</LinearLayout>
</android.support.v7.widget.CardView>
Register.xml
<?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="match_parent" app:cardElevation="10dp">
<LinearLayout android:orientation="vertical" android:layout_margin="20sp" android:layout_width="match_parent" android:layout_height="wrap_content">
<com.rengwuxian.materialedittext.MaterialEditText android:id="@+id/edit_email" android:hint="Email:" android:textSize="20sp" android:textColor="#2196F3" android:textColorHint="#4CAF50" android:layout_width="match_parent" android:layout_height="wrap_content" />
<com.rengwuxian.materialedittext.MaterialEditText android:id="@+id/edit_password" android:hint="Password:" android:textSize="20sp" android:textColor="#2196F3" android:textColorHint="#4CAF50" android:layout_width="match_parent" android:layout_height="wrap_content" />
<com.rengwuxian.materialedittext.MaterialEditText android:id="@+id/edit_name" android:hint="Name:" android:textSize="20sp" android:textColor="#2196F3" android:textColorHint="#4CAF50" android:layout_width="match_parent" android:layout_height="wrap_content" />
<com.rengwuxian.materialedittext.MaterialEditText android:id="@+id/edit_phone" android:hint="Phone:" android:textSize="20sp" android:textColor="#2196F3" android:textColorHint="#4CAF50" android:layout_width="match_parent" android:layout_height="wrap_content" />
</LinearLayout>
</android.support.v7.widget.CardView>
// Adding Gradleimplementation 'com.rengwuxian.materialedittext:library:2.1.4'implementation 'com.android.support:cardview-v7:28.0.0'implementation 'com.android.support:design:28.0.0'
//Adding FireBaseimplementation 'com.google.firebase:firebase-auth:16.1.0'implementation 'com.google.firebase:firebase-database:16.0.6'
0 Comments