Integrate a specific font with Android apk - android

I have built an application which displays the news in Nepali (Devnagari) font.
Everything is fine but the problem is that the font is not displayed properly in some Android phones.
Here I have posted two images showing the variation of the same content in different phones. The first one is perfect but the second one is incorrectly displayed.
1st Image with correct fonts
2nd Image with incorrect fonts
The problem I realized is that the second phone does not contained the correct font installed in it.
How can we get rid of this problem?
I have got no proper idea regarding this, but a solution that came in my mind is that - What if we integrate the necessary font along with the apk file and the font too gets downloaded with the apk.
Please pass me suggestion for this and would be grateful to know how to implement any solution that exists.

If you are using the native Android Textview Component you could use the method:
public void setTypeface (Typeface tf)
And create the typeface this way :
static Typeface createFromAsset(AssetManager mgr, String path)
Create a new typeface from the specified font data.
Typeface.createFromAsset(getAssetManager(),"RELATIVE_PATH_TO_YOUR_TYPFACE_IN_THE_ASSET_FOLDER");
If you are using Android Studio the asset folder should be created under src/main/assets.

Make 1 folder in assets named "fonts", put your font files inside that folder. e.g. see following image
import android.graphics.Typeface;
Then declare Typeface object
Typeface allFontType, allFontTypeNormal, allFontTypeLight;
try
{
allFontType = Typeface.createFromAsset(getApplicationContext().getAssets(), "fonts/Dosis-Bold.ttf");
allFontTypeNormal = Typeface.createFromAsset(getApplicationContext().getAssets(), "fonts/Dosis-Medium.ttf");
allFontTypeLight = Typeface.createFromAsset(getApplicationContext().getAssets(), "fonts/Dosis-ExtraLight.ttf");
}
catch (Exception e1)
{
e1.printStackTrace();
allFontType = Typeface.create(Typeface.SANS_SERIF, Typeface.BOLD);
allFontTypeNormal = Typeface.create(Typeface.SANS_SERIF, Typeface.NORMAL);
allFontTypeLight = Typeface.create(Typeface.SANS_SERIF, Typeface.NORMAL);
}
To apply font in TextView use following code
TextView lblDaysName = (TextView)view.findViewById(R.id.lblDaysName);
lblDaysName.setTypeface(allFontTypeNormal);

Related

How to use custom font in android library project?

I have created a TextView library project where I have used a custom font. I have to create this library because this TextView will use in other three project as well. I found that assets/fonts folder is not supported by android library project. If I use this way it gives me error message
native typeface cannot be made at android.graphics.Typeface
Is there any workaround here to solve this issue. Other way, I have to use duplicate view in three different app. And also the sample code I am using to get font from library assets folder.
public static Typeface getFont(Context c, String fontName) {
synchronized (fonts) {
if (!fonts.containsKey(fontName)) {
Typeface t = Typeface.createFromAsset(
c.getAssets(), "fonts" + File.separator + fontName);
fonts.put(fontName, t);
}
return fonts.get(fontName);
}
}
infect you can do lot of things .one of easiest solution is following
you can write a function that copy paste font in some sd card folder and for sure it will be static . now you can create type from that path
createFromFile(String path)
http://developer.android.com/reference/android/graphics/Typeface.html
To use different Fonts in my project I have use font Library. Just add this library in gradle.
dependencies {
compile 'com.vstechlab.easyfonts:easyfonts:1.0.0'
}
Use of this library is very easy. I have use reference https://android-arsenal.com/details/1/2044
create fonts directory in your assets folder and set your font with ttf format in the font directory. then call font by this code:
Typeface tf = Typeface.createFromAsset(getAssets(),"fonts/font.ttf");
textViewName.setTypeface(tf);
your font must be here :
assets>fonts>font.ttf

Android Studio - Set custom fonts with asset manager in fragment

I'm rewriting a project of mine to incorporate fragments for use on tablets. I also decided to start using Android Studio with the conversion. Pretty straight forward but I've run into an issue on the pretty simple task of setting a custom font.
I started with these:
Custom fonts in android and
List of files in assets folder and its subfolders and this Set custom font for Android fragments
In Eclipse my class extended Activity and I did the following in the OnCreate with no problems.(Having a directory and file "assets/FUT_R.ttf")
TextView tv=(TextView)findViewById(R.id.someTextView);
Typeface font = Typeface.createFromAsset(getAssets(), "FUT_R.ttf");
tv.setTypeface(font);
now trying to convert this Activity to an ActionBarActivity (fragment with V7 support library) and I've changed the above code to this. (where view is the inflated layout with one Framelayout for phones)
TextView tv=(TextView) view.findViewById(R.id.someTextView);
Typeface font = Typeface.createFromAsset(getActivity().getAssets(), "FUT_R.ttf");
tv.setTypeface(font);
and it crashes at run time with the lovely java.lang.RuntimeException: native typeface cannot be madeeven though i created and 'assets' directory on my new project with FUT_R.ttf in its root.
To confirm the asset manager I tried this code:
String[] f = null;
try {
f = getActivity().getAssets().list("");
}
catch (IOException e){
e.printStackTrace();
}
for(String f1:f){
Log.i("names",f1);
}
And got the following output:
07-26 07:40:40.134 2114-2114/com.myapp I/names: images
07-26 07:40:40.134 2114-2114/com.myapp I/names: sounds
07-26 07:40:40.134 2114-2114/com.myapp I/names: webkit
I'm confused because no where in my 'assets' directory or project do I have these files and/or directories. Obviously the error is the system can't find my font file. WHAT AM I DOING WRONG? Any direction would be greatly appreciated, I've wasted far too much time on this stupid problem.
Thanks!
I finally stumbled upon this: Load a simple text file in Android Studio
which points out the assets folder in Android Studio has apparently been changed. Instead of its normal location in the root app folder (same level as libs, src, ect) it needs to be created under yourapp/src/main/assets. Hopefully this helps someone else. I wasted a lot of time on it!

How can I set chosen font in textview

I have a question. How Can I set font for example: Harrington in textview. I use:
Typeface type = Typeface.createFromFile("C:/Fonts/Kokila.ttf");
gameResult.setTypeface(type);
gameResult = (TextView) findViewById(R.id.textViewResult);
but this solution is not working. Any ideas?
first place your font in assets folder then like:
gameResult = (TextView) findViewById(R.id.textViewResult);
Typeface type = Typeface.createFromAsset(getAssets(),"fonts/Kokila.ttf");
gameResult.setTypeface(type);
You are trying to access the font from your PC! Your phone does not have access to the 'C:/' drive of your PC, and therefore cannot load the font. You need to place the Font into the Assets folder of your application, and then read it from there.
You can read a file from the Assets folder using
context.getAssets().open(fileName);

How to set gujarati for textview in the android application?

I want to make a application in which i needs to show the textview in gujarati language.
I tried with
Typeface tf = Typeface.createFromAsset(getAssets(), "G-SARAL1.TTF");
text.setTypeFace(tf);
but this is not working, I set the gujarati text in string.xml file and then i set that string in the textview, but when i run the application, it display square boxs instead of gujarati character,
can anyone help me , how can i implement this task.
As gujarati and hindi languages are not supported by Android, you can still give that support to your Application.
For Gujarati copy the C:\WINDOWS\Fonts\Shruti.TTF file to your asset folder. Then use the following code.
TextView text_view = new TextView(this);
Typeface font = Typeface.createFromAsset(getAssets(), "Shruti.TTF");
text_view.setTypeface(font);
text_view.setText("ગુજરાતી");
Shruti.TTF file is for Gujarati font. Similarly you can add support for hindi file.
Try this code this will work
TextView mtxt = (TextView) findViewById(R.id.action_settings);
Typeface face1 = Typeface.createFromAsset(getAssets(),
"Lohit-Gujarati.ttf");
mtxt.setTypeface(face1);
mtxt.setText("પૂર્વ વડાપ્રધાન રાજીવ ગાંધીની હત્યા કરનારા 3 આરોપીઓની ફાંસીની સજા સુપ્રિમ કોર્ટે ઉમ્રકેદમાં ફેરવી નાંખી છે.આમ રાજીવગાંધીના" +
"3 હત્યારાઓને હવે ફાંસીની સજા નહી થાય.સુપ્રીમ કોર્ટમાં 3 જજોની બેન્ચે આજે આ ચુકાદો આ");
You can download the Montserrat fonts, this font is use for both language English and Gujarati. If you do not want to download the saperate fonts than can use this. You can make base text and can create method like below and use the fonts. One more thing you have to pass the
fun setFonts(context: Context, fontName: String) {
val typeface = Typeface.createFromAsset(context.assets, fontName)
setTypeface(typeface)
}
To create localized string file you can also use Translations Editor.
I checked out all the given solutions but no one solved my prob
so I did some more and more google and found out that, you need to create assets folder first
and then paste all the fonts file which you required in your application
here is a little guide for how we can add assets folder in our project
Step 1 : Navigate to the top left side of an android studio where you got menu like this and just change the option from the dropdown to Packages
Step 2 : now you will see parent as app and now just right click on the app and click new and then Folder and then Assets Folder
this is how you can add Assets Folder.
once you add Assets Folder then simply paste your ttf files into that Assets folder.
and follow my code for reference
TextView text_view = (TextView) findViewById(R.id.txtMsg);
//TextView text_view = new TextView(this);
Typeface faceShruti = Typeface.createFromAsset(getAssets(),
"shruti.ttf");
text_view.setTypeface(faceShruti);
text_view.setText("In Gujarati\n" + "સ્વાગત");

Android develop LCD font

How to add additional font in android environment? I need to make a font like LCD with pale background numbers like this:
http://www.androidfreeware.net/img2/gps_speedometer_android_1.png
We at bangalore android developers group had done this for one of our project AutoMeter You can have a look at the code there in detail.
The code would look something like this
Typeface myTypeface = Typeface.createFromAsset(this.getAssets(),"DS-DIGIB.TTF");
waitingTimeView = (TextView) findViewById(R.id.WaitingTime);
waitingTimeView.setTypeface(myTypeface);
DS-DIGIB.TTF is the font put in the assets folder
public void setFontTextView(Context c, TextView name) {
Typeface font = Typeface.createFromAsset(c.getAssets(),"fonts/Arial.ttf");
name.setTypeface(font);
}
Download Arial font in ttf file formate and put in asset folder. Thats enough.
You want to use the android.graphics.Typeface class, which can create new Typefaces from assets or files.

Categories

Resources