Android Custom Fonts

Android Custom Fonts

Android Custom Fonts that you can define your own for the string in your android . You will need to download the mandatory font from the web just, and then stick it in Android fonts folder.

android custom fonts

After adding fonts in the resources folder under fonts folder, you can get access to it in your java code through Typeface class. First , have the reference of the written text view in the code. Its syntax is listed below

TextView tx = (TextView)findViewById(R.id.textview1);

The following point you must do is to call static approach to Typeface class createFromAsset() to really get your android custom fonts from belongings. Its syntax is listed below

Typeface custom_font = Typeface.createFromAsset(getAssets(), "fonts/font name.ttf");

The very last thing you must do is to create this android custom fonts subject to your TextView Typeface property. You will need to call setTypeface() solution to do this. Its syntax is listed below

tx.setTypeface(custom_font);

 

From these Methods apart, there are other methods described in the Typeface, which you can use to take care of Android Fonts better.

Methods & Descriptions

create(String familyName, int style)

Make a Typeface subject given a family group name with option style info

create(Typeface family, int style)

Build a Typeface thing that best suits the given existing Typeface and the given Style

createFromFile(String path)

Create a fresh Typeface from the given font file

defaultFromStyle(int style)

Comes back one of the default Typeface things, predicated on the given style

getStyle()

Profits the Typeface’s intrinsic style attributes

Android Custom Fonts Example

Here is a good android custom fonts example demonstrating the utilization of Typeface to take care of CustomFont. It generates a basic program that presents a custom font that you given in the android fonts data file.

To test out this android custom fonts example, you can run this on a genuine device or within an emulator.

Steps     Description

  1. You use Android studio IDE to generate an Android App under a package deal com.example.sappal.myapplication.
  2. Download  fonts from internet and put it under Android fonts folder.
  3. Modify src/MainActivity.java data file to include necessary code.
  4. Modify the res/structure/activity_main to include particular XML components
  5. Run the application form and choose a jogging google android device and mount the application onto it and check the results

Following is this content of the customized main activity MainActivity.java.

package com.example.sappal.myapplication;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;

import android.graphics.Typeface;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;

import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity {
   TextView tv1,tv2;
   
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      
      tv1=(TextView)findViewById(R.id.textView3);
      tv2=(TextView)findViewById(R.id.textView4);
      
      Typeface face= Typeface.createFromAsset(getAssets(), "font/font.ttf");
      tv1.setTypeface(face);
      
      Typeface face1= Typeface.createFromAsset(getAssets(), "font/font1.ttf");
      tv2.setTypeface(face1);
   }
   
   @Override
   public boolean onCreateOptionsMenu(Menu menu) {
      // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(R.menu.menu_main, menu);
      return true;
   }
   
   @Override
   public boolean onOptionsItemSelected(MenuItem item) {
      // Handle action bar item clicks here. The action bar will
      // automatically handle clicks on the Home/Up button, so long
      // as you specify a parent activity in AndroidManifest.xml.
      
      int id = item.getItemId();
      
      //noinspection SimplifiableIfStatement
      if (id == R.id.action_settings) {
         return true;
      }
      return super.onOptionsItemSelected(item);
   }
}

 

Given Below  is the modified content of the xml activity_main.xml.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
   android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
   
   <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Typeface"
      android:id="@+id/textView"
      android:layout_alignParentTop="true"
      android:layout_centerHorizontal="true"
      android:textSize="30dp" />
      
   <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Andro Development"
      android:id="@+id/textView2"
      android:layout_below="@+id/textView"
      android:layout_centerHorizontal="true"
      android:textSize="35dp"
      android:textColor="#ff16ff01" />
      
   <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Andro Development"
      android:id="@+id/textView3"
      android:layout_centerVertical="true"
      android:textSize="45dp"
      android:layout_alignParentRight="true"
      android:layout_alignParentEnd="true"
      android:layout_alignParentLeft="true"
      android:layout_alignParentStart="true" />
      <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Andro Development"
      android:id="@+id/textView4"
      android:layout_below="@+id/textView3"
      android:layout_alignLeft="@+id/textView3"
      android:layout_alignStart="@+id/textView3"
      android:layout_marginTop="73dp"
      android:textSize="45dp" />

  
    
</RelativeLayout>

 

Given Below is the code of the res/values/string.xml.

<resources>
   <string name="app_name">My Application</string>
   <string name="hello_world">Hello world!</string>
   <string name="action_settings">Settings</string>
</resources>

 

Given Below is the content of AndroidManifest.xml file.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.example.sappal.myapplication" >
   
   <application
      android:allowBackup="true"
      android:icon="@mipmap/ic_launcher"
      android:label="@string/app_name"
      android:theme="@style/AppTheme" >
      
      <activity
         android:name=".MainActivity"
         android:label="@string/app_name" >
         
         <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
         </intent-filter>
         
      </activity>
      
   </application>
</manifest>

 

Let’s make an effort to run our Android Custom Fonts App we just developed. I assume you’d created your AVD while doing Android Environment Setup. To perform the software from Android studio, available one of your project’s activity and click Eclipse Run icon from the toolbar.Google android studio room installs the software on your AVD and starts off it if everything is okay with your installation and application, it’ll screen pursuing Emulator home window

android custom fonts

As you can plainly see that the written text that made an appearance on the AVD hasn’t a default android fonts, rather the custom is possessed because of it font that you given in the android fonts folder.

Note – You will need to care for the scale and the type backed by the font , when working with android custom fonts.