I am trying to display arabic text with custom font. my code is as following:
package com.androidbook.test;
import java.io.BufferedInputStream;
import java.io.InputStream;
import org.apache.http.util.ByteArrayBuffer;
import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.widget.TextView;
public class ArabicTextActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.arabic_display);
TextView arabicTextView;
Typeface arabicFont;
arabicTextView = (TextView)findViewById(R.id.arabicTextDisplayTextView);
arabicFont =Typeface.createFromAsset(getAssets(),"me_quran.ttf");
arabicTextView.setTypeface(arabicFont);
arabicTextView.setTextSize(40);
InputStream is = getResources().openRawResource(R.raw.arabic_text);
BufferedInputStream bis = new BufferedInputStream(is);
int myInt=0;
try{
ByteArrayBuffer myByteArray = new ByteArrayBuffer(100);
while((myInt = bis.read())!=-1)
{
if(myInt == 10) break;
myByteArray.append((byte)myInt);
}
String arabicLine = new String(myByteArray.toByteArray(), 0, myByteArray.length(), "UTF-8");
arabicTextView.setText(arabicLine);
}catch(Exception ex)
{
ex.printStackTrace();
}
}
}
and arabic_display.xml is:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/quranDisplayItemLinearLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
android:id="@+id/arabicTextDisplayTextView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Arabic" />
</LinearLayout>
where res/raw/as arabic_text.txt is displayed in notepad as (font name: me_quran.ttf and font size 40)

but in simulator it is shown as:

you can notice that small characters (called aaraab characters in arabic) are not adjusted at all.. how should i fix this?
///////////////////////////////////////////////////////////////////
///////////
////
/////
RESULT AFTER USING DroidSansArabic.ttf

Judging by the charmap of the “me_quran.ttf” , it looks like what the simulator is showing is what it ought to show.
This font looks way too different from other arabic fonts that I’ve seen, you might want to get a hold on another font.