Fonty


Source link: https://github.com/MarcinOrlowski/fonty

Fonty

Fonty is simple Android library allowing you to easily change the typeface of your UI elements. Contrary to other implementations Fonty is designed with the assumption that if you want to change the font for your app, then you change it globally per whole application, to achieve consistency across your Fragments or Activities.

Using Fonty requires no change to be made to your layout files and all you need to do is to initialize the library and specify what typeface you want to be used as regular and boldfaced ones. That's it.

Download demo application APK from releases section. Source code in project's app/ module.

Features

  • Fast and lightweight
  • Simple API
  • Supports the following UI elements and its subclasses:
    • TextInputLayout (see notes below!)
    • TextView
    • EditText
    • Button
    • Toolbar
    • Navigation Drawer
  • Can be used in libraries

Installation

Edit your master gradle.build file and add maven { url 'https://jitpack.io' } to your current repositories block content (if you use other jitpack hosted libraries, then this step can be skipped):

allprojects {

repositories {

  maven {
 url 'https://jitpack.io' 
}

  
}
 
}
 

Next, edit your module's build.gradle and the following dependency:

compile 'com.github.MarcinOrlowski:fonty:<VERSION>' 

For right value of <VERSION> consult release section or see jitpack page.

Configuration

Put your TrueType font files into module's assets/fonts folder ( <MODULE>/src/main/assets/fonts folder, where <MODULE> usually equals app).

Then add the following lines to your custom Application's class' onCreate() method (if you do not use own Application subclass, see demo app for how to make one and how it should be referenced form your AndroidManifest.xml file):

Fonty
  .context(this)
  .regularTypeface("Xenotron.ttf")
  .italicTypeface("Carramba.ttf")
  .boldTypeface("XPED.ttf")
  .done();
 

The above sets up Xenotron.ttf to be used whenever regular font should be rendered and XPED.ttf to be used if your UI elements sets android:textStyle="bold" attribute and Carramba.ttf for android:textStyle="italic".

If you prefer to have font files stored elsewhere than in assets' fonts/ subfolder use fontDir() in your builder chain:

Fonty
  .context(this)
  .fontDir("my-fonts")
  .regularTypeface("Xenotron.ttf")
  .italicTypeface("Carramba.ttf")
  .boldTypeface("XPED.ttf")
  .done();
 

and put your font files into <MODULE>/src/main/assets/my-fonts folder.

NOTE: You MUST call fontDir() before invoking xxxTypeface() in your setup chain, otherwise xxxTypeface() will try to look for fonts in default location and end up failing throwing exception due to missing typeface file.

Font substitution

This sets up font substitution but we yet need to apply fonts to widgets.

For Activity add this as last entry in your onCreate():

Fonty.setFonts(this);
 

Same for Fragments, add the following to your onCreateView():

 Fonty.setfonts(view);
 

where view is the View is what you just inflated.

Using it with RecyclerView is also pretty simple. Edit your onCreateViewHolder() and add:

 Fonty.setFonts(view);
 

where view stands for first argument passed to your onCreateViewHolder() method.

If you are using Android Data Binding library, then you just need to call:

 Fonty.setFonts((ViewGroup)binding.getRoot());
 

Layout files

Once Fonty is properly initialized and applied, all supported widgets will automatically be convinced to use fonts of your choice. Font specified with setRegularFont() is used as default, and if widget sets android:textStyle="bold" then font set with boldTypeface() is applied:


 <TextView

android:text="This will use regular typeface"

... />

<EditText

android:text="This will use boldfaced typeface"

android:textStyle="bold"

... /> 

Fonty and Toolbars

Unfortunately changing Toolbar/ ActionBar title and subtitle fonts cannot be handled automatically by Fonty in some cases. This is due to Toolbar's internals as it simply have not instance of TextView created unless title or subtitle is set, so there's nothing Fonty can manipulate in advance. To work that around add this to your base activity class:

 private Toolbar mActivityActionBarToolbar;
@Override  public void setSupportActionBar(@Nullable Toolbar toolbar) {

  super.setSupportActionBar(toolbar);

  mActivityActionBarToolbar = toolbar;  
}

@Override  public void setTitle(CharSequence title) {

  ActionBar ab = getSupportActionBar();

  if (ab != null) {

  ab.setTitle(title);

  Fonty.setFonts(mActivityActionBarToolbar);

  
}
  
}

public void setSubtitle(CharSequence subtitle) {

  ActionBar ab = getSupportActionBar();

  if (ab != null) {

  ab.setSubtitle(subtitle);

  Fonty.setFonts(mActivityActionBarToolbar);

  
}
 
}
 

TextInputLayout

If you use TextInputLayout and its error message feature (text shown below the EditText widget), and you want error text typeface to be changed by Fonty as well, then you must either set app:errorEnabled="true" in the XML layout or call setErrorEnabled(true) on the object prior calling Fonty.setFonts(). This is because of how TextInputLayout works internally.

Limitations

Due to limitations of the Android API, once fonts are replaced by Fonty, former style information (like bold, regular) is reset, so all calls to i.e. isBold() or isItalic() will always return false. At the moment there's no workaround for this issue, yet it should not really affect many, however because this information is gone, and Fonty relies on it then calling Fonty.setFonts() twice on the same layout elements will end up with wrong results (mostly all widgets will be using REGULAR typeface).

Contributing

Please report any issue spotted using GitHub's project tracker.

If you'd like to contribute to the this project, please open new ticket before doing any work. This will help us save your time in case I'd not be able to accept such changes. But if all is good and clear then follow common routine:

  • fork the project
  • create new branch
  • do your changes
  • send pull request

License

  • Written and copyrighted ©2013-2017 by Marcin Orlowski <mail (#) marcinorlowski (.) com>
  • Fonty is open-sourced library licensed under the Apache 2.0 license

Resources

Android library project that intends to simplify the usage of Adapters for ListView/GridView and RecyclerView. You won't have to code any boring adapter again!

Quote it is a social platform for sharing quotes and wise sayings. It is great to share and spread the wisdom and thought full.

Are you tired of searching images for your Android app and copying each image file into its own drawable folder? I had the same problem and each icon I have copied I thought that I am wasting my time. Especially when Google published their big "Material Design Icon" pack. Nearly 70,000 icons in too many folders. So I thought that I need a tool which reads all icons of a folder and give me the chance to copy them easily into my project drawable folder. And here it is :-)

This tiny library lets you easily send Android Intents to the main Evernote app.

A simple rest API client library.

Some common classes for use in your android projects.

Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes