Search code examples
android-studioprefixandroid-textinputlayouttextinputlayout

Always make the prefix text in text input layout visible like the hint


I am having a prefix text in a Text Input Layout but it only shows when i click inside the Text Input Edit Text which is inside the Text Input Layout that has my prefix text. How can i make the prefix text to always show with the hint

Below is my Text Input Edit Text


<com.google.android.material.textfield.TextInputLayout
                android:id="@+id/til_number"
                style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/til_country"
                android:layout_marginStart="30dp"
                android:layout_marginLeft="30dp"
                android:layout_marginTop="10dp"
                android:layout_marginEnd="30dp"
                android:layout_marginRight="30dp"
                android:layout_marginBottom="10dp"
                android:padding="5dp"
                app:boxBackgroundColor="#EFEFF2"
                app:prefixText="+01&#160;-&#160;"
                app:shapeAppearanceOverlay="@style/RoundedTextInputLayout">

                <com.google.android.material.textfield.TextInputEditText
                    android:id="@+id/et_first_name"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:hint="@string/number"
                    android:inputType="phone"
                    android:maxLength="100"
                    android:textSize="15sp" />

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


What i would like to achieve is

+01&#160;-&#160;

the above text in prefix should always show

i have tried doing the following below


app:prefixTextColor="@color/secondaryDarkColor"


Solution

  • If you check the logic of the view, you will see the prefix text will be hidden if:

    prefixTextView.setVisibility((prefixText != null && !isHintExpanded()) ? VISIBLE : GONE);

    Add app:expandedHintEnabled="false" to your TextInputLayout and your prefix text will stay visible:

        <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/text_input_layout1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:layout_marginTop="16dp"
            android:layout_marginEnd="16dp"
            app:expandedHintEnabled="false"
            app:prefixText="MyPrefix: ">
    
            <com.google.android.material.textfield.TextInputEditText
                android:id="@+id/text_input_edit_text1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="My optional Hint" />
    
        </com.google.android.material.textfield.TextInputLayout>
    

    Note: Of course the hint will show on top like this.