Thursday , November 21 2024

Android Custom Fonts Tutorial

In android, you can define your own custom fonts for the strings in your application. You just need to download the required font from the internet, and then place it in assets/fonts folder.

After putting fonts in the assets folder under fonts folder, you can access it in your java code through Typeface class. First , get the reference of the text view in the code. Its syntax is given below −

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

The next thing you need to do is to call static method of Typeface class createFromAsset() to get your custom font from assets. Its syntax is given below −

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

The last thing you need to do is to set this custom font object to your TextView Typeface property. You need to call setTypeface() method to do that. Its syntax is given below −

tx.setTypeface(custom_font);
Example

1) MainActivity.java

package com.w2class.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);
}
}

2) 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=”W2class”
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=”W2class”
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=”W2class”
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>

3) 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>

4) AndroidManifest.xml

<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android”
package=”com.w2class.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>

 

Preview

font

About admin

Check Also

Binding JavaScript and Android Code – Example

When developing a web application that’s designed specifically for the WebView in your Android application, …

Leave a Reply