skip to Main Content

I want to make a custom dialog that I can re-use to configure some parameters. I added a default hint that I want to modify each time the user opens the Dialog.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="@dimen/space_default">
    <com.google.android.material.textfield.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense">
        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/etValue"
            android:layout_width="match_parent"
            android:hint="default"
            android:layout_height="wrap_content"/>
    </com.google.android.material.textfield.TextInputLayout>
</LinearLayout>

When I set the Hint programmatically, the default hint is not modified and a second hint is added.

binding.clDireccion.setOnClickListener {
    val dialogView = layoutInflater.inflate(R.layout.dialog_configuration, null)
    val valueEditText = dialogView.findViewById<TextInputEditText>(R.id.etValue)
    
    valueEditText.setHint("Direccion")
    
    MaterialAlertDialogBuilder(this)
        .setView(dialogView)
        .setCancelable(false)
        .setPositiveButton("Modificar") { dialogInterface, i ->
            val textValue = valueEditText.text.toString()
            with(preference.edit()) {
                putString(getString(R.string.sp_direccion), textValue).apply()
            }
        }
        .setNegativeButton("Cancelar", null)
        .show();
}

This is the result:

enter image description here

enter image description here

My expected result is to modify the default hint and I also want that when the EditText is focused the Hint moves to the top like the default.

Thanks in advance!

2

Answers


  1. You have to add id and hint to your TextInputlayout:

    <com.google.android.material.textfield.TextInputLayout
        ...
        android:id="@+id/til_value"
        android:hint="@string/default">
    
        <com.google.android.material.textfield.TextInputEditText
            ...
        />
    </com.google.android.material.textfield.TextInputLayout>
    

    Then in code change hint of TextInputlayout ( in example used View Binding):

    binding.tilValue.hint = "Some new hint"
    
    Login or Signup to reply.
  2. Due to the TextInputLayout’s documentation:

    The hint should be set on the TextInputLayout, rather than the EditText. If a hint is specified on the child EditText in XML, the TextInputLayout might still work correctly; TextInputLayout will use the EditText’s hint as its floating label. However, future calls to modify the hint will not update TextInputLayout’s hint. To avoid unintended behavior, call setHint(CharSequence) and getHint() on TextInputLayout, instead of on EditText.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search