Sunday, April 9, 2017

Add XML fonts in Android O

As we all know Android has released its Android O preview for developers,
now developer can download this preview version on there Android Studio version 2.4


To add different fonts in Android XML for Android API level <26
we used to follow this steps



  1. Go to the (project folder)
  2. Then app>src>main
  3. Create folder 'assets>fonts' into the main folder.
  4. Put your 'abc.ttf' into the fonts folder.
        AssetManager am = context.getApplicationContext().getAssets();
    
        typeface = Typeface.createFromAsset(am,
                String.format(Locale.US, "fonts/%s", "abc.ttf"));
    
        setTypeface(typeface);
    or try this way:
       TextView tx = (TextView)findViewById(R.id.textview1);
    
       Typeface custom_font = Typeface.createFromAsset(getAssets(),  "fonts/abc.ttf");
    
       tx.setTypeface(custom_font);


Now Google has made much simpler to add  and explore various fonts styles in Android studio 2.4

here are the steps to follow


  1. Go to the (project folder)
  2. res>right click > choose resource folder >type font > choose ok
    Screen Shot 2017-03-29 at 9.31.41 PM-min.png
    Screen Shot 2017-03-29 at 9.28.02 PM-min.png

Next step is to add the font styles to your font folder

you can download the various fonts style given by google here and each downloaded .ttf files to font folder

again to explore this fonts you can write in xml

    <TextView
        android:fontFamily="@font/indie_flower"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:text="@string/sample_text" />
If you want to use this fonts style programmatically you can call
Typeface typeface = getResources().getFont(R.font.indie_flower);
textView.setTypeface(typeface);

Now Google as made much easier to succeed

 

0 comments:

Post a Comment