I am using a custom font named AwesomeFont in Android App. This help us to produce scalable vector icon. Now the font file get applied on the view (both TextView and EditText) if the application launches from beginning but if the application is in background and get open after few minutes the font-file from the views get removed.
I also try to set typeface from onResume() method but it doesn't help me to get rid of the problem.
Please suggest a solution.
I found that that this problem exist for every custom font that I am using in the application. Custom font get applied throughout the application if is starts from launcher-icon(splash screen), but not if app launches from background after few minutes.
1- Copy fontawesome-webfont.ttf into my assests folder
2-Create an entry in strings.xml for each icon. Eg for a fa-bell:
<string name="bell"></string>
3-create entry in the view of my xml layout:
<Button
android:id="#+id/bells"
style="?android:attr/buttonStyleSmall"
...
android:text="#string/bell" />
4-Loaded the font in my onCreate method and set it for the appropriate Views:
Typeface font = Typeface.createFromAsset( getAssets(), "fontawesome-webfont.ttf" );
Button button = (Button)findViewById( R.id.bells);
button.setTypeface(font);
For more See this
Related
I am currently developing an app which displays special unicode characters (e.g. ꁴ)
Now I encountered that on older devices those symbols can not be displayed.
How can I know if it works on the current device or not?
Do I have to create an Virtual Android Device for each SDK-Version and test?
Would it be possible to bundle the current Roboto Font into my apk and deliver it to older devices so that the characters will be displayed?
if place your custom font in assets folder and address when used you will not have an issue
place your Roboto.ttf in assests folder
Created an entry in strings.xml for each icon. Eg for a "Yi Syllable Bby":
<string name="icon_note">ꁴ</string>
Referenced said entry in the view of my xml layout:
<Button
android:id="#+id/like"
style="?android:attr/buttonStyleSmall"
...
android:text="#string/icon_note" />
Loaded the font in my onCreate method and set it for the appropriate Views:
Typeface font = Typeface.createFromAsset( getAssets(), "Roboto.ttf" );
...
Button button = (Button)findViewById( R.id.like );
button.setTypeface(font);
it can be used to use icon lib as font check here How to use icons and symbols from "Font Awesome" on Native Android Application
I added a Google font using Android Studio,
It created a 'font' folder and under it is an XML file as follow:
res -> font -> font_name.xml
I'm trying to apply it programmatically but I can'y find how to do it,
I've tried several codes but nothing worked, examples:
Typeface font = Typeface.createFromFile("font/font_name.xml");
or
Typeface font = Typeface.createFromAsset(getAssets(), "#font/font_name");
Please note,
The 'font' folder along with the 'font_name.xml' file,
Were created automatically by Android Studio.
Your help would be appreciated,
Thank you.
I am certainly not an expert, but I managed to get my Typefaces working.
Firstly, this thread contains a lot of useful info:
Custom fonts and XML layouts (Android)
Now specifically to your problem, the way I managed to get Typefaces to work is by downloading a .ttf file either somewhere online, or when using android studio and clicking more fonts, set the radio button to add font to project. That will download the .ttf to res\font\font.ttf Move it to your assets directory into fonts folder and then create the typeface like this:
Typeface typeface = Typeface.createFromAsset(getAssets(), "fonts/font.ttf");
If you want to use your font often and don't want to bother setting it every time, a good idea might be extending a TextView or whatever class you need and using the .setTypeface() method in the custom constructor.
Just solved this myself, here is what I have put together.
Typeface font = Typeface.create("cursive", Typeface.NORMAL);
Log.e("Setup", "Font: " + font.getStyle());
signatureButton.setTypeface(font);
The log just checks if you have selected an acceptable font, it should return 0 if not. The hardest part was finding a font family that worked. The key is to go to your XML file and type "android:fontFamily=" from here a suggestion list should pop up with choices at the bottom.
To change the google font family
Typeface typeface = ResourcesCompat.getFont(this, R.font.your_font);
textView.setTypeface(typeface);
Add that to your fonts folder then use them in the XML or programmatically.
I have a TextView which contains just one letter. The size of the text is calibrated so that the letter occupies entire TextView area. Now one of my users reported a problem that the letter does not fit properly into the TextView. From the attached screenshot I can see that she uses some kind of custom font on her Samsung S4 device. I am sure that's the problem. Here are some snapshots:
Custom font in a TextView over ImageView:
Custom font in the Status Bar:
Distorted letter N in my TextView:
Is there any way to make a TextView use standard android font ignoring any custom fonts that users applies? How can I install custom fonts on the emulator or Sony phone (do not have Samsung) and replicate the behavior?
Yes you can set custom fonts to your app. Make use TypeFace class.
Create a folder called fonts under assets folder and place all your fonts in it (Font format ttc or ttf. Folder name can be anything)
Within you onCreate method add the below code.
TextView mytextview= (TextView) findViewById(R.id.text_view_id);
// Loading Font Face. Replace with your font file name
Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/your_custom_font.ttf");
// Applying font
mytextview.setTypeface(tf);
Similar way you can have as many as TypeFace you require and also use the same TypeFace declared on as many as TextView you want.
This tutorial below very well explains your requirement and also provides screenshot for better assistance.
http://www.androidhive.info/2012/02/android-using-external-fonts/
Also kindly note that TypeFace has a lot of other options that you can play around with. You can refer the android docs to get an idea about them.
I hope it helped you.
Thanks!!
My advise:
Create a subfolder in your asset folder called fonts.
Put there the fonts you want to use (for example ttc, or ttf) Set
your TextView typeface. For example:
yourTextView.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/yourFont.ttc"));
I hope it helps!!
In my application i have not set typeface property for textviews in my app that is used to change font of textview.If someone runs the app on phone ,the default fonts used are the fonts set for the phone.I want my app to have only one font for all textviews in my app even if it is running on phone having different default font.My default fonts should be same on any phone.I dont want to use custom Textview.Is there a way to do this?
I hope it's not to late.
Use this library:
Calligraphy
What you need to do is
1) Add this in your onCreate() method and select your font in the assets/fonts folder of your project.
CalligraphyConfig.initDefault("fonts/Roboto-Regular.ttf", R.attr.fontPath);
2) Wrap the activity context.
#Override
protected void attachBaseContext(Context newBase) {
super.attachBaseContext(new CalligraphyContextWrapper(newBase));
}
Do this for every activity! And thats it. :)
For all the textviews you are declaring in your project you need to get the id in java code using findViewById and u need to set the typeface for each and every textview like following
font = Typeface.create("serif", Typeface.NORMAL);
myTextView = (TextView) findViewById(R.id.myTextView);
myTextView.setTypeface(font,Typeface.BOLD);
u need to set the font for all the textviews in your project in above way
I am new android developer , I am design such type of application ,In my application i want to see to actual look of textview at **design time when change the different font in eclipse ** .
Here ,I am giving one example .
Please help me is there any way to possible to see different font style at design time ?
You can use TTF font for this.You need to download and put this in assets folder and then use Typeface for this.You'll have to manage at runtime according to the project requirement.
TextView tv=(TextView)findViewById(R.id.tv);
Typeface face=Typeface.createFromAsset(getAssets(), "fonts/Alan Den.ttf");
tv.setTypeface(face);
Hope this will work,for any query let me know.Good luck