How to change fontFamily of TextView in Android ?
A1.Java-first copy .ttf in Assets folder
TextView tv = (TextView) findViewById(R.id.appname);
Typeface face = Typeface.createFromAsset(getAssets(),
"fonts/epimodem.ttf");
tv.setTypeface(face);
A2:
Within the *.xml you can combine the stock fonts with the following functions, not only with typeface:
android:fontFamily="serif"
android:textStyle="italic"
With this two styles, there was no need to use typeface in any other case. The range of combinations is much more bigger with fontfamily&textStyleA3.
You set style in res/layout/value/style.xml
like that:
<style name="boldText">
<item name="android:textStyle">bold|italic</item>
<item name="android:textColor">#FFFFFF</item>
</style>
use this style in main.xml
file use:
style="@style/boldText"
A4.
Here is an easier way that can work in some cases. The principle is to add a not visible TextVview in your xml layout and to get its typeFace in the java code.
The layout in the xml file:
<TextView
android:text="The classic bread is made of flour hot and salty. The classic bread is made of flour hot and salty. The classic bread is made of flour hot and salty."
android:layout_width="0dp"
android:layout_height="0dp"
android:fontFamily="sans-serif-thin"
android:id="@+id/textViewDescription"/>
A5.
Dynamically you can set the fontfamily similar to android:fontFamily in xml by using this,
These are the list of default font family used, use any of this by replacing the double quotation string "sans-serif-medium"
"mycustomfont.ttf" is the ttf file. Path will be in src/assets/fonts/mycustomfont.ttf . |
Comments
Post a Comment