How to Change Fontfamily of Textview in Android

How to change fontFamily of TextView in Android

From android 4.1 / 4.2 / 5.0, the following Roboto font families are available:

android:fontFamily="sans-serif"           // roboto regular
android:fontFamily="sans-serif-light" // roboto light
android:fontFamily="sans-serif-condensed" // roboto condensed
android:fontFamily="sans-serif-black" // roboto black
android:fontFamily="sans-serif-thin" // roboto thin (android 4.2)
android:fontFamily="sans-serif-medium" // roboto medium (android 5.0)

Sample Image

in combination with

android:textStyle="normal|bold|italic"

this 16 variants are possible:

  • Roboto regular
  • Roboto italic
  • Roboto bold
  • Roboto bold italic
  • Roboto-Light
  • Roboto-Light italic
  • Roboto-Thin
  • Roboto-Thin italic
  • Roboto-Condensed
  • Roboto-Condensed italic
  • Roboto-Condensed bold
  • Roboto-Condensed bold italic
  • Roboto-Black
  • Roboto-Black italic
  • Roboto-Medium
  • Roboto-Medium italic

fonts.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="font_family_light">sans-serif-light</string>
<string name="font_family_medium">sans-serif-medium</string>
<string name="font_family_regular">sans-serif</string>
<string name="font_family_condensed">sans-serif-condensed</string>
<string name="font_family_black">sans-serif-black</string>
<string name="font_family_thin">sans-serif-thin</string>
</resources>

Android Studio Set font family on Textview

You have to put the font's file (galada.ttf in your own case) in the /res/font folder, then use android:fontFamily="@font/galada" in your TextView.

OR

put the font's file in /assets/fonts folder and use this code in your java file to set its typeface.

Typeface typeface = Typeface.createFromAsset(context.getAssets(), "fonts/galada.ttf");
textview.setTypeface(typeface);

How to change the font on the TextView?

First, the default is not Arial. The default is Droid Sans.

Second, to change to a different built-in font, use android:typeface in layout XML or setTypeface() in Java.

Third, there is no Helvetica font in Android. The built-in choices are Droid Sans (sans), Droid Sans Mono (monospace), and Droid Serif (serif). While you can bundle your own fonts with your application and use them via setTypeface(), bear in mind that font files are big and, in some cases, require licensing agreements (e.g., Helvetica, a Linotype font).

EDIT

The Android design language relies on traditional typographic tools
such as scale, space, rhythm, and alignment with an underlying grid.
Successful deployment of these tools is essential to help users
quickly understand a screen of information. To support such use of
typography, Ice Cream Sandwich introduced a new type family named
Roboto, created specifically for the requirements of UI and
high-resolution screens.

The current TextView framework offers Roboto in thin, light, regular
and bold weights, along with an italic style for each weight. The
framework also offers the Roboto Condensed variant in regular and bold
weights, along with an italic style for each weight.

After ICS, android includes Roboto fonts style,
Read more Roboto

EDIT 2

With the advent of Support Library 26, Android now supports custom fonts by
default. You can insert new fonts in res/fonts which can be set to TextViews individually either in XML or programmatically. The default font for the whole application can also be changed by defining it styles.xml The android developer documentation has a clear guide on this here

Change fontFamily for Android EditTextPreference

I thankfully managed to find an answer to my question. The solution involved creating a custom layout and referencing that in my preferences.xml file. Here's how it broke down:

preferences.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:fragment="com.app_name.fragment">
<EditTextPreference
android:id="@+id/key_Edit"
android:clickable="true"
android:inputType="textCapCharacters|textMultiLine"
android:enabled="true"
android:gravity="top"
android:key="pref_phrase"
android:singleLine="true"
android:textAlignment="center"
android:title="@string/preference_key"
android:defaultValue=" "
android:selectAllOnFocus="true"
android:fontFamily="@font/my_font"
android:textColor="@color/colorPrimary"
android:layout="@layout/preference_layout"/>
<EditTextPreference
android:id="@+id/password_Edit"
android:gravity="top"
android:key="pref_password"
android:selectAllOnFocus="true"
android:singleLine="true"
android:textAlignment="center"
android:title="@string/preference_security"
android:defaultValue=" "
android:inputType="number"
android:maxLength="5"
android:fontFamily="@font/my_font"
android:textColor="@color/colorPrimary"
android:layout="@layout/preference_layout"/>
</PreferenceScreen>

preference_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="?attr/listPreferredItemHeight"
android:paddingEnd="?attr/listPreferredItemPaddingRight"
android:paddingLeft="?attr/listPreferredItemPaddingLeft"
android:paddingRight="?attr/listPreferredItemPaddingRight"
android:paddingStart="?attr/listPreferredItemPaddingLeft">
<android.support.constraint.ConstraintLayout
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="@android:id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="@font/my_font"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
android:textColor="@color/colorPrimary"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@id/layout" />
<TextView
android:id="@android:id/summary"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="@font/my_font"
android:textAppearance="@style/TextAppearance.AppCompat.Small"
android:textColor="@color/colorPrimary"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@android:id/title"
android:paddingTop="4dp"/>
</android.support.constraint.ConstraintLayout>
</android.support.constraint.ConstraintLayout>

If you intend to also modify the font of the dialog that inflates with these preferences, you will have to create a custom EditTextPreference and work with the associated dialog builder. Hope it helps you!

EDIT

If you are comfortable using styles.xml, EditTextPreferences ultimately inherit from AlertDialogs in creating and displaying their dialogs. Thus, you can create a custom theme for AlertDialogs in which you set the fontFamily. Thus:

<style name="AlertDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="android:fontFamily">@font/my_font</item>
<item
name="android:textAppearance">@style/TextAppearance.AppCompat.Medium</item>
<item name="android:textStyle">bold</item>
<item name="android:windowNoTitle">true</item>
</style>

Lastly, override the standard AlertDialogTheme in your application's theme using that custom AlertDialogTheme.

How to change fontFamily of TextView in Android

From android 4.1 / 4.2 / 5.0, the following Roboto font families are available:

android:fontFamily="sans-serif"           // roboto regular
android:fontFamily="sans-serif-light" // roboto light
android:fontFamily="sans-serif-condensed" // roboto condensed
android:fontFamily="sans-serif-black" // roboto black
android:fontFamily="sans-serif-thin" // roboto thin (android 4.2)
android:fontFamily="sans-serif-medium" // roboto medium (android 5.0)

Sample Image

in combination with

android:textStyle="normal|bold|italic"

this 16 variants are possible:

  • Roboto regular
  • Roboto italic
  • Roboto bold
  • Roboto bold italic
  • Roboto-Light
  • Roboto-Light italic
  • Roboto-Thin
  • Roboto-Thin italic
  • Roboto-Condensed
  • Roboto-Condensed italic
  • Roboto-Condensed bold
  • Roboto-Condensed bold italic
  • Roboto-Black
  • Roboto-Black italic
  • Roboto-Medium
  • Roboto-Medium italic

fonts.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="font_family_light">sans-serif-light</string>
<string name="font_family_medium">sans-serif-medium</string>
<string name="font_family_regular">sans-serif</string>
<string name="font_family_condensed">sans-serif-condensed</string>
<string name="font_family_black">sans-serif-black</string>
<string name="font_family_thin">sans-serif-thin</string>
</resources>

How to change the fontFamily attribute according to on-device language?

There are methods using the Android APIs, but they don't seem very reliable.

The approach I use is to declare a string in each strings.xml file that tells which is the current language. Something like this:

<string name="app_lang">en-rGB</string>

(Use the convention that fits better: "en-rGB", "en-GB", "english (GB)", …)

Then, in code, have somewhere the static variables:

public final static String APP_LANG_EN_GB = "en-rGB";
// more languages…

And use it when required:

void someMethod() {
String appLang = getString(R.string.app_lang);

switch(appLang) {
case APP_LANG_EN_GB:
// Logic for english GB language
break;
case …:
// Logic for another language
}

if(appLang.equals(APP_LANG_EN_GB)) {
// Do logic for english GB language
}
}


Related Topics



Leave a reply



Submit