How to change the fonts in the Android App I am developing
You can change the fonts in Android app in two ways one is static way by defining its in styles.xml and another one is declaring at runtime in class files.
1. By declaring in class files
copy fontsname.ttf file in assets(src/main/assets) folder
- TextView tv = new TextView(getBaseContext);
- Typeface custom_font = Typeface.createFromAsset(getAssets(), "fonts/font name.ttf");
- tx.setTypeface(custom_font);
2.Another is creating a custom TextView and use it in layout xml file
- android:id="@+id/textViewPlus1"
- android:layout_height="match_parent"
- android:layout_width="match_parent"
- android:text="@string/showingOffTheNewTypeface" >
where TextViewPlus is a custom textview extending TextView
so TextViewPlus will be
- public class MyTextView extends TextView {
- public MyTextView(Context context, AttributeSet attrs, int defStyle)
- {
- super(context, attrs, defStyle); init();
- }
- public MyTextView(Context context, AttributeSet attrs)
- {
- super(context, attrs); init();
- }
- public MyTextView(Context context)
- {
- super(context); init(); }
- private void init() {
- Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "your_font.ttf");
- setTypeface(tf);
- }
- }
Artigos semelhantes
- What is the difference between Medium, Demi- and Semibold fonts?
- What are good Android folder based audio players for books/podcasts with speed change and remember last playback position?
- How to change a text button in jQuery
- Is there any way in c to change the value of a global variable through a function without passing it to the function?