Is there any way to view custom fonts/views in the preview section of Android Studio?
I have used font-awesome as a custom typeface to show microphone icon in my app. Everything is working fine. But as we all know the preview section cannot load custom views.
Is there any plugin or hack to see the custom views in the preview window while coding?
This is what I am loading on my app:
This is what I see in the preview section:
To make FontAwesome icons visible in Android Studio XML designer you can.
Here is full demo code in gist
Demo img with code from comment:
Important parts: (pretty much the same as Declaring a custom android UI element using XML but with small tuning)
TextViewWithFont.java - Custom view class
res/values/attrs.xml - Need this to use
app:customFont="fontawesome-webfont.ttf"
in our layout xml.Typefaces.java - Helper class to reuse fonts (Cache for fonts)
activity_main.xml - Layout and how to use TextViewWithFont custom view
Preview section loads custom views fine as long as these views are correctly written. You have to remember about all small details like draw/onDraw/dispatchDraw methods, measuring and layouting, setting correct theme, styling, providing editMode data, etc.
The deal is that Android Studio has its own Context and Resources classes which are unable to perform certain things. For example these classes lack implementation of reading assets from assets folder and raw resources from raw folder.
To load a custom font, you need assets folder which you don't have access to in Android Studio. Custom view should work, more or less.