listview - Custom Font in ExpandableListView Android

I wanted to make custom font for one of my textview but i'm still getting

cannot resolve method getAssets()

Here is a part of code:

    @Override
public View getChildView(int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
    final String childText = (String)getChild(groupPosition, childPosition);
    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater)this._context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.list_item, parent, false);
    }
    Utils.setFontAllView((ViewGroup)convertView);

    TextView txtListChild   = (TextView)convertView.findViewById(R.id.txtcardname);
    txtListChild.setTypeface(null, Typeface.BOLD);

    TextView kategoria  = (TextView)convertView.findViewById(R.id.kategoria);
    kategoria.setText(this.KATEGORIE[numerKategori]);


    custom_font = Typeface.createFromAsset(getAssets(), "fonts/capture.ttf");
    txtListChild.setTypeface(custom_font);

I want to make it with this code:

custom_font = Typeface.createFromAsset(getAssets(), "fonts/capture.ttf");
        txtListChild.setTypeface(custom_font);

3 Answers

  1. Laurent- Reply

    2019-11-15

    update your code to this it will work:

    custom_font = Typeface.createFromAsset(mContext.getAssets(), "fonts/capture.ttf");
    

    You can't access your assets without context of your view. So Context is mandatory

    instead of setting the fonts inside getView method you can define fonts in your custom textview class, by using this it will take less memory and your app performance will be better.

  2. Lawrence- Reply

    2019-11-15

    You can call getAssets with a context. Use this:

    custom_font = Typeface.createFromAsset(this._context.getAssets(), "fonts/capture.ttf");
    

    Good luck.

  3. Leander- Reply

    2019-11-15

    As you want to use it into Adapter from your code

    Do the below changes

    Use Context

    Make Sure in your asset folder under fonts there is file of capture.ttf

    check Path for file capture.ttf

     Context context = null;
                Typeface custom_font = Typeface.createFromAsset(context.getAssets(), "fonts/capture.ttf");
          txtListChild.setTypeface(custom_font);
    

Leave a Reply

Your email address will not be published. Required fields are marked *

You can use these HTML tags and attributes <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>