- Create new android project and name it AmharicFont
- Click next and leave everything as it is by default
- Open activity_main layout window drag and drop TextView and on the property window change id to textAmharic. The xml should look like this
<TextView
android:id="@+id/textAmharic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="14dp"
android:layout_marginTop="30dp"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" /> - Now define amharic string on the resource to do this, go to res/values/strings.xml file and the following line
<string name="wellcom_msg">እንኳን ደህና መጡ!</string> - Navigate to your project assets folder and create new folder named fonts inside
- Right click on fonts folder -> Import -> General ->File systems -> Navigate to your amharic font file and click ok. Now you have successfully embedded your font
- Open your java file named MainActivity.java in src folder
- Next to setContentView(R.layout.activity_main) put the following code snippet
TextView tv=(TextView)findViewById(R.id.textAmharic);
tv.setText(getResources().getString(R.string.welcome_msg));
Typeface tf=Typeface.createFromAsset(getAssets(),"fonts/NYALA.TTF");
tv.setTypeface(tf);
Now run your application and enjoy.
This will generate lots of TypeFace objects and eat up memory AND pre-4.0 Android doesn't free up TypeFaces properly - a bug i assume. Take a look at this dicussion http://code.google.com/p/android/issues/detail?id=9904
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteThanks,I will check.
DeleteTo accommodate the aforementioned issue you can do the following
ReplyDelete1.create java class which will create typeface instance and store in the catch and whenever a typeface is requested it will check if its existence in the catch and return or create a new one store it in the catch and return. you can achieve this by the following java class.
public class TypeFaces {
private static final String TAG = "TypeFaces";
private static final Hashtable cache = new Hashtable();
public static Typeface get(Context c, String assetPath) {
synchronized (cache) {
if (!cache.containsKey(assetPath)) {
try {
Typeface t = Typeface.createFromAsset(c.getAssets(),
assetPath);
cache.put(assetPath, t);
} catch (Exception e) {
Log.e(TAG, "Could not get typeface '" + assetPath
+ "' because " + e.getMessage());
return null;
}
}
return cache.get(assetPath);
}
}
}
2. Then you have to change the code snippet shown in A with code snippet shown in B
A. tf=Typeface.createFromAsset(getAssets(),"fonts/NYALA.TTF");
//Note that this is for demonstration purpouse
B.tf=TypeFaces.get(getApplicationContext(), "fonts/NYALA.TTF")
3. This one works fine for me and if there is another simple please forward.
For more information you can also refer the link mentioned in the discussion above. (http://code.google.com/p/android/issues/detail?id=9904)
Thanks Banti, its really helpful...cheers.
ReplyDeleteYou also can apply chosen typeface to all textViews recursively.
ReplyDelete