Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot resolve method getAssets() while adding custom font

Tags:

android

assets

I want to add custom font to the button (LetterBtn) in my Adapter that extends BaseAdapter, but I get an error with getAssets() as "cannot resolve method 'getAssets()'".

The font is in the assets/fonts/ folder, it works fine in my other Activity.

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

    Button letterBtn;

    if (convertView == null) {
        letterBtn = (Button)letterInf.inflate(R.layout.letter, parent, false);
    } else {
        letterBtn = (Button)convertView;
    }

    letterBtn.setText(letters[position]);


Typeface font1 = Typeface.createFromAsset(getAssets(), 
"fonts/nexalight.otf");
letterBtn.setTypeface(font1);

    return letterBtn;

}
like image 966
prezli Avatar asked Dec 14 '22 23:12

prezli


1 Answers

I think you are facing this problem in your Adapter class. Update your Adapter class as below:

....................
................................

public class YourAdapter extends ......
{
    .........
    ..............

    Typeface font;

    public YourAdapter(Context context, ...........)
    {
        super(context, ..........);

        font = Typeface.createFromAsset(context.getAssets(), "fonts/nexalight.otf");
    }

    ...............  
    ........................

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


        if (convertView == null) {
            convertView = letterInf.inflate(R.layout.letter, parent, false);
        } 

        Button letterBtn = (Button ) convertView.findViewById(R.id.YOUR_BUTTON_ID);

        letterBtn.setText(letters[position]);
        letterBtn.setTypeface(font);

        return convertView;
    }

    ...................
    .........................
}
like image 151
Ferdous Ahamed Avatar answered Feb 13 '23 06:02

Ferdous Ahamed