custom font in android ListView
Asked Answered
S

8

9

I'm using a custom font throughout my application (which, incidentally, I've frustratingly found out that you have to apply programmatically by hand to EVERY control!), and I need to apply it to a listview. The problem is that I can't see where I'd set the textview used in the list's font to my custom font (as I never instantiate it - that's all taken care of by the adapter).

What I'd ideally like is to be able to use an adapter like this:

new ArrayAdapter(Context context, TextView textView, List<T> objects)

That way I could do: textView.setTypeface before populating my list. Does anyone know if there's a way to do something along these lines?

Stanislaus answered 2/1, 2011 at 0:6 Comment(1)
You don't have to apply programmatically to every control. Simply create a custom MyFontTextView that extents TextView and set the type face in constructor- check the answer to this #2973770Chlordane
M
8

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.

Menstruation answered 2/1, 2011 at 0:12 Comment(2)
I thought that might do it, but was hoping there'd be an easier way! Thanks though - should be fine to implement!Stanislaus
yup - that's done the trick, and really wasn't that much work! I'll try to be less lazy before asking questions next time!Stanislaus
A
9

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

Atherton answered 15/11, 2012 at 21:14 Comment(0)
M
8

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.

Menstruation answered 2/1, 2011 at 0:12 Comment(2)
I thought that might do it, but was hoping there'd be an easier way! Thanks though - should be fine to implement!Stanislaus
yup - that's done the trick, and really wasn't that much work! I'll try to be less lazy before asking questions next time!Stanislaus
A
2

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.

Atrium answered 7/7, 2011 at 6:53 Comment(0)
E
2

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);
Etheleneethelin answered 16/10, 2013 at 6:23 Comment(0)
S
1

In addition to the response of Moisés Olmedo - an alternative variant without creating a new class:

    tf = Typeface.createFromAsset(getAssets(), fontPath);

    recordsAdapter = new SimpleCursorAdapter(this, R.layout.item1, cursor, from, to);

    recordsAdapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
        public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
            if (columnIndex == 1) {
                final TextView tv = (TextView) view;
                tv.setTypeface(tf);
            }
            return false;
        }
    });
Salvadorsalvadore answered 3/3, 2013 at 12:53 Comment(0)
C
0

First copy and paste the font files into assets/fonts folder. Then identify the textview.

    Typeface font=Typeface.createFromAsset(activity.getAssets(), "fonts/<font_file_name>.ttf");
    holder.text.setTypeface(font);
    holder.text.setText("your string variable here");
Clairclairaudience answered 13/2, 2012 at 5:49 Comment(0)
X
0

You can Set base Adapter like follow Steps May be help you

Define your Base Adapter

public class LessonAdapter extends BaseAdapter {

    private Context mContext;

    public LessonAdapter(Context mContext, ArrayList<String> titles) {
        super();
        this.mContext = mContext;
    }

    public int getCount() {
        // TODO Auto-generated method stub
        if (titles!=null)
            return titles.size();
        else
            return 0;
    }

    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return 0;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = convertView;
        try
        {
            if (v == null) {
                v = inflater.inflate(R.layout.lesson_item, null);
            }
                TextView title = (TextView) v.findViewById(R.id.title);

                Typeface tf = Typeface.createFromAsset(getAssets(),
                        "fonts/Rabiat_3.ttf");
                title.setTypeface(tf);

                title.setText(titles.get(position).toString());     

        }
        catch (Exception e) {
            Log.d("searchTest", e.getMessage());
        }

        return v;
    }

}

by TypeFace Method you can set your font by add folder 'Fonts' in Assets then add your font in 'Fonts' Folder

then to set your adapter

adapter = new LessonAdapter(LessonsTitle.this, titles);
    setListAdapter(adapter);
Xylotomous answered 2/9, 2013 at 13:3 Comment(0)
T
0

holder.txt_name.setTypeface(Typeface.createFromAsset(activity.getAssets(), "font/nasimbold.ttf"));

Trapan answered 4/6, 2015 at 5:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.