How to clear fontFamily after setup in XML - android

I have some layout already setup font for textView.
android:fontFamily="#font/cookie_regular"
But i want to have more setting option to clear/set default font when i need in Java code.
Do you have any suggestion ?

Call this method when you want to change some textview to a different font.
public void change_font(){
//if or switch statement may be needed or not.
Typeface custom_font = Typeface.createFromAsset(getActivity().getAssets(), "fonts/THE_FONT_I_HAVE_CREATED_OR_DOWNLOADED.ttf");
//FYI font is a file you can create in your res folder.
myTEXTVIEW.setTypeface(custom_font);
}
Lastly more information about downloadable fonts
https://developer.android.com/guide/topics/ui/look-and-feel/downloadable-fonts

Related

I can't apply Typeface programmatically for Google font in Android Studio

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.

Setting Default fonts for whole app

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

How can i use clocktopia font which is already there in my Android Device?

I have a clocktopia font on my android device and it's only on some applications.
How do I use that particular font programmatically?
Thank you.
First, put the .ttf (or other font file) in your /assets directory.
Then, set the font face of your textview with the following code.
TextView textView = (TextView) findViewById(R.id.myTextView);
Typeface font = Typeface.createFromFile("path-to-file");
textView.setTypeface(font);
Unfortunately you cannot use custom typefaces in XML (so you also cannot define a custom theme with your font).
Custom fonts and XML layouts (Android)
You can use the TextViewPlus class provider here to load the typeface from xml but I don't know of a way to use a theme to do this automatically.

Android - Fonts in Application

I have added an external font in /assets directory, and manually doing setFacetype(font).
Isn't there a general way to set the whole application to use a specific font if you have added it external? Or do you have to use Android's selected fonts in order to achieve this?
You cannot use your custom fonts through to whole application in a general way.
You cannot set your custom fonts through xml files.
You have to use the Typeface functions in your code to use your custom fonts within your application.
tv=(TextView)findViewById(res);
Typeface font = Typeface.createFromAsset(this.getAssets(), "MYFONT.TTF");
tv.setTypeface(font);
This also how to use it in a textview.
For whole application go to Using a custom typeface in Android.
and go to Manish Singla answer
Typeface mTypeface = Typeface.createFromAsset(getAssets(), "YOUR FONT NAME");
textview.setTypeface(mTypeface, Typeface.NORMAL);

Set the fonts toa text in android

Hi want to change the font type to atext according to spinner selection like arial balck,Thlist etc.any one have idea ? suggest me
You should change the typefaces of your text views. You can specify a variable:
Typeface typeface = Typeface.DEFAULT;
Then when the user chooses another font you reinitialize this variable, using the built-in typefaces, or loading your own from .ttf files, that you can place in assets/fonts directory. Loading own fonts can be done with this call:
typeface = Typeface.createFromAsset(context.getAssets(),
"fonts/font.ttf");
This should do it.

Categories

Resources