3

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"
            />
Hissaan Ali
  • 2,229
  • 4
  • 25
  • 51
  • I searched a solution for you and find out that to do this people always create a custom text view. Please have to look into it [Custom font for clock textview](https://stackoverflow.com/questions/36904466/custom-font-for-clock-textview) – Gk Mohammad Emon Dec 09 '19 at 11:57

2 Answers2

6

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!

Rahul Gaur
  • 1,661
  • 1
  • 13
  • 29
1

I just follow this to add font to Android https://developer.android.com/guide/topics/ui/look-and-feel/fonts-in-xml.html

  1. Copy .ttf / .otf file to "res/font" folder
  2. Generate font resource file with style and weight details.
  3. Set new font as android:fontFamily="@font/sample_font"

Check sample code below, with comparison with and without adding font

<!--With Font-->
<Chronometer
    android:id="@+id/chronometer"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/framelayout"
    android:layout_margin="5dp"
    android:fontFamily="@font/sample_font"
    android:textColor="@android:color/holo_blue_dark"
    android:textSize="30sp"
    app:layout_constraintBottom_toTopOf="@+id/textView"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.502"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/audio_player_chronometer"
    app:layout_constraintVertical_bias="0.528" />

<!--Without Font-->
<Chronometer
    android:id="@+id/audio_player_chronometer"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/framelayout"
    android:layout_margin="5dp"
    android:textSize="30sp"
    android:textColor="@android:color/holo_blue_dark"
    app:layout_constraintBottom_toTopOf="@+id/textView"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/framelayout" />

<!--sample text-->
<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!"
    android:fontFamily="@font/sample_font"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

Screenshot enter image description here

UdayaLakmal
  • 4,035
  • 4
  • 29
  • 40