4

I have a custom font for my android activity.

MainActivity.class

    private void initControls() {
    // TODO Auto-generated method stub
    header = (TextView) findViewById (R.id.tvAccommodations);
    lv = (ListView) findViewById (R.id.lvAccommodations);
    text = (TextView) findViewById (R.id.textView);

    Typeface tf = Typeface.createFromAsset(getAssets(),
            "fonts/heartbre.ttf");
    header.setTypeface(tf);
    text.setTypeface(tf);



    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
                    R.array.abra_hotel, R.layout.custom_list_text);
            lv.setAdapter(adapter);
            header.setText(value);

custom_list_text.xml

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/list_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textStyle="bold"
android:text="@string/app_name"
android:paddingLeft="6dip" />

Android throws NullPointerException. Why is it so? Any help is appreciated. Thanks.

LOGCAT:

    03-08 19:48:03.859: E/AndroidRuntime(413): Caused by: java.lang.NullPointerException
    03-08 19:48:03.859: E/AndroidRuntime(413):  at com.say.philippineexplorer.PlaceAccommodations.initControls(PlaceAccommodations.java:34)
    03-08 19:48:03.859: E/AndroidRuntime(413):  at com.say.philippineexplorer.PlaceAccommodations.onCreate(PlaceAccommodations.java:22)

2 Answers2

10

Here is the custom adapter class and the constructor

class CustomAdapter extends ArrayAdapter<CharSequence>{

    Context context; 
    int layoutResourceId;    
    CharSequence data[] = null;
    Typeface tf; 

public CustomAdapter(Context context, int layoutResourceId, CharSequence[] data, String FONT ) { 
    super(context, layoutResourceId, data);
    this.layoutResourceId = layoutResourceId;
    this.context = context;
    this.data = data;
    tf = Typeface.createFromAsset(context.getAssets(), FONT);
}   

Put the font you want to use in your assets folder and fill your listview like this:

listAdapter = new CustomAdapter(this, R.layout.custom_list_text, R.array.abra_hotel, "name_of_font.ttf");
Zabador
  • 607
  • 3
  • 12
  • It is not working for my adapter, here is the SO: http://stackoverflow.com/questions/21439707/typeface-not-taking-place-in-custom-arrayadapter?answertab=active#tab-top – vlio20 Jan 29 '14 at 19:02
  • 1
    You created a property called `tf` and set a `Typeface` to it, but `tf` isn't assigned to anything. How does this code set the font of the item renderer? – William Grand Dec 16 '14 at 21:56
  • I put font sin "src/assets/fonts/myfont.ttf" and passed into the adapter "fonts/myfont.ttf" and it worked. Thanks! – Anna Billstrom Nov 04 '15 at 17:26
2

you can't apply font directly to listview and you need to create custom adapter for listview and change it font for more details click below stack post it's already discussed.

How to change color and font on ListView

Community
  • 1
  • 1
Dinesh
  • 6,500
  • 10
  • 42
  • 77