3

I am using the latest version of the android material design library (1.2.0-alpha05). I have added app:suffixText="PlaceHolder" in TextInputLayout. I want suffixText to be displayed by default, but currently, it is visible only when TextInputLayout gains focus. When TextInputLayout loses focus, I can't see suffixText. Here is my code-:

<com.google.android.material.textfield.TextInputLayout
android:id="@+id/textField"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
app:suffixText="PlaceHolder"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:hint="hint">

         <com.google.android.material.textfield.TextInputEditText android:layout_width="match_parent"
android:layout_height="wrap_content" />

</com.google.android.material.textfield.TextInputLayout>

When I am launching the Activity, I can't see suffix text of TextInputLayout

Only after clicking on TextInputEditText, I can see suffix text

Please let me know, how can I display suffix text by default.

3 Answers3

7

You can use app:expandedHintEnabled="false" to display the suffix/prefix also when the text field is empty.

        <com.google.android.material.textfield.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:suffixText="...."
            app:expandedHintEnabled="false"
            >

            <com.google.android.material.textfield.TextInputEditText
                .../>

        </com.google.android.material.textfield.TextInputLayout>

enter image description here

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
1

I've just created an extension in kotlin:

fun TextInputLayout.keepSuffixVisible() {
    val suffixParent = this.suffixTextView.parent as View
    suffixParent.viewTreeObserver.addOnGlobalLayoutListener {
        if (suffixParent.visibility != View.VISIBLE) {
            suffixParent.visibility = View.VISIBLE
        }
    }
    this.suffixTextView.viewTreeObserver.addOnGlobalLayoutListener {
        if (this.suffixTextView.visibility != View.VISIBLE) {
            this.suffixTextView.visibility = View.VISIBLE
        }
    }
    this.suffixTextView.visibility = View.VISIBLE
}
betu
  • 21
  • 4
0

In code

inputLayout.suffixTextView.visibility = View.VISIBLE
inputLayout.addOnLayoutChangeListener { _, _, _, _, _, _, _, _, _ ->
    inputLayout.suffixTextView.visibility = View.VISIBLE
}