Question

Hi I need a little help with using a custom typeface for a listview items. I'm getting my typeface from assets folder like this :

Typeface font = Typeface.createFromAsset(getAssets(), "hermesbgbold.otf"); 

And I'm trying to set it for my listview items, but the problem is that I'm using a SimpleAdapter for my ListView and the TextView's are in another XML , which I'm using as a contentView for my list View. Here is the code for better understanding :

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.events);
// code

SimpleAdapter adapter = new SimpleAdapter(this, items, R.layout.events_items,
                new String[]{ICON,TITLE, INFO}, new int[]{ R.id.thumb,R.id.title, R.id.dates})

}

So, the textview's which I want to use with custom typeface are in events_items.xml. So how can I set title and dates to use this custom typeface?

Was it helpful?

Solution

Create your own custom adapter and set font for the text.

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // ViewHolder will buffer the assess to the individual fields of the row
    // layout

    ViewHolder holder;
    // Recycle existing view if passed as parameter
    // This will save memory and time on Android
    // This only works if the base layout for all classes are the same
    View rowView = convertView;
    if (rowView == null) {

        LayoutInflater inflater = context.getLayoutInflater();
        rowView = inflater.inflate(R.layout.main_listview, null, true);

        holder = new ViewHolder();
        holder.textView = (TextView) rowView.findViewById(R.id.main_name);
        rowView.setTag(holder);

    } else {
        holder = (ViewHolder) rowView.getTag();
    }

    Typeface font = Typeface.createFromAsset(getAssets(), "hermesbgbold.otf"); 
    holder.textView.setTypeface(font);
    holder.textView.setText(title);

    return rowView;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top