Set the fonts toa text in android - 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.

Related

How to clear fontFamily after setup in XML

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

Adding costum font to android in font family xml

In the android guides for Fonts in XML, they give the example of font family XML you create by adding:
android:font="#font/lobster_regular"
Which I think its the font you added to your assets/font folder. But when i try it I don't get it why it isn't able to find the font I've added. It only fins sans, serif and mono-space. Anyone have a clue what it might be the problem?
I know there are other ways to add a custom font, but due to the material calendar that I want to use, I need it to define in the styles XML, only way to customize it. I tried calligraphy lib, but does not help.
Using typeface you easily set font family
Typeface type = Typeface.createFromAsset(getAssets(),
"fonts/fontname");
now set this typeface to your textview
tv.setTypeface(type);
Add this to your activity code:
TextView tv = (TextView) findViewById(R.id.appname);
Typeface face = Typeface.createFromAsset(getAssets(),
"fonts/fontname");
tv.setTypeface(face);
Note that for this you'll have to create a fonts folder at app/src/main/assets

I want to change the font on part of my String variable (textview) with a font located in assets

I have textview which contains a string that i want a specific part of it to be red and use a different font from the rest of the text this is what i've tried
in the onCreate method i put a different font on the whole textview
Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/RobotoCondensed-Light.ttf");
mn.setTypeface(tf);
and after that Si is part of a for loop and when a case is matched i want to change the font
Si+="<font face='RobotoCondensed-Bold' color='#8B2121'><b>"+SiArray[0]+"</b></font>";
mn.setText(Html.fromHtml(Si));
mn.setTextSize(19.5f);
so RobotoCondensed-Bold is a font that i have in assets folder in my android project how to use that font instead of the regular Ariel....etc...etc...

Android custom typeface in chinese text

I am trying to change typeface in a Textview which shows a chinese text.
My code is:
chinesetext = (TextView)findViewById(R.id.textChinese);
Typeface tf = Typeface.createFromAsset(getAssets(),"fonts/DFXSM1B.ttf");
chinesetext.setTypeface(tf);
DFXSM1B.ttf is in my assets/fonts folder.
I am unable to see the textview typeface changed.
How can I change to a custom typeface in a chinese text ?
I am using paint to set typeface, it dosen't work for chinese text...
I used the font family is 思源宋体 Medium
and my code like this: (that is very simple)
var typeface = Typeface.createFromAsset(context.assets, "fonts/font1.otf")
paint.typeface = typeface
All the files in asset folder must have lower-cased names.
Rename the file in lower case (dfxsm1b.ttf) and it must work.

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);

Categories

Resources