I am using ImageAdapter extends BaseAdapter
to inflate a gridview
. Gridview has two textview
s. I want to set custom font for one of them. Using Typeface font = Typeface.createFromAsset(getAssets(), "BABYCAKE.TTF");
in ImageAdapter
gives error The method getAssets() is undefined for the type ImageAdapter
.
ImageAdapter
is defined as
package com.amit.wozzle;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.view.LayoutInflater;
public class ImageAdapter extends BaseAdapter
{
private ArrayList<String> listCountry;
private ArrayList<String> scorestage;
private Activity activity;
Typeface font;
public ImageAdapter(Activity activity,ArrayList<String> listCountry, ArrayList<String> scorestage) {
super();
this.listCountry = listCountry;
this.scorestage = scorestage;
this.activity = activity;
font = Typeface.createFromAsset(activity.getAssets(), "BABYCAKE.TTF");
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return listCountry.size();
}
@Override
public String getItem(int position) {
// TODO Auto-generated method stub
return listCountry.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
public static class ViewHolder
{
public ImageView imgViewFlag;
public TextView txtViewTitle;
public TextView txtViewTitle2;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder view;
LayoutInflater inflator = activity.getLayoutInflater();
if(convertView==null)
{
view = new ViewHolder();
convertView = inflator.inflate(R.layout.grid_item, null);
view.txtViewTitle = (TextView) convertView.findViewById(R.id.textView1);
view.txtViewTitle2.setTypeface(font);
view.txtViewTitle2 = (TextView) convertView.findViewById(R.id.textView2);
// view.imgViewFlag = (ImageView) convertView.findViewById(R.id.imageView1);
convertView.setTag(view);
}
else
{
view = (ViewHolder) convertView.getTag();
}
// view.txtViewTitle2.setBackgroundColor(Color.BLUE);
view.txtViewTitle2.setText(listCountry.get(position));
view.txtViewTitle.setText(scorestage.get(position));
// view.imgViewFlag.setImageResource(scorestage.get(position));
return convertView;
}
}
Please help.
try
Typeface font = Typeface.createFromAsset(activity.getAssets(), "BABYCAKE.TTF");
Why are you creating Typeface
object inside getView
. Typeface
takes lot of memory and it will slow down your app as less memory space will be available due to number of Typeface objects created inside getView
.
Instead Font file should be created only one time and reused when needed. Create font file outside of getView
. Declare it as an instance variable inside your adapter if you use it only inside adapter and initialise it inside your adapter constructor. And instead of creating new instance each time inside getView use that single instance to set font.
And for your error use activity instance variable to call getAssests()
TypeFace font = Typeface.createFromAsset(activity.getAssets(), "fonts/BABYCAKE.TTF");
Edit- Try to use it like this-
class DemoFonts{
private static TypeFace typeFace;
public static TypeFace getTypeFace(Context mContext){
if(typeface==null){
typeface = Typeface.createFromAsset(mContext.getAssets(), "fonts/BABYCAKE.TTF");
}
return typeface;
}
}
Try to use like above. Assuming that you have fonts
folder inside assets folder.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With