Search code examples
androidxmlandroid-layoutchronometer

Chronometer font doesn't change in android


I want to change the font of my chronometer in android.I have set my default font to be a custom font . The font seems to be applied every where except the chronometer. I have also tried setting android:fontFamily="@font/myfont" but it didn't work. Though the font appears to be applied in the preview window, but it doesn't change when i run my app on my device.

Any idea to get over it ?

Here's my code:

<Chronometer
            android:fontFamily="@font/linotte_semibold"
            android:id="@+id/audio_player_chronometer"
            android:layout_width="wrap_content"
            android:layout_height="20dp"
            android:layout_below="@+id/frame_layout"
            android:textColor="@color/white"
            android:layout_margin="5dp"
            />

Solution

  • You can download Fonts from Google Material

    1. Go to layout editor
    2. Select your view and view All Attributes on your right side and find fontFamily

    fontFamily Attribute with default font

    1. Now click down arrow, it will open a window for selecting font

    Resources of Google Fonts

    1. Find your desired font, you can also search and click ok

    2. Your Chronometer will look differently

    Preview of changed font

    Here is the code

    <Chronometer
        android:id="@+id/chronometer"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:fontFamily="@font/allan_bold"
        android:textColor="@color/colorAccent"
        android:textSize="30sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    

    If in case font does not change use this in your Activity.class

    Chronometer chronometer = findViewById(R.id.chronometer);
    
    chronometer.setTypeface(ResourcesCompat.getFont(this, R.font.allan_bold));
    //replace allan_bold with your desired font 
    

    Edit 1 Here is the Output in the device Device output

    Hope this will help!