Android: using custom font as fallback font for missing glyph - android

is there anyway to set custom font as a fallback font to the default font?
I need to display some cjk characters not supported by android's default font (which I think is noto font for non latin characters). I have a custom font consists of the glyphs missing, so I'm thinking to use noto as the main font, and set my custom font as fallback.
The only way I can come up with is to make noto font as a typeface and set my custom font as fallback. Then I can assign this typeface to where I need it.
However, this mean I need to programmatically assign almost all textview in the app, which doesn't seem right... (and elegant😅)
I would like to ask what is the best practice for this scenario? Like, perhaps there's a way to declare them as a font family in XML?
Thanks a lot ;)

Let me answer myself :)
After trying, I think the easiest way is to merge two font files together.
Just make sure the font's license allows you to do so.

Related

Is there a way to fall back to Typeface.SERIF in a StaticLayout?

I'm creating a custom View class that displays text using a StaticLayout with a custom typeface loaded from a .ttf asset file. The basic look of the typeface is a serif font and the TrueType properties in the font file indicate that it's serif. Nevertheless, for characters not in the font, Android falls back to a sans-serif typeface (it looks like Roboto or Noto, depending on the Android version).
Is there a way to control this so that the Android layout engine falls back to a serif typeface? (Like maybe some magic string to pass to Paint#setFontFeatureSettings(), or perhaps a way to specify a stack of typefaces for use in a Paint?)
Any solution would have to work when the app is distributed, so playing games with Android's system configuration files is not an option. Also, I'm aware of this question (that is still unanswered after four years), but my problem is a bit different, although the solution to both issues might very well be the same.
As of Lollipop, there's a hidden class called FontFamily and a hidden method called Typeface.createFromFamilies. If you're very careful, you can probably use this stuff via reflection or other trickery, but be prepared to fallback to the public API.

Default custom font

Is it possible to set a custom font as default in a android application? Because typing this for every TextView is not efficient.
login = (TextView)findViewById(R.id.tvLogin);
login.setTypeface(titleFont);
Is it possible to set a custom font as default in a android application?
Not for a custom Typeface that you loaded from your own font file, sorry. As others have noted, there are workarounds and libraries to help simplify matters a bit. Personally, I'd look at Calligraphy or perhaps Fontify.

Multiple fonts associated with same TextView

Is there any way to set different fonts for different states in a TextView?
Let's say I want Helvetica Regular for normal state, and Helvetica Bold for pressed?
I know how to link a custom font with a text view, but not sure how to do same for multiple fonts and single TextView? Specifically, is there a way to achieve this behaviour through xml?
Update: I'm not looking into workarounds, like having HTML in TextView, or even replacing TextView with WebView. If it's not possible to achieve, I'd rather have one font
check this link may help you:
http://shuklaxyz.blogspot.com/2012/05/custom-fonts-for-webview-and-textviewv.html
also :
Custom fonts and XML layouts (Android)
also:
Custom TrueType font from xml only
and this :
http://polwarthlimited.com/2013/05/android-typefaces-including-a-custom-font/
hope that help.
Using Html.fromHtml comes in handy, but it is limited to the tags you can use. I personally only have used this to do Bold, Colors, italics.
Like this:
String myWords ="<FONT COLOR = "#992211">This is Red:</FONT><br>";
myTextView.setText(Html.fromHtml(myWords));

Android - Combining multiple font styles, into one single font (Typeface)

I have several fonts with different styles, lets say: "MyFont_default.otf", "MyFont_italic.otf", "MyFont_bold.otf"
Normally, I would set the font of a TextView like this:
Typeface tf=Typeface.createFromAsset(context.getAssets(), "MyFont_italic.otf");
textView.setTypeface(tf);
And my question is: is it possible to combine all these fonts, into one single font, say "MyFont", and depending of the TextView style defined in the XML layout file (italic, bold), the TextView to be displayed in the appropriate font?
I don't have the proper reputation to comment but I can confirm, that approaches work.
Using FontForge (binary available for Windows):
Open your multiple .otf files with FontForge (Regular, Bold, Italic).
Make sure to select the window showing the "Regular" version of the font
Use File > Save ttc from that window
See that indeed the other sub fonts are listed in the save dialog (bold, italic)
Save and use this ttc file in Android
I tested it with an html text displayed in a TextView :
<b>Test</b> Test <i>Test</i>->
"Test Test Test" which didn't work previously in Android 2.3.7 and with this ttc font all three versions of the text show perfectly.
As I said, it's important to select the Regular font when exporting because that's the one Android uses as "default" (I did an export first from the Bold version and it was all Bold).
Use some sort of font editor to combine the fonts into a single file. Many fonts come with multiple styles in a single file, which is what you would need in this case.

Android - Fallback typeface for Views

Is there a way to set a fallback typeface for views? My client is requesting a certain typeface to be used which does not support Cyrillic characters (and they want cyrillic languages..) so therefore any cyrillic characters are beautiful squares! I can't seem to find anything to set a fallback font for a textview?
In iOS theres a default fallback.. Don't let me down Android! The thought of remaking a textview and then changing every single one in my app to the new textview, to support a fallback makes my head hurt.
Thanks,
Currently you can't specify more than one typeface for a view's style declaration.
I guess you'll have to be applying some damage control right now, so you may get off by specifying two styles, one that will support the client's typeface and another that will be used to render user input. You can apply one of the two to the whole application and manually set the other style to override the default typeface.

Categories

Resources