custom font in android ListView

Solution 1:

If you don't want to create a new class you can override the getView method when creating your Adapter, this is an example of a simpleAdapter with title and subtitle:

Typeface typeBold = Typeface.createFromAsset(getAssets(),"fonts/helveticabold.ttf");
Typeface typeNormal = Typeface.createFromAsset(getAssets(), "fonts/helvetica.ttf");

SimpleAdapter adapter = new SimpleAdapter(this, items,R.layout.yourLvLayout, new String[]{"title",
    "subtitle" }, new int[] { R.id.rowTitle,
    R.id.rowSubtitle }){
            @Override
        public View getView(int pos, View convertView, ViewGroup parent){
            View v = convertView;
            if(v== null){
                LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v=vi.inflate(R.layout.yourLvLayout, null);
            }
            TextView tv = (TextView)v.findViewById(R.id.rowTitle);
            tv.setText(items.get(pos).get("title"));
            tv.setTypeface(typeBold);
            TextView tvs = (TextView)v.findViewById(R.id.rowSubtitle);
            tvs.setText(items.get(pos).get("subtitle"));
            tvs.setTypeface(typeNormal);
            return v;
        }


};
listView.setAdapter(adapter);

where items is your ArrayList of Maps

hope that helps

Solution 2:

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.

Solution 3:

Looks like the constructor is wrong

change it to:

public MyAdapter (Context context, List<Object> objects) {
    this.context = context;
    this.objects = objects;
}

it worked well for me.

Solution 4:

Try like this for arrayadapters::

Typeface typeNormal = Typeface.createFromAsset(getAssets(), "roboto_lite.ttf");

timearray = new ArrayAdapter<String>(DetailsActivity.this,R.layout.floorrow,R.id.txt, flor) {
    public View getView(int pos, View convertView, android.view.ViewGroup parent) {
        View v = convertView;
        if (v == null) {
            LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.floorrow, null);
        }
        TextView tv = (TextView)v.findViewById(R.id.txt);
        tv.setText(flor.get(pos));
        tv.setTypeface(typeNormal);
        return v;
    }; 
};

lv_building.setAdapter(timearray);