Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Android
How to use custom font in listview in android?
Posted 4-Mar-12 6:56am

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

This StackOverflow question[^] has the answer[^].
 
Answer states:
You can't do it that way because the text view resource you pass to the ArrayAdapter is inflated each time it is used.
 
You need to create your own adapter and provide your own view.
 
An example for your adapter could be
public class MyAdapter extends BaseAdapter {
 
private List<Object>        objects; // obviously don't use object, use whatever you really want
private final Context   context;
 
public CamAdapter(Context context, List<Object> objects) {
    this.context = context;
    this.objects = objects;
}
 
@Override
public int getCount() {
    return objects.size();
}
 
@Override
public Object getItem(int position) {
    return objects.get(position);
}
 
@Override
public long getItemId(int position) {
    return position;
}
 
@Override
public View getView(int position, View convertView, ViewGroup parent) {
 
    Object obj = objects.get(position);
 
    TextView tv = new TextView(context);
    tv.setText(obj.toString()); // use whatever method you want for the label
    // set whatever typeface you want here as well
    return tv;
}
 
}
And then you could set that as such
ListView lv = new ListView(this);
lv.setAdapter(new MyAdapter(objs));
Hopefully that should get you going.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 6,045
1 DamithSL 4,611
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,310


Advertise | Privacy | Mobile
Web03 | 2.8.141220.1 | Last Updated 13 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100