Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.ilham.dompettebal;
- import android.content.Intent;
- import android.service.autofill.UserData;
- import android.support.annotation.NonNull;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.text.TextUtils;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.ProgressBar;
- 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.database.DatabaseReference;
- import com.google.firebase.database.FirebaseDatabase;
- import org.w3c.dom.Text;
- import java.util.HashMap;
- public class register extends AppCompatActivity {
- private EditText Inputemail,Inputusername,Inputpassword;
- private TextView signin;
- private Button buttonRegister;
- private FirebaseAuth auth;
- private DatabaseReference users;
- String userId;
- // private ProgressBar progressBar;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_register);
- auth = FirebaseAuth.getInstance();
- userId = auth.getCurrentUser().getUid();
- users = FirebaseDatabase.getInstance().getReference().child("users").child(userId);
- buttonRegister = (Button) findViewById(R.id.buttonRegister);
- Inputemail = (EditText) findViewById(R.id.email);
- Inputpassword = (EditText) findViewById(R.id.password);
- Inputusername = (EditText) findViewById(R.id.username);
- signin = (TextView) findViewById(R.id.signin) ;
- signin.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- startActivity(new Intent(register.this, login.class));
- }
- });
- buttonRegister.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- String email = Inputemail.getText().toString().trim();
- String password = Inputpassword.getText().toString().trim();
- String username = Inputusername.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 {
- SavedUser();
- startActivity(new Intent(register.this, MainActivity.class));
- finish();
- }
- }
- });
- }
- });
- }
- private void SavedUser()
- {
- Inputemail = (EditText) findViewById(R.id.email);
- Inputpassword = (EditText) findViewById(R.id.password);
- Inputusername = (EditText) findViewById(R.id.username);
- String email = Inputemail.getText().toString().trim();
- String password = Inputpassword.getText().toString().trim();
- String username = Inputusername.getText().toString().trim();
- HashMap userData = new HashMap();
- userData.put("username",username);
- userData.put("email",email);
- users.updateChildren(userData).addOnCompleteListener(new OnCompleteListener() {
- @Override
- public void onComplete(@NonNull Task task) {
- if(task.isSuccessful())
- {
- Toast.makeText(register.this, "User Created" + task.getException(),
- Toast.LENGTH_SHORT).show();
- }
- }
- });
- }
- }
- apply plugin: 'com.android.application'
- apply plugin: 'com.google.gms.google-services'
- android {
- compileSdkVersion 28
- defaultConfig {
- applicationId "com.example.ilham.dompettebal"
- minSdkVersion 15
- targetSdkVersion 28
- versionCode 1
- versionName "1.0"
- testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
- }
- buildTypes {
- release {
- minifyEnabled false
- proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
- }
- }
- }
- dependencies {
- implementation fileTree(dir: 'libs', include: ['*.jar'])
- //noinspection GradleCompatible
- implementation 'com.android.support:appcompat-v7:28.0.0'
- implementation 'com.android.support.constraint:constraint-layout:1.1.3'
- testImplementation 'junit:junit:4.12'
- androidTestImplementation 'com.android.support.test:runner:1.0.2'
- androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
- implementation 'com.google.firebase:firebase-core:16.0.1'
- implementation 'com.google.firebase:firebase-storage:16.0.1'
- implementation 'com.google.firebase:firebase-auth:16.0.1'
- implementation 'com.google.firebase:firebase-database:16.0.1'
- }
Add Comment
Please, Sign In to add comment