Home > Software engineering >  Can't update data in Firestore with TextInputLayout
Can't update data in Firestore with TextInputLayout

Time:01-29

I am using TextInputLayout on Edit Profile screen and I wanted to update datas in that screen but when I used that, datas update to like

"com.google.android.material.textfield.TextInputLayout{9544eb3 V.ED..... ........ 93,232-931,455 #7f0a00d8 app:id/etEditName aid=1073741880}"

I will leave my codes.

ProfileFragment.kt

....
    
    binding.btnEditProfile.setOnClickListener {
            val dialog =
                LayoutInflater.from(context).inflate(R.layout.edit_profile_dialog, null)
            val builder = AlertDialog.Builder(context).setView(dialog).show()
            builder.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
            builder.setCancelable(true)

            val etName = dialog.findViewById<TextInputLayout>(R.id.etEditName)
            etName.editText?.setText(user.name)
            val etSurname = dialog.findViewById<TextInputLayout>(R.id.etEditSurname)
            etSurname.editText?.setText(user.surname)
            val etWeight = dialog.findViewById<TextInputLayout>(R.id.etEditWeight)
            etWeight.editText?.setText(user.weight)
            val etHeight = dialog.findViewById<TextInputLayout>(R.id.etEditHeight)
            etHeight.editText?.setText(user.height)
            val etGoal = dialog.findViewById<TextInputLayout>(R.id.etEditGoal)
            etGoal.editText?.setText(user.calorieGoal.toString())

            dialog.findViewById<Button>(R.id.btnEditProfile).setOnClickListener {
                if (etSurname.isNotEmpty() && etHeight.isNotEmpty() && etWeight.isNotEmpty() && etGoal.isNotEmpty())
                    dbUser.document(auth.currentUser?.email.toString())
                        .update(
                            mapOf(
                                "name" to etName.toString(),
                                "surname" to etSurname.toString(),
                                "height" to etHeight.toString(),
                                "weight" to etWeight.toString(),
                            )
                        )
                        .addOnSuccessListener {
                            Toast.makeText(context, "Updated!!", Toast.LENGTH_SHORT).show()
                            builder.dismiss()
                        } else {
                    Toast.makeText(context, "Fill in the fields!!", Toast.LENGTH_SHORT).show()
                }
            }
        }

edit_dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<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"
    android:layout_marginHorizontal="36dp"
    android:background="@drawable/dialog_bg">

    <TextView
        android:id="@ id/txtEditProfile"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:fontFamily="monospace"
        android:text="@string/edit_profile"
        android:textColor="@color/primaryDarkColor"
        android:textSize="32sp"
        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/etEditName"
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginHorizontal="24dp"
        android:layout_marginTop="24dp"
        android:hint="@string/enter_name"
        android:maxLines="1"
        app:errorEnabled="true"
        app:hintTextColor="@color/gray"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/txtEditProfile">

        <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/etEditSurname"
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginHorizontal="24dp"
        android:layout_marginTop="24dp"
        android:hint="@string/enter_surname"
        android:maxLines="1"
        app:errorEnabled="true"
        app:hintTextColor="@color/gray"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/etEditName">

        <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/etEditWeight"
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
        android:layout_width="110dp"
        android:layout_height="wrap_content"
        android:layout_marginHorizontal="24dp"
        android:layout_marginTop="24dp"
        android:focusableInTouchMode="false"
        android:hint="@string/weight"
        android:maxLines="1"
        android:text=""
        app:errorEnabled="true"
        app:layout_constraintEnd_toStartOf="@ id/etEditHeight"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/etEditSurname">

        <com.google.android.material.textfield.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:cursorVisible="false"
            android:inputType="number"
            android:textColorHint="@color/primaryDarkColor" />
    </com.google.android.material.textfield.TextInputLayout>

    <com.google.android.material.textfield.TextInputLayout
        android:id="@ id/etEditGoal"
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
        android:layout_width="140dp"
        android:layout_height="wrap_content"
        android:layout_marginHorizontal="24dp"
        android:layout_marginTop="8dp"
        android:hint="@string/calorie_goal"
        android:maxLines="1"
        android:text=""
        app:errorEnabled="true"
        app:hintTextColor="@color/gray"
        app:layout_constraintEnd_toStartOf="@ id/etEditHeight"
        app:layout_constraintHorizontal_bias="0.508"
        app:layout_constraintStart_toEndOf="@ id/etEditWeight"
        app:layout_constraintTop_toBottomOf="@id/etEditHeight">

        <com.google.android.material.textfield.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:cursorVisible="false"
            android:inputType="number"
            android:textColorHint="@color/primaryDarkColor" />
    </com.google.android.material.textfield.TextInputLayout>

    <com.google.android.material.textfield.TextInputLayout
        android:id="@ id/etEditHeight"
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
        android:layout_width="110dp"
        android:layout_height="wrap_content"
        android:layout_marginHorizontal="24dp"
        android:layout_marginTop="24dp"
        android:hint="@string/height"
        android:maxLines="1"
        android:text=""
        app:errorEnabled="true"
        app:hintTextColor="@color/gray"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@ id/etEditWeight"
        app:layout_constraintTop_toBottomOf="@id/etEditSurname">

        <com.google.android.material.textfield.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:cursorVisible="false"
            android:inputType="number"
            android:textColorHint="@color/primaryDarkColor" />
    </com.google.android.material.textfield.TextInputLayout>


    <Button
        android:id="@ id/btnEditProfile"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:layout_margin="24dp"
        android:text="@string/submit"
        android:textColor="@color/secondaryTextColor"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/etEditGoal" />

</androidx.constraintlayout.widget.ConstraintLayout>

Thanks for your helps :)

CodePudding user response:

You are updating the TextInputLayout itself when you need to update the text in the EditText inside the TextInputLayout. So Replace

"name" to etName.toString(),
"surname" to etSurname.toString(),
"height" to etHeight.toString(),
"weight" to etWeight.toString(),

With

"name" to etName.editText?.text.toString(),
"surname" to etSurname.editText?.text.toString(),
"height" to etHeight.editText?.text.toString(),
"weight" to etWeight.editText?.text.toString(),

This will update user data in the Edit Profile screen.

  • Related