set font for the hint TextInputLayout in Kotlin Android Studio

99 Views Asked by At

I work with Kotlin and android studio

        <com.google.android.material.textfield.TextInputLayout
            android:hint="شماره تلفن همراه"
            style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense"
            android:layout_marginStart="5dp"
            android:layout_marginEnd="5dp"
            android:layout_marginTop="5dp"
            app:shapeAppearanceOverlay="@style/button4"
            app:boxStrokeColor="#4E4A4A"
            app:boxStrokeWidth="1dp"
            app:prefixText="+98"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <com.google.android.material.textfield.TextInputEditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:inputType="phone"
                android:id="@+id/phoneNumber"
                android:ems="10"
                android:textSize="13sp"
                android:textColor="@color/black"
                android:textStyle="normal"
                />
        </com.google.android.material.textfield.TextInputLayout>

enter image description here

I want to set a font for the hint (شماره تلفن) and I have a font in res/font ; how can I set that font?

2

There are 2 best solutions below

0
ItzDavi On BEST ANSWER

Super easy.

In the XML of your TextInputEditText add android:fontFamily="font-family".

<com.google.android.material.textfield.TextInputLayout
        android:hint="شماره تلفن همراه"
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense"
        android:layout_marginStart="5dp"
        android:layout_marginEnd="5dp"
        android:layout_marginTop="5dp"
        app:shapeAppearanceOverlay="@style/button4"
        app:boxStrokeColor="#4E4A4A"
        app:boxStrokeWidth="1dp"
        app:prefixText="+98"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <com.google.android.material.textfield.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="phone"
            android:id="@+id/phoneNumber"
            android:ems="10"
            android:fontFamily="YOUR FONT NAME HERE" <--- THIS
            android:textSize="13sp"
            android:textColor="@color/black"
            android:textStyle="normal"
            />
    </com.google.android.material.textfield.TextInputLayout>

If this doesn't help, please let me know and I'll provide more support, happy coding!

0
Ajit Patel On

Method 1 - By using fontFamily atrribute

 <com.google.android.material.textfield.TextInputLayout
        android:hint="شماره تلفن همراه"
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense"
        android:layout_marginStart="5dp"
        android:layout_marginEnd="5dp"
        android:layout_marginTop="5dp"
        app:shapeAppearanceOverlay="@style/button4"
        app:boxStrokeColor="#4E4A4A"
        app:boxStrokeWidth="1dp"
        app:prefixText="+98"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <com.google.android.material.textfield.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="phone"
            android:id="@+id/phoneNumber"
            android:ems="10"
            android:textSize="13sp"
            android:textColor="@color/black"
            android:textStyle="normal"
            app:fontFamily="@font/myfont"
            />
    </com.google.android.material.textfield.TextInputLayout>

Method 2 -

To change the font of the hint in a TextInputLayout, you can use the setTypeface() method on the TextInputLayout itself. Here's how you can achieve this:

Add Font File: Place your custom font file (e.g., myfont.ttf) in the res/font folder of your Android project.

Programmatically Set Font for Hint: In your activity or fragment code, find the TextInputLayout by its ID and set a custom font for the hint:

import android.graphics.Typeface;
import androidx.appcompat.app.AppCompatActivity;
import com.google.android.material.textfield.TextInputLayout;

public class YourActivity extends AppCompatActivity {

  @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);

      TextInputLayout textInputLayout = findViewById(R.id.textInputLayout);

      Typeface customFontForHint = Typeface.createFromAsset(getAssets(), 
      "font/myfont.ttf");
      textInputLayout.setTypeface(customFontForHint);
   }
 }

Replace "font/myfont.ttf" with the actual path to your font file.

XML Layout: Your XML layout remains the same, just include the TextInputLayout and TextInputEditText as you've defined:

<com.google.android.material.textfield.TextInputLayout
android:id="@+id/textInputLayout"
android:hint="شماره تلفن همراه"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense"
<!-- Other attributes -->
android:layout_width="match_parent"
android:layout_height="match_parent">

   <com.google.android.material.textfield.TextInputEditText
        <!-- Other attributes -->
        android:id="@+id/phoneNumber" />
</com.google.android.material.textfield.TextInputLayout>

By using the setTypeface() method on the TextInputLayout, you can change the font of the hint while keeping the user-entered text in the default font.

Hope these methods resolve your problem. Let me know if any other help required, I will be more than happy to help.