Search code examples
javaandroidtextviewandroid-adapterfont-awesome-4

Custom TextView font not applied on setting text from java android


I have created a Custom TextView to use Font Awesome support, its working fine when you add the text (unicode ) in layout xml. But if am trying to set the text from my Adapter dynamically using view.setText(), its not applying the font.

FontView class

public class FontView extends TextView {
    private static final String TAG = FontView.class.getSimpleName();
    //Cache the font load status to improve performance
    private static Typeface font;

    public FontView(Context context) {
        super(context);
        setFont(context);
    }

    public FontView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setFont(context);
    }

    public FontView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setFont(context);
    }

    private void setFont(Context context) {
        // prevent exception in Android Studio / ADT interface builder
        if (this.isInEditMode()) {
            return;
        }

        //Check for font is already loaded
        if(font == null) {
            try {
                font = Typeface.createFromAsset(context.getAssets(), "fontawesome-webfont.ttf");
                Log.d(TAG, "Font awesome loaded");
            } catch (RuntimeException e) {
                Log.e(TAG, "Font awesome not loaded");
            }
        }

        //Finally set the font
        setTypeface(font);
    }
}

XML for Layout

 <com.domain.app.FontView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:textSize="60sp"
    android:textAlignment="center"
    android:text="&#xf179;"
    android:gravity="center"
    android:id="@+id/iconView"
    android:background="@drawable/oval"
    android:padding="10dp"
    android:layout_margin="10dp" />

My Adapter

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;
    IconListHolder viewHolder;

    if( convertView == null ) {
        LayoutInflater layoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = layoutInflater.inflate(R.layout.icon, null);
        viewHolder = new IconListHolder(v);
        v.setTag(viewHolder);
    } else {
        viewHolder = (IconListHolder) v.getTag();
    }

    //Set the text and Icon
    viewHolder.textViewIcon.setText(pages.get(position).getIcon());

    viewHolder.textViewName.setText(pages.get(position).getTitle());

    return v;
}

private class IconListHolder {
    public FontView textViewIcon;
    public TextView textViewName;

    public IconListHolder(View base) {
        textViewIcon = (FontView) base.findViewById(R.id.iconView);
        textViewName = (TextView) base.findViewById(R.id.iconTextView);
    }
}

Please help whats am doing wrong.


Solution

  • After struggling more than 3 hrs. I have found the answer here. It was unicode issue.

    Changing Adapter setText() to use Html.fromHtml("&#xf179;").toString() fixed the issue

    viewHolder.textViewIcon
       .setText(Html.fromHtml(pages.get(position).getIcon()).toString());
    

    Read more here

    I hope you guys find it useful.