1

I having this custom array adapter:

public class CustomAdapter extends ArrayAdapter<DataObj> 
{
int                     layoutResourceId;
List<DataObj>   data;
Activity                activity;
Typeface                iconFont;

public CustomAdapter(Activity activity, int layoutResourceId, List<DataObj> data)
{
    super(activity, layoutResourceId, data);
    this.layoutResourceId = layoutResourceId;
    this.activity = activity;
    this.data = data;
    iconFont = Typeface.createFromAsset(activity.getAssets(), "icons.ttf" );
}

public static class ViewHolder
{
    public TextView icon;
    public TextView name;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) 
{
    View row = convertView;
    ViewHolder holder;

    if (row == null) 
    {
        LayoutInflater layoutInflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row = layoutInflater.inflate(R.layout.event_type_list_row, null);
        holder = new ViewHolder();
        holder.icon = (TextView) row.findViewById(R.id.layout_icon_EditText);
        holder.name = (TextView) row.findViewById(R.id.layout_name_EditText);
        row.setTag(holder);
    }
    else
    {
        holder = (ViewHolder) row.getTag();
    }
    final DataObj obj = data.get(position);
    if (eventType != null) 
    {
        holder.icon.setText(obj.getIconCode());
        holder.icon.setTypeface(iconFont);
        holder.name.setText(obj.getName());
    }

    return row;
}

}

Instead of icon I see code (I am using IcoMoon as my icon font). Other icons in my app that are created statically are seen, But here I see this: enter image description here what is wrong with my code?

Here is how it works when it is static (it is not an image it is the font of IcoMoon): enter image description here

Note: I tried to use other fonts (not icon fonts) and it worked, it seems that only icon fonts not working.

vlio20
  • 8,955
  • 18
  • 95
  • 180

5 Answers5

0

Why you don't try with:

TextView text = new TextView();

String str = "some text";
SpannableStringBuilder sb = new SpannableStringBuilder(str);
            sb.setSpan(iconFont, 0, str.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            text.setTextSize(18);
            text.setText(sb);
Pablo Pantaleon
  • 1,506
  • 1
  • 19
  • 38
0

The problem is with settings the typeface. You are setting the typeface when eventType is not null but you are resetting it when eventType is null. Once the cell has the typeface set, it might get reused for another time when the eventType is null. In that case the holder.icon will still have the old typeface instead of the default typeface.

if (eventType != null) 
{
    holder.icon.setText(obj.getIconCode());
    holder.icon.setTypeface(iconFont);
    holder.name.setText(obj.getName());
} else {
    holder.icon.setTypeface(defaultFont);
    ...
}
Aswin Rajendiran
  • 3,399
  • 1
  • 21
  • 18
  • but if I fall in the else span then edit text don't get any text in it, so no typface needs to be set. – vlio20 Jan 29 '14 at 18:46
0

You're trying to set an image as text, so it will show up as just that if I'm not mistaken. Have a look at this answer, it'll help you out. How to use icons and symbols from "Font Awesome" on Native Android Application

Community
  • 1
  • 1
Aashir
  • 2,621
  • 4
  • 21
  • 37
  • I think you wrong, I am using it in other parts of my app. I will add screenshot in my question. – vlio20 Jan 29 '14 at 18:48
0

Are you sure that the name is correct?

When you do:

iconFont = Typeface.createFromAsset(activity.getAssets(), "icons.ttf" );

Is "icons" the correct name for the typeface?

Aside from that, since the typeface is always the same you can declare it inside the holder.

holder = new ViewHolder();
holder.icon = (TextView) row.findViewById(R.id.layout_icon_EditText);
holder.name = (TextView) row.findViewById(R.id.layout_name_EditText);
holder.icon.setTypeface(iconFont);
row.setTag(holder);
nunofmendes
  • 3,731
  • 2
  • 32
  • 42
0

What solved it fore me was to make it like this:

holder.icon.setText(Html.fromHtml(eventType.getIconCode()));
vlio20
  • 8,955
  • 18
  • 95
  • 180