Reputation: 322
I'm building the registration activity in which I use a textwatcher to see if email or password are empty, if all is correct is supposed to go through firebase auth and also set the opacity of register button to 100%, but it doesn't do anything
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_register)
val email = email_editText_register.text.toString()
val password = password_editText_register.text.toString()
registrar_button_register.alpha = 0.5f
registrar_button_register.addTextChangedListener(object : TextWatcher{
override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {
}
override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
val emailInput = email_editText_register.getText().toString().trim()
val passwordInput = password_editText_register.getText().toString().trim()
registrar_button_register.setEnabled(!emailInput.isNotEmpty() && !passwordInput.isNotEmpty())
}
override fun afterTextChanged(s: Editable) {
registrar_button_register.setOnClickListener {
registrar_button_register.animate().alpha(1f).setDuration(200)
FirebaseAuth.getInstance().createUserWithEmailAndPassword(email, password)
//createUserWithEmailAndPassword(email, password)
.addOnCompleteListener {
if (!it.isSuccessful) return@addOnCompleteListener
// Sign in success, update UI with the signed-in user's information
Log.d("register", "Successfully created with uid: ${it.result?.user?.uid}")
val intent = Intent(applicationContext, Inicio::class.java)
startActivity(intent)
}
.addOnFailureListener {
Log.d("main", "Failed to create user: ${it.message}")
Toast.makeText(getApplicationContext(), "the email already exist or it is not valid", Toast.LENGTH_SHORT).show()
}
}
}
})
this is the layout
<EditText
android:layout_width="0dp"
android:layout_height="40dp"
android:inputType="textPersonName"
android:ems="10"
android:paddingLeft="20dp"
android:id="@+id/username_editText_register"
android:hint="Usuario"
style="@android:style/Widget.AutoCompleteTextView"
android:background="#F3F3F3" android:textAlignment="viewStart" android:textIsSelectable="true"
android:textSize="14dp"
app:layout_constraintEnd_toEndOf="@+id/email_editText_register"
app:layout_constraintStart_toStartOf="@+id/email_editText_register" android:layout_marginTop="12dp"
app:layout_constraintTop_toBottomOf="@+id/email_editText_register"/>
<EditText
android:layout_width="0dp"
android:layout_height="40dp"
android:inputType="textEmailAddress"
android:ems="10"
android:paddingLeft="20dp"
android:id="@+id/email_editText_register"
android:hint="E-mail"
android:background="#F3F3F3" android:textSize="14dp"
android:layout_marginEnd="24dp"
app:layout_constraintEnd_toEndOf="parent" android:layout_marginStart="24dp"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintHorizontal_bias="1.0"
android:layout_marginTop="160dp" app:layout_constraintTop_toTopOf="parent"/>
<Button
android:text="Registrar"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#CC196AE0"
android:textColor="#FFFFFF" android:textStyle="bold" android:id="@+id/registrar_button_register"
app:layout_constraintStart_toStartOf="@+id/password_editText_register"
app:layout_constraintEnd_toEndOf="@+id/password_editText_register" android:layout_marginTop="12dp"
app:layout_constraintTop_toBottomOf="@+id/password_editText_register"/>
<TextView
android:text="Volver"
android:layout_width="0dp"
android:layout_height="50dp"
android:id="@+id/volver_textView_register" app:layout_constraintStart_toStartOf="@+id/registrar_button_register"
app:layout_constraintEnd_toEndOf="@+id/registrar_button_register"
android:layout_marginTop="14dp" app:layout_constraintTop_toBottomOf="@+id/registrar_button_register"
android:textStyle="bold" android:textSize="14dp" android:textAlignment="center"
android:textColor="#050505"/>
<EditText
android:layout_width="0dp"
android:layout_height="40dp"
android:inputType="textPassword"
android:ems="10"
android:paddingLeft="20dp"
android:id="@+id/password_editText_register" android:layout_marginTop="12dp"
app:layout_constraintTop_toBottomOf="@+id/username_editText_register"
app:layout_constraintEnd_toEndOf="@+id/username_editText_register"
app:layout_constraintStart_toStartOf="@+id/username_editText_register" android:background="#f3f3f3"
android:hint="Contraseña" android:textSize="14dp"/>
after the imputs are correct the button should be 100% oppacity, also the registration should work and then go to the next activity called "Inicio"
thank you for everyone who has help this is the final code with everything fixed just in case somebody need it:
override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState)
setContentView(R.layout.activity_register)
registrar_button_register.setEnabled(false)
registrar_button_register.animate().alpha(0.5f).setDuration(200)
val textWatcher = object : TextWatcher {
override fun afterTextChanged(p0: Editable?) {
}
override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
}
override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
val emailInput = email_editText_register.getText().toString().trim()
val passwordInput = password_editText_register.getText().toString().trim()
registrar_button_register.setEnabled(emailInput.isNotEmpty() && passwordInput.isNotEmpty())
val isEnabled = emailInput.isNotEmpty() && passwordInput.isNotEmpty()
registrar_button_register.setEnabled(isEnabled)
if (isEnabled) {
registrar_button_register.animate().alpha(1f).setDuration(200)
} else {
registrar_button_register.animate().alpha(0.5f).setDuration(200)
}
}
}
email_editText_register.addTextChangedListener(textWatcher)
password_editText_register.addTextChangedListener(textWatcher)
Toast.makeText(this, "d", Toast.LENGTH_SHORT).show()
registrar_button_register.setOnClickListener {
val email = email_editText_register.text.toString()
val password = password_editText_register.text.toString()
Log.d("Register", "Email is: " + email)
Log.d("Register", "password is: $password")
email_editText_register.addTextChangedListener(textWatcher)
password_editText_register.addTextChangedListener(textWatcher)
FirebaseAuth.getInstance().createUserWithEmailAndPassword(email, password)
//createUserWithEmailAndPassword(email, password)
.addOnCompleteListener {
if (!it.isSuccessful) return@addOnCompleteListener
// Sign in success, update UI with the signed-in user's information
Log.d("register", "Successfully created with uid: ${it.result?.user?.uid}")
Toast.makeText(this, "td", Toast.LENGTH_SHORT).show()
val intent = Intent(applicationContext, Inicio::class.java)
startActivity(intent)
}
.addOnFailureListener {
Log.d("main", "Failed to create user: ${it.message}")
Toast.makeText(this, "the email already exist or it is not valid", Toast.LENGTH_SHORT).show()
}
}
Upvotes: 0
Views: 121
Reputation: 1792
You are doing firebase authentication in asynchronous manner.So need to do firebase authentication in synchronous (using Task.wait()) manner.
Upvotes: 0
Reputation: 137
I think you got confused. I think you are using the addTextChangedListener on a Button, and not on a EdiText.
The addTextChangedListener method fires when text changes, and your button text never changes.
What you have to do is implement the same textChangedListener object on both email and password object.
Upvotes: 0
Reputation: 9352
You want to enable the button in case that both fields is not empty, so it should be:
registrar_button_register.setEnabled(emailInput.isNotEmpty() && passwordInput.isNotEmpty())
And you need to add textwatchers to edit text fields not button. You should create instance of TextWatcher and attach it to edittexts, some thing like this:
val textWatcher = object: TextWatcher {
override fun afterTextChanged(p0: Editable?) {
}
override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
}
override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
val emailInput = email_editText_register.getText().toString().trim()
val passwordInput = password_editText_register.getText().toString().trim()
val isEnabled = emailInput.isNotEmpty() && passwordInput.isNotEmpty()
registrar_button_register.setEnabled(isEnabled)
if(isEnabled) {
registrar_button_register.animate().alpha(1f).setDuration(200)
}
else {
registrar_button_register.animate().alpha(0.5f).setDuration(200)
}
}
}
email_editText_register.addTextChangedListener(textWatcher);
password_editText_register.addTextChangedListener(textWatcher);
registrar_button_register.setOnClickListener {
FirebaseAuth.getInstance().createUserWithEmailAndPassword(email, password)
//createUserWithEmailAndPassword(email, password)
.addOnCompleteListener {
if (!it.isSuccessful) return@addOnCompleteListener
// Sign in success, update UI with the signed-in user's information
Log.d("register", "Successfully created with uid: ${it.result?.user?.uid}")
val intent = Intent(applicationContext, Inicio::class.java)
startActivity(intent)
}
.addOnFailureListener {
Log.d("main", "Failed to create user: ${it.message}")
Toast.makeText(getApplicationContext(), "the email already exist or it is not valid", Toast.LENGTH_SHORT).show()
}
}
Upvotes: 1