Question

I have a ListView that uses SimpleAdapter, each row has 2 TextViews, and I would like to use an external font to one of the two TextView.. Here is the code of the class:

  public class QuranEnglish extends Activity {
<...>
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.quran_english);

    listview=(ListView) findViewById(R.id.QuranEnglishListView);    
    <..add into list..>
    SimpleAdapter adapter = new SimpleAdapter(
            this,
            list,
            R.layout.quran_english_row,
            new String[] {"Arabic","English"},
            new int[] {R.id.QuranEnglishTextViewArabic,R.id.QuranEnglishTextViewEnglish});

    listview.setAdapter(adapter);

xml for each row (quran_english_row.xml):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent" android:orientation="vertical">
    <LinearLayout android:gravity="right" android:layout_height="wrap_content" android:id="@+id/linearLayout1" android:layout_width="fill_parent">
        <TextView android:textSize="21px" android:text="TextView" android:id="@+id/QuranEnglishTextViewArabic" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
    </LinearLayout>

    <TextView android:textSize="21px" android:text="TextView" android:id="@+id/QuranEnglishTextViewEnglish" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>

</LinearLayout>

xml for the layout (quran_english.xml):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent" android:orientation="vertical">
    <ListView android:id="@+id/QuranEnglishListView" android:layout_width="fill_parent" android:layout_height="fill_parent"></ListView>

</LinearLayout>

So how can I put the external typeface of the TextView that is in the row (quran_english_row.xml)? I tried using this line though it crashed my application:

((TextView) findViewById(R.id.QuranEnglishTextViewArabic)).setTypeface(Typeface.createFromAsset(getAssets(),"dejavusans.ttf"));
Was it helpful?

Solution

R.id.QuranEnglishTextViewArabic 

is not an id of one of your views here. It is an id of a example view, which is used to fill the list. In order to change the font, you need to get one view of your list. This works if you have chosen one of your views:

TextView view = getSelectedView();
if (view != null) {
  view.setTypeface(Typeface.createFromAsset(getAssets(),"dejavusans.ttf"));
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top