Jacksonkr
Jacksonkr

Reputation: 32207

Android Studio set fontfamily via layout editor

currently I'm setting up fonts for runtime with

@Override
protected void onFinishInflate() {
    super.onFinishInflate();

    Typeface customFont = Typeface.createFromAsset(getContext().getAssets(), "fonts/customFont.otf");
    this.customTxtView.setTypeface(customFont);
}

But that font doesn't reflect in the Layout Editor in Android Studio. I've tried setting android:typeface as well as android:fontFamily using the same values as I'm using in the code with no avail.

Is it possible to have the Layout Builder reflect custom font changes?

Upvotes: 0

Views: 1707

Answers (2)

Ivan Nikitin
Ivan Nikitin

Reputation: 4133

You should see textAppearance property on the right when a TextView is selected. You can either select a predefined text appearance or expand the group and specify the font family only.

text appearance settings in Layout Editor

Upvotes: 1

piotrek1543
piotrek1543

Reputation: 19351

According to 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-thin"      // roboto thin (android 4.2)
android:fontFamily="sans-serif-medium"    // roboto medium (android 5.0)

enter image description here

in combination with

android:textStyle="normal|bold|italic"

this 14 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-Medium
  • Roboto-Medium italic

NOTE: There's also robotium-thin typespace.


But as I see you want to use your custom font, so you would find also there that:

Android doesn't allow you to set custom fonts from the XML layout. Instead, you must bundle the specific font file in your app's assets folder, and set it programmatically. Something like:

TextView textView = (TextView) findViewById(<your TextView ID>);
Typeface typeFace = Typeface.createFromAsset(getAssets(), "<file name>");
textView.setTypeface(typeFace);

Note that you can only run this code after setContentView() has been called. Also, only some fonts are supported by Android, and should be in a .ttf (TrueType) or .otf (OpenType) format. Even then, some fonts may not work.

This is a font that definitely works on Android, and you can use this to confirm that your code is working in case your font file isn't supported by Android.

Please free to say, if this post doesn't help you

Upvotes: 3

Related Questions