Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

custom font for textview in ArrayAdapter

Tags:

android

I'm trying to change the font of a TextView in my ArrayAdapter. The font chantelli_antiqua.ttf is in the assets folder.

Here is my Java code:

listItemAdapter = new ArrayAdapter<MenuItem>(this, R.layout.listitem, menuItems);

Typeface font = Typeface.createFromAsset(getAssets(), "chantelli_antiqua.ttf");  
TextView v = (TextView)listItemAdapter.getView(0, null, null);
v.setTypeface(font);

xml for the listitem layout:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dp"
    android:textSize="30sp"
/>

I'm quite sure the problem lies with the Adapter.getView(int, View, ViewGroup) method. I just don't really understand what to pass as variables and tried null. But this doesn't do what I would like it to.

How to change the font of the TextView in the Adapter to the custom font?

Update

According to Pixie's suggestion I created a MenuItemAdapter which extends ArrayAdapter<MenuItem>:

public class MenuItemAdapter extends ArrayAdapter<MenuItem>
{
    private Typeface font;

    public MenuItemAdapter(Context context, int textViewResourceId, List<MenuItem> objects) 
    {
        super(context, textViewResourceId, objects);

        font = Typeface.createFromAsset(context.getAssets(), "chantelli_antiqua.ttf"); 
    }

    @Override  
    public View getView(int position, View view, ViewGroup viewGroup)
    {
        ((TextView)view).setTypeface(font);
        return super.getView(position, view, viewGroup);
    }
}

And changed my java code to:

listItemAdapter = new MenuItemAdapter(this, R.layout.listitem, menuItems);

But now my app crashes after the onCreate of the ListActivity, but before hitting the breakpoint in getView(...), I haven't been able to figure out yet why. Any suggestion?

Update2

Changed the code for getView(...) to:

@Override  
public View getView(int position, View view, ViewGroup viewGroup)
{
 View v = super.getView(position, view, viewGroup);
 ((TextView)v).setTypeface(font);
 return v;
}

and this works. :)

like image 388
Bazzz Avatar asked May 06 '11 12:05

Bazzz


2 Answers

You shouldn't call the getView() method of your adapter. The ListView does this for you. You have to extend the ArrayAdapter class and override the getView() method instead. In this method you have to inflate a new view or re-use convertView and set the typeface for this view.

like image 98
Michael Avatar answered Sep 20 '22 13:09

Michael


I think the problem is in return super.getView(position, view, viewGroup); at the end of getView() method. I think it should be like this

 @Override  
public View getView(int position, View view, ViewGroup viewGroup)
{
    TextView tv = ((TextView)view).setTypeface(font);
    tv.setText(<String> getItem());
    return tv;
}

please note this code is example I didn't try it now but I made custom arrayAdapter before and it was something like that.
Here is a tutorial describing how to create custom arrayAdapter.

like image 40
Amal Avatar answered Sep 23 '22 13:09

Amal