I'm trying to make sign up page in firebase database i have done all the implementation regarding firebase but
task is not successfull in this case i don't know how but i
t does not go to if condition it only goes to else condition
if(task.isSuccessful()){
user User=new user(name,profession,email,password);
String id=task.getResult().getUser().getUid();
database.getReference().child("users").child(id).setValue(User);
Toast.makeText(signup.this, "signup successfully", Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(signup.this, "failed", Toast.LENGTH_SHORT).show();
}
it does not go to if condition it only goes to else condition
signup.java
package com.example.instagram;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.example.instagram.databinding.ActivitySignupBinding;
import com.example.instagram.model.user;
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.FirebaseDatabase;
public class signup extends AppCompatActivity {
TextView signup;
Button signupbtn;
ActivitySignupBinding binding;
FirebaseDatabase database;
FirebaseAuth auth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_signup);
binding=ActivitySignupBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
signup=findViewById(R.id.signup);
signupbtn=findViewById(R.id.signupbtn);
auth=FirebaseAuth.getInstance();
database=FirebaseDatabase.getInstance();
String email=binding.email.getText().toString();
String password=binding.pwd.getText().toString();
String name=binding.Name.getText().toString();
String profession=binding.profession.getText().toString();
signupbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String email=binding.email.getText().toString();
String password=binding.pwd.getText().toString();
auth.createUserWithEmailAndPassword(email,password).
addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if(task.isSuccessful()){
user User=new user(name,profession,email,password);
String id=task.getResult().getUser().getUid();
database.getReference().child("users").child(id).setValue(User);
Toast.makeText(signup.this, "signup successfully", Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(signup.this, "failed", Toast.LENGTH_SHORT).show();
}
}
});
}
});
signup.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent=new Intent(signup.this,loginpage.class);
startActivity(intent);
}
});
}
}
signup.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"
tools:context=".loginpage">
<TextView
android:id="@ id/textView14"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="Instagram"
android:textSize="30dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@ id/imageView3"
android:layout_width="223dp"
android:layout_height="195dp"
android:layout_marginTop="4dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@ id/textView14"
app:srcCompat="@drawable/instagram" />
<TextView
android:id="@ id/textView16"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Welcome to social world"
android:textSize="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@ id/imageView3" />
<EditText
android:id="@ id/Name"
android:layout_width="297dp"
android:layout_height="49dp"
android:layout_marginTop="16dp"
android:background="@drawable/inputbg"
android:ems="10"
android:hint="Name"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.491"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@ id/textView16" />
<EditText
android:id="@ id/profession"
android:layout_width="297dp"
android:layout_height="49dp"
android:layout_marginTop="12dp"
android:background="@drawable/inputbg"
android:ems="10"
android:hint="profession"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@ id/Name" />
<EditText
android:id="@ id/email"
android:layout_width="297dp"
android:layout_height="49dp"
android:layout_marginTop="8dp"
android:background="@drawable/inputbg"
android:ems="10"
android:hint="Email"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.491"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@ id/profession" />
<EditText
android:id="@ id/pwd"
android:layout_width="297dp"
android:layout_height="49dp"
android:background="@drawable/inputbg"
android:ems="10"
android:hint="Password"
android:inputType="textPersonName"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.491"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@ id/email"
app:layout_constraintVertical_bias="0.063" />
<Button
android:id="@ id/signupbtn"
android:layout_width="298dp"
android:layout_height="51dp"
android:layout_marginTop="32dp"
android:text="sign up"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.504"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@ id/pwd" />
<TextView
android:id="@ id/textView15"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Already have a account? "
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@ id/signup"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@ id/signupbtn"
app:layout_constraintVertical_bias="0.859" />
<TextView
android:id="@ id/signup"
android:layout_width="63dp"
android:layout_height="18dp"
android:text="Log In!"
android:textColor="#F10606"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@ id/textView15"
app:layout_constraintTop_toBottomOf="@ id/signupbtn"
app:layout_constraintVertical_bias="0.86" />
</androidx.constraintlayout.widget.ConstraintLayout>
build.gradle(module level)
plugins {
id 'com.android.application'
}
android {
compileSdk 32
defaultConfig {
applicationId "com.example.instagram"
minSdk 21
targetSdk 32
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
buildFeatures{
viewBinding true
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
implementation 'androidx.appcompat:appcompat:1.5.1'
implementation 'com.google.android.material:material:1.7.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
testImplementation 'junit:junit:4.13.2'
implementation 'de.hdodenhof:circleimageview:3.1.0'
implementation'com.github.florent37:diagonallayout:1.0.9'
androidTestImplementation 'androidx.test.ext:junit:1.1.4'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.0'
implementation 'com.hardik.hsdp:scalablesize-android:1.0.1'
implementation 'com.makeramen:roundedimageview:2.3.0'
implementation 'com.github.MrNouri:DynamicSizes:1.0'
implementation platform('com.google.firebase:firebase-bom:31.1.1')
implementation 'com.google.firebase:firebase-auth'
implementation 'com.google.firebase:firebase-database'
implementation 'com.google.firebase:firebase-core:9.6.1'
}
apply plugin: 'com.google.gms.google-services'
help to out be get if condition runs successfully
CodePudding user response:
You may have not started Authentication