Typeface is not rendered in Android - android

I'm trying to set a font like this:
Typeface tf = Typeface.createFromAsset(getAssets(),"fonts/myfont.ttf");
textView.setTypeFace(tf);
However, it works for some fonts like Helvetica, or like that, but as soon as I try to load some other fonts, it ignores my typeface and it loads android's default one.
I've checked a looot of answers, and an user stated
Android at the moment when can't set a typeface, instead of throwing an exception, just places system default typeface.
But this is obviously not an answer.
What can I do?
I've even checked sourcecode of TextView, which leaded me to TextPaint, and TextPaint leaded me to Paint but couldn't really find a solution about my issue.
Help would be sooo appreciated.
Thanks.

But this is obviously not an answer.
Well, grammatically it has issues, but it is the answer. Android has had problems loading fonts since Android 1.0, and the API for Typeface does not report success or failure.
What can I do?
Test your typefaces and see which ones work.
If you have licensed the rights to modify the font, you could try loading it into a font editor and saving it again, to see if it is a question of the format of the font file.
Or, contribute changes to the AOSP to improve typeface handling, then wait a few years for those changes to make it out to the majority of Android devices.

Try typeface using SpannableStringBuilder it will work,,
String stringResult="Your unicode string";
SpannableStringBuilder SS = new SpannableStringBuilder(stringResult);
Typeface tf = Typeface.createFromAsset(getAssets(),"fonts/myfont.ttf");
if(tf != null)
{
SS.setSpan (new CustomTypefaceSpan("", tf), 0, stringResult.length(),Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
}
textView.setText(SS);

Related

Android - Shortname to Unicode from Emojione doesn't work

First of all, I don't know whether I should ask this here or in the repo, but as I saw other questions for other libraries like.. Picasso, so here goes :
I'm trying to implement Emojione into my Android app. I've downloaded a small static class to convert all short name to unicode from Emojione Github Repo.
The problem is, when I try to use it to convert :smile: to unicode..
Emojione.shortnameToUnicode(postMessageText, true); // postMessageText is ":smile:"
It always return.. A square, as if it failed to convert. I tried other short names too like :smiley:, :grinning:, but they also failed. :(something): is really a short name right?
The answer is actually in front of my eyes all the time. It really did convert to unicode but, I might be wrong here, in a font unsupported by Android. So I tried using SpannableStringBuilder to span the emoji part to use emojione-android.ttf
Here's how :
int firstEq = sb.length();
Typeface font = FontCache.getTypeface("emojione-android.ttf", context);
String convertPart = Emojione.shortnameToUnicode(part, true);
sb.append(convertPart + " ");
int lastEq = sb.length();
sb.setSpan(new CustomTypefaceSpan("", font), firstEq, lastEq, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
And that's how I got the TextView to show emojis.. Just use the font!

typeface not applied no error thrown

Im trying to apply typeface to textviews and editTexts but the typeface silently fails with no error or exception in logs.There is no change in font and android default font is shown.
I have tried around 10 fonts nothing works(otf & ttf both) nothing worked
I also tried to convert it http://www.freefontconverter.com/ here as some answers suggested. But, still not working.
typefaceLight = Typeface.createFromAsset(getAssets(), "fonts/MyriadProRegular.otf");
// typefaceBold = Typeface.createFromAsset(getAssets(), "fonts/HNBold.otf");
// typefaceBold = Typeface.createFromAsset(getAssets(), "fonts/Helvetica.ttf");
typefaceBold = Typeface.createFromAsset(getAssets(), "fonts/HelveticaBold.ttf");
typefaceLight = Typeface.createFromAsset(getAssets(), "fonts/HelveticaLight.ttf");
typefaceArialBold = Typeface.createFromAsset(getAssets(), "fonts/helveticabold1.ttf");
// typefaceLight = Typeface.createFromAsset(getAssets(), "fonts/HNLight.otf");
registerTitleTv.setTypeface(typefaceBold);
signInTittleTv.setTypeface(typefaceBold);
termsSiginTextView.setTypeface(typefaceLight);
termsSignUpTextview.setTypeface(typefaceLight)
I have referred to
Custom Fonts not working in lollipop?
Custom Font not working on Android
Changing the android typeface doesn't work
https://code.google.com/p/android/issues/detail?id=61771
Android 5.0 (API 21) fails to render custom fonts / typefaces
Is there something wrong with my code?
your code seems fine . I would suggest you either clean your code or uninstall app and install again .

Android Studio: Layout Preview gives "native typeface cannot be made"

I'm using a font I found on the internet. In particular, the font that is currently causing a problem is Amaranth (Google Fonts link).
I've looked through these questions:
Native typeface cannot be made
RuntimeException: native typeface cannot be made
native typeface cannot be made, exception
custom font in android studio
All of those questions seem to be dealing with crashes that occur on the device at runtime. My problem is that I get this Exception when trying to use Android Studio's Preview tool to preview my layout.
The text I'm seeing in the "Rendering Problems" pop-up is this:
Exception raised during rendering: native typeface cannot be made
java.lang.RuntimeException: native typeface cannot be made   
at android.graphics.Typeface.<init>(Typeface.java:147)   
at android.graphics.Typeface.createFromAsset(Typeface.java:121)   
at app.SegmentButton.onDraw(SegmentButton.java:79)
The code in SegmentButton is this:
#Override
public void onDraw(Canvas canvas) {
// Other code...
String fontPath = "fonts/Amaranth-Bold.otf";
// The next line is the line that causes the "crash"
Typeface tf = Typeface.createFromAsset(mContext.getAssets(), fontPath);
textPaint.setTypeface(tf);
// Other code...
}
I have checked and ensured the following items:
My assets folder is in 'app/src/main/assets'
My fonts folder is in 'app/src/main/assets/fonts'
The file name is exactly "Amaranth-Bold.otf"
This font works exactly as desired when running my app. I have tested this on an LG Optimus Pro G as well as a Droid X. You can see the results in these images:
LG Optimus G Pro Screen Shot
Droid X Screen Shot
As you can see, the fonts show up as desired in both places. As you can probably also see, the Droid X screen shot shows some layout issues that I need to resolve. I'm trying figure out how to do this at design time using the tools built in to Android Studio. This is merely the first issue I'm tackling. I would hate to have to run the app each time in order to make small tweaks to the layout. This is especially true since these are the only two hardware devices I currently have, and I want to test on a much wider variety of screen sizes (and the emulator is sloooooooooow).
Based on my reading of other questions (and primarily based on the fact that the devices load the font fine at runtime), my current conclusions are:
The font file isn't "broken".
The font location isn't wrong.
The typed-in file name isn't wrong (extension, capitalization, etc.)
What else can I try?
I had the same problem yesterday. Android Studio gives you this tip:
Use View.isInEditMode() in your custom views to skip code or show sample data when shown in the IDE
Rewrite your onDraw() method like this:
#Override
public void onDraw(Canvas canvas) {
// Other code...
if(!isInEditMode())changeFont();
// Other code...
}
private void changeFont(){
String fontPath = "fonts/Amaranth-Bold.otf";
Typeface tf = Typeface.createFromAsset(mContext.getAssets(), fontPath);
textPaint.setTypeface(tf);
}

Correcting Hindi (Unicode) rendering in Android

I successfully added a custom typeface (DroidSansFallBack.ttf) to show Hindi characters instead of Blocks on Pre ICS . All is working well on 4.1+ in any case.
But the rendering order is wrong on Froyo and Gingerbread.
I understand it is not resolved http://code.google.com/p/android/issues/detail?id=4153
What are the possible workarounds to correct the rendering? (I don't want to use images )
EDIT:
Example,
दिन string from values-hi renders as दनि
नक्षत्र renders as नक् ष त र
(On 4.1+ everything renders correctly)
I have a alternative solution for your problem but not exact...
use font....
Typeface tf = Typeface.createFromAsset(getAssets(),"Hindi-SARAL1.TTF");
for नक्षत्र use .......
TextView tv = (TextView) findViewById(R.id.textView1);
tv.setText("n95");
tv.setTypeface(tf);
and for दिन........ unicode value continues no gap.. i have given a space between &# and 2367;
tv.setText(Html.fromHtml("&# 2367;&# 2342;&# 2344;"));
tv.setTypeface(tf);
I could not find a simple work around for fixing the rendering everywhere. So finally I had to limit the Hindi language feature to devices having android version 4.0 or later.

textStyle for Tamil font?

I'm already working on an Android application that displays RSS feed.
My problem is that this feed has some lines in Tamil(which Android still doesn't support)
I found a font online that displays the text right(without converting to Bamini) but the problem is that textStyle doesn't have any effect on it.
so you know any font that can do the job or any thing i have to do to make textStyling?
thanks in advance
What you need to do is import custom fonts on to the phone before using them.
A good way to do that is to include them in the package - in the APK file
Hence you should be having the font in your project when you build the APK file.
Let me give you an example. Assuming your tamil font's name is Harabara.ttf and you have copied it to /assets/fonts
Use this method (anywhere in your activity)
private void initializeFonts() {
font_harabara = Typeface.createFromAsset(getAssets(), "fonts/Harabara.ttf");
}
and call this API from your onCreate like this
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initializeFonts();
setContentView(getViewResourceId());
}
Make sure you have declared this class level variable
Typeface font_harabara = null;
Finally, simply use
myTextField.setTypeface(font_harabara);
tada ! tamil font should now start displaying.
nandri vanakkam,
vaidyanathan
There are hundreds of fonts available online. Did you check some TSCII fonts?
I've written some solutions for Tamil and Android issues. Check it out too.

Categories

Resources