Java – Custom TextView fonts are not available for setting text from java android

Custom TextView fonts are not available for setting text from java android… here is a solution to the problem.

Custom TextView fonts are not available for setting text from java android

I created a custom TextView to use Font Awesome support and it works fine when you add text (unicode ) in layout xml. However, if I try to dynamically set text from my adapter using view.setText(), it doesn’t apply the font.

The font View 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 me where I am doing wrong.

Solution

After more than 3 hours of struggle. I found the answer here. This is a Unicode issue.

Changing adapter setText() to use Html.fromHtml("").toString() fixed the issue

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

Read more here

I hope you find it useful.

Related Problems and Solutions