I am using Firebase Auth and Firestore on my app and I using TextInputLayout for Login and Register screens but I got a problem it's like exception named "The email address is badly formatted" when I want to add new user from Register screen. I looked up but I can't find any Kotlin question about this problem I'll leave my codes below. I'm waiting your help. Have a good Codes :) .
LoginActivity.kt
class LoginActivity : AppCompatActivity() {
private val db = Firebase.firestore.collection("users")
private val auth = Firebase.auth
private lateinit var binding: ActivityLoginBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = DataBindingUtil.setContentView(this, R.layout.activity_login)
binding.tvRegister.setOnClickListener {
val dialog = BottomSheetDialog(this@LoginActivity)
val view = layoutInflater.inflate(R.layout.bottom_sheet_layout, null)
dialog.setContentView(view)
val etName = view.findViewById<TextInputLayout>(R.id.etName).toString()
val etSurname = view.findViewById<TextInputLayout>(R.id.etSurname).toString()
val etMail = view.findViewById<TextInputLayout>(R.id.etRegisterEmail).toString()
val etPassword = view.findViewById<TextInputLayout>(R.id.etRegisterPassword).toString()
val etHeight = view.findViewById<TextInputLayout>(R.id.etHeight).toString()
val etWeight = view.findViewById<TextInputLayout>(R.id.etWeight).toString()
val btnRegister = view.findViewById<Button>(R.id.btnRegister)
btnRegister.setOnClickListener {
val user = hashMapOf<Any, String>(
"name" to etName,
"surname" to etSurname,
"email" to etMail,
"password" to etPassword,
"height" to etHeight,
"weight" to etWeight
)
if (etName.isNotEmpty() && etSurname.isNotEmpty() && etMail.isNotEmpty() && etPassword.isNotEmpty()) {
registerUser(etMail,etPassword,user)
} else {
Toast.makeText(
this@LoginActivity,
"You have to fill blanks",
Toast.LENGTH_SHORT
).show()
}
}
dialog.show()
}
}
private fun registerUser(email: String, password: String, user: HashMap<Any, String>) {
CoroutineScope(Dispatchers.IO).launch {
try {
auth.createUserWithEmailAndPassword(email, password)
.addOnSuccessListener {
db.document(auth.currentUser?.email.toString()).set(user)
.addOnSuccessListener {
Toast.makeText(this@LoginActivity, "Welcome", Toast.LENGTH_LONG)
.show()
checkLogged()
}
.addOnFailureListener {
Toast.makeText(this@LoginActivity, it.message, Toast.LENGTH_LONG)
.show()
}
}.await()
} catch (e: java.lang.Exception) {
withContext(Dispatchers.Main){
Toast.makeText(this@LoginActivity, e.message, Toast.LENGTH_LONG).show()
}
}
}
}
private fun checkLogged() {
if (auth.currentUser != null) {
startActivity(Intent(this@LoginActivity, MainActivity::class.java))
finish()
} else {
auth.signOut()
}
}
}
bottom_sheet_layout.xml (Register Screen)
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@ id/tvRegisterTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:fontFamily="monospace"
android:text="Register"
android:textColor="@color/primaryDarkColor"
android:textSize="36sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.google.android.material.textfield.TextInputLayout
android:id="@ id/etName"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="48dp"
android:layout_marginTop="24dp"
android:hint="@string/name"
app:errorEnabled="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tvRegisterTitle">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:inputType="textPersonName"
android:textColorHint="@color/primaryDarkColor" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="@ id/etSurname"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="48dp"
android:layout_marginTop="8dp"
android:hint="@string/surname"
app:errorEnabled="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/etName">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:inputType="textPersonName"
android:textColorHint="@color/primaryDarkColor" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="@ id/etRegisterEmail"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="48dp"
android:layout_marginTop="8dp"
android:hint="@string/e_mail"
app:endIconMode="clear_text"
app:endIconTint="@color/secondaryColor"
app:errorEnabled="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/etSurname">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:inputType="textPersonName"
android:textColorHint="@color/secondaryDarkColor" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="@ id/etRegisterPassword"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="48dp"
android:layout_marginTop="8dp"
android:hint="@string/password"
app:endIconMode="password_toggle"
app:endIconTint="@color/secondaryColor"
app:errorEnabled="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/etRegisterEmail">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:inputType="textPassword"
android:textColorHint="@color/secondaryDarkColor" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="@ id/etHeight"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="110dp"
android:layout_height="80dp"
android:layout_marginTop="8dp"
android:hint="@string/height"
app:errorEnabled="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/etWeight"
app:layout_constraintTop_toBottomOf="@id/etRegisterPassword">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:inputType="number"
android:textColorHint="@color/secondaryDarkColor" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="@ id/etWeight"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="110dp"
android:layout_height="80dp"
android:layout_marginTop="8dp"
android:hint="@string/weight"
app:errorEnabled="true"
app:layout_constraintEnd_toStartOf="@id/etHeight"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/etRegisterPassword">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:inputType="number"
android:textColorHint="@color/secondaryDarkColor" />
</com.google.android.material.textfield.TextInputLayout>
<Button
android:id="@ id/btnRegister"
style="?attr/materialButtonOutlinedStyle"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_marginVertical="16sp"
android:backgroundTint="@color/primaryColor"
android:text="@string/register"
android:textColor="@color/secondaryTextColor"
app:layout_constraintBottom_toTopOf="@ id/imageView2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/etWeight" />
<ImageView
android:id="@ id/imageView2"
android:layout_width="70dp"
android:layout_height="70dp"
android:rotation="26"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@ id/imageView3"
app:layout_constraintStart_toStartOf="parent"
app:srcCompat="@drawable/broccoli_png" />
<ImageView
android:id="@ id/imageView3"
android:layout_width="70dp"
android:layout_height="70dp"
android:rotation="26"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@ id/imageView4"
app:layout_constraintStart_toEndOf="@ id/imageView2"
app:srcCompat="@drawable/broccoli_png" />
<ImageView
android:id="@ id/imageView4"
android:layout_width="70dp"
android:layout_height="70dp"
android:rotation="26"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@ id/imageView3"
app:srcCompat="@drawable/broccoli_png" />
</androidx.constraintlayout.widget.ConstraintLayout>
CodePudding user response:
Use this :
auth.currentUser?.email.toString().trim()
Dont forget to add trim()
CodePudding user response:
I still haven't solved the problem but I changed somethings in code
1- Changed hash state to User named data class
2- I define etMail like:
val etMail =view.findViewById<TextInputLayout>(R.id.etRegisterEmail).editText?.text.toString()
3- And I carried defined properties to inside of btnRegister.setOnClickListener
Final Version of Code
class LoginActivity : AppCompatActivity() {
private val db = Firebase.firestore.collection("users")
private val auth = Firebase.auth
private lateinit var binding: ActivityLoginBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = DataBindingUtil.setContentView(this, R.layout.activity_login)
binding.tvRegister.setOnClickListener {
val dialog = BottomSheetDialog(this@LoginActivity)
val view = layoutInflater.inflate(R.layout.bottom_sheet_layout, null)
dialog.setContentView(view)
val btnRegister = view.findViewById<Button>(R.id.btnRegister)
btnRegister.setOnClickListener {
val etName =
view.findViewById<TextInputLayout>(R.id.etName).editText?.text.toString()
val etSurname =
view.findViewById<TextInputLayout>(R.id.etSurname).editText?.text.toString()
val etMail =
view.findViewById<TextInputLayout>(R.id.etRegisterEmail).editText?.text.toString()
val etPassword =
view.findViewById<TextInputLayout>(R.id.etRegisterPassword).editText?.text.toString()
val etHeight =
view.findViewById<TextInputLayout>(R.id.etHeight).editText?.text.toString()
val etWeight =
view.findViewById<TextInputLayout>(R.id.etWeight).editText?.text.toString()
val user = User(etName, etSurname, etMail, etHeight, etWeight)
registerUser(etMail, etPassword, user)
}
dialog.show()
}
}
private fun registerUser(email: String, password: String, user: User) {
if(email.isNotEmpty()&&password.isNotEmpty()){
CoroutineScope(Dispatchers.IO).launch {
try {
auth.createUserWithEmailAndPassword(email, password)
.addOnSuccessListener {
db.document(auth.currentUser?.uid.toString()).set(user)
checkLogged()
Toast.makeText(this@LoginActivity,"Welcome",Toast.LENGTH_SHORT).show()
}.await()
} catch (e: java.lang.Exception) {
withContext(Dispatchers.Main) {
Toast.makeText(this@LoginActivity, e.message, Toast.LENGTH_LONG).show()
}
}
}
}
}
private fun checkLogged() {
if (auth.currentUser != null) {
startActivity(Intent(this@LoginActivity, MainActivity::class.java))
finish()
} else {
auth.signOut()
}
}
}