Casa > H > How To Change The Fonts In The Android App I Am Developing

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

  1. TextView tv = new TextView(getBaseContext); 
  2. Typeface custom_font = Typeface.createFromAsset(getAssets(), "fonts/font name.ttf"); 
  3. tx.setTypeface(custom_font); 

2.Another is creating a custom TextView and use it in layout xml file

  1. android:id="@+id/textViewPlus1"  
  2. android:layout_height="match_parent"  
  3. android:layout_width="match_parent"  
  4. android:text="@string/showingOffTheNewTypeface" >  
  5.  

where TextViewPlus is a custom textview extending TextView

so TextViewPlus will be

  1. public class MyTextView extends TextView {  
  2. public MyTextView(Context context, AttributeSet attrs, int defStyle) 
  3. {  
  4. super(context, attrs, defStyle); init(); 
  5. }  
  6. public MyTextView(Context context, AttributeSet attrs)  
  7. super(context, attrs); init();  
  8. }  
  9. public MyTextView(Context context) 
  10. {  
  11. super(context); init(); } 
  12. private void init() { 
  13. Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "your_font.ttf"); 
  14. setTypeface(tf);  
  15. }  

De Nikos

O iPhone 11 Pro precisa de vidro temperado? :: Como é que o protector de ecrã se cola ao telefone?