Michal O
Michal O

Reputation: 27

Implement custom fonts in Listview- Android

I am trying to use custom font in my ListView adapter, but I am doing something wrongly.

I tried to use getAssets and getContext, but I can't use them in my app.
I hope that maybe someone can help me find the solution.

Adapter code in Java, as shown below:

public class ListviewAdapter extends BaseAdapter {

    private LayoutInflater mLayoutInflater;
    private ArrayList<Cwiczenie> listaCwiczen;

    public ListviewAdapter(Context context, ArrayList<Cwiczenie> data) {
        mLayoutInflater = LayoutInflater.from(context);
        listaCwiczen = data;
    }

    @Override
    public int getCount() {
        return listaCwiczen == null ? 0 : listaCwiczen.size();
    }

    @Override
    public Cwiczenie getItem(int position) {
        return listaCwiczen.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {

        View vi = view;
        ViewHolder holder = null;

        if (vi == null) {
            vi = mLayoutInflater.inflate(R.layout.wierszlisty, parent, false);
            holder = new ViewHolder();

            holder.tvName = (TextView) vi.findViewById(R.id.nazwa_cwiczenia);

            // holder.tvDescription = (TextView)
            // vi.findViewById(R.id.textView_item_description);
            vi.setTag(holder);
        } else {
            holder = (ViewHolder) vi.getTag();
        }

        Cwiczenie cwiczenie = getItem(position);

        holder.tvName.setText(cwiczenie.getNazwa());
        // holder.tvDescription.setText(item.getDescription());
        return vi;
    }

    static class ViewHolder {
        TextView tvName;
        // TextView tvDescription;
    }
}

Upvotes: 0

Views: 104

Answers (4)

Anand Singh
Anand Singh

Reputation: 5692

holder.tvName = (TextView) vi.findViewById(R.id.nazwa_cwiczenia);

below this use this code:

 Typeface tf = Typeface.createFromAsset(vi.getContext().getAssets(), "font/chiller.ttf");
 holder.tvName.setTypeface(tf);

create a "font" folder inside assets folder and place your font file there. Instead of "chiller.ttf" write your font file name.

This code worked for me. I hope it will work for you too.

Upvotes: 4

Vlad
Vlad

Reputation: 910

Well, when you call "getContext()" ,it won't get the context of the activity you are in ,therefor, it won't work.What you want, is to get the context sent via the constructor and use it.

So, declare a context:

Private Context context;

Than ,in your constructor, go for this:

this.context = context;

This will practically get the context sent via the constructor and set it to your local variable.

Now you can use it like this:

context.getAssets()...//and so on

Upvotes: 0

Blackbelt
Blackbelt

Reputation: 157447

keep a reference to Typeface, as member variable, and initialize it in the constructor of your adapter:

Typeface tf;
public ListviewAdapter(Context context, ArrayList<Cwiczenie> data) {
    tf = Typeface.createFromAsset(context.getAssets(),"fonts/yourfonts.ttf");  
    // other code
}

in getView() when you instantiate your row, assign the font

 if (vi == null) {
    vi = mLayoutInflater.inflate(R.layout.wierszlisty, parent, false);
    holder = new ViewHolder();
    holder.tvName = (TextView) vi.findViewById(R.id.nazwa_cwiczenia);
    holder.tvName.setTypeface(tf ,1);
    // holder.tvDescription = (TextView) vi.findViewById(R.id.textView_item_description);
    vi.setTag(holder);
} 

Upvotes: 2

almightyGOSU
almightyGOSU

Reputation: 3739

Since you pass in the context when calling the ListView constructor,

public ListviewAdapter(Context context, ArrayList<Cwiczenie> data) {
    mLayoutInflater = LayoutInflater.from(context);
    listaCwiczen = data;
}

Save the context into a variable? i.e.

// Allows you to use the context afterwards
private Context context = null;

public ListviewAdapter(Context context, ArrayList<Cwiczenie> data) {
    this.context = context;
    mLayoutInflater = LayoutInflater.from(context);
    listaCwiczen = data;
}

And you can use it to get your assets.

Upvotes: 1

Related Questions