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

 

Related Posts:

  • How to use SVG images in Android Studio Support Library This technique requires Android Support Library 23.2 or higher and Android Plugin for Gradle 2.0 or higher, and uses vector drawabl… Read More
  • Android What is Activity ? An activity is an application component that provides a screen with which users can interact in order to do something, What is … Read More
  • File upload using OkHttp and Cookie Manager in Android OkHttp is a modern application network,  OkHttp will help making network calls efficiently, makes our stuffs load faster and saves the Band… Read More
  • Android2 Can we change application name in after deployment? Things That Cannot Change: The most obvious and visible of these is the “manifest package nam… Read More
  • Android Errors and Basic Short Answer Gradle is a build system. Long Answer Before Android Studio you were using Eclipse for your development purposes, and, chances ar… Read More

0 comments:

Post a Comment