Custom font TextView not being applied when setting text from java android

I created a Custom TextView to take advantage of Font Awesome support and it works great when you add text (unicode 

) to the xml layout. But if I try 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 what you are doing wrong.

+3


source to share


2 answers


After battles for more than 3 hours. I found the answer here. This is a unicode issue.

Changing the adapter setText()

to use Html.fromHtml("&#xf179;").toString()

fixed the problem

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

      



More details here

Hope you guys find this helpful.

+6


source


Could not find your code error.

This is the custom textView I am using. Create a fonts folder in your assets folder



CustomText.java

import android.content.Context;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.TextView;

public class CustomText extends TextView {

public CustomText(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    init(attrs);
}

public CustomText(Context context, AttributeSet attrs) {
    super(context, attrs);
    init(attrs);

}

public CustomText(Context context) {
    super(context);
    init(null);
}

private void init(AttributeSet attrs) {
    if (attrs!=null) {

        {
             Typeface myTypeface = Typeface.createFromAsset(getContext().getAssets(),"fonts/custom_font.ttf");
             setTypeface(myTypeface);
         }

    }
}

} 

      

0


source







All Articles