Ben
Ben

Reputation: 1849

Different font family for EditText hint and text

I have created a layout with multiple EditTexts where all of them are the same font family.

One of the EditTexts should show a quote and I had like it to have a different font than the other once I type = the hint should be same for all but once typed to change the font of this specific EditText.

Is there any way to do it?

This is how my EditText is built:

<EditText
    android:id="@+id/et_Where"
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:layout_marginStart="30dp"
    android:layout_marginTop="16dp"
    android:layout_marginEnd="30dp"
    android:background="@drawable/btn_underline_gray"
    android:fontFamily="@font/assistant_semibold"
    android:hint="@string/ActivityAdd_WhereHint"
    android:inputType="text"
    android:textColor="@color/colorBlackText"
    android:textSize="14sp"
    app:layout_constraintTop_toBottomOf="@+id/et_Source" />

Thank you

Upvotes: 0

Views: 436

Answers (2)

SerjantArbuz
SerjantArbuz

Reputation: 1244

Kotlin solution:

enter.doAfterTextChanged {
    val text = it?.toString() ?: return@doAfterTextChanged

    /** Sent font only for hint. */
    val font = if (text.isEmpty()) resources.getFont(R.font.your_font) else null
    enter.typeface = font
}

Setup font only when enter text is empty (for hint only).

Upvotes: 0

snachmsm
snachmsm

Reputation: 19263

maybe consider using TextWatcher - detect when EditText is empty or not and exchange fontFamily. some example of TextWatcher registration in HERE

be aware of known bug with loading fontFamily during runtime, it may cause memory leak. check out some workarounds in HERE

Upvotes: 0

Related Questions