Sunday, June 17, 2012

Add Sinhala Fonts to Android Application

Now I'm going to tell how add sinhala fonts to an android application. So we need sinhala fonts TTF files.
This techniques can be used for other language also, so basically what do we need is any language TTF files.
Firstly, create an android project, my sample project is named as 'LanguageApp'
Then you can add any sinhala TTF file to the assets directory in the project(copy and paste).
I'm going to create a sample Sinhala to English converter using button event. So I have create main.xml file using two edit text filed and a button.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:weightSum="1">
    <LinearLayout android:layout_width="match_parent" android:id="@+id/linearLayout1" android:orientation="vertical" android:layout_height="wrap_content">
        <EditText android:layout_height="wrap_content" android:id="@+id/editText1" android:layout_width="match_parent">
            <requestFocus></requestFocus>
        </EditText>
    </LinearLayout>
    <Button android:text="Tnkak" android:layout_height="wrap_content" android:id="@+id/button1" android:layout_width="104dp"></Button>
    <EditText android:layout_height="wrap_content" android:id="@+id/editText2" android:layout_width="match_parent"></EditText>
</LinearLayout>
In this button, I have given like android:text="Tnkak", What I do is, this English letters are related to Sinhala ඔබන්න(Click) word.

This is my LanguageAppActivity class

package com.languageapp;
import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class LanguageAppActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */ 
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button myButton=(Button)findViewById(R.id.button1);
        Typeface type=Typeface.createFromAsset(getAssets(), "ARADANA.TTF");
        myButton.setTypeface(type);
        myButton.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
    // TODO Auto-generated method stub
    Typeface tf = Typeface.createFromAsset(getAssets(),"ARADANA.TTF");
    EditText et=(EditText)findViewById(R.id.editText2);
    et.setTypeface(tf);
    EditText editText=(EditText)findViewById(R.id.editText1);
    et.setText(editText.getText());
    } 
}
I have highlighted to show that how I change font to Sinhala.
After run the application we can see like this output and give some English words to edit text and click the button. You can see convert English fonts related to those Sinhala fonts.
I have uploaded sample project, 
LanguageApp.zip

5 comments:

  1. hey this is really good one bro. patta

    ReplyDelete
  2. ela kiri..patta..and thank you for sharing this..

    ReplyDelete
  3. There's no need to constantly search for the best online CBD store because Earthsaxis has you covered.The decision to Buy Best CBD Products is not an easy one. Choosing the right products can feel daunting, especially when.there is so much in the market.

    ReplyDelete
  4. At Master Livery Services, we are partnered with travel affiliates across the globe, ensuring you can enjoy luxurious Limo service Boston, ground transportation anywhere, at any time.
    boston logan car service
    wedding limo service boston
    worldwide airport transfer
    car service to logan airport from south shore
    car service to logan airport from ri

    ReplyDelete