Skip to content Skip to sidebar Skip to footer

How To Use Icon From Font File As A Drawable In Android

I have a font file whose icons I am using in layout files via a Custom TextView. I created a Custom Class : class CustomFontTextView extends TextView say, icon is there in Font

Solution 1:

You can create your own drawable class for doing this, something like this

/** Embed an icon into a Drawable that can be used as TextView icons, or ActionBar icons.
 *
 * new IconDrawable(context, IconValue.icon_star)
 *           .colorRes(R.color.white)
 *           .actionBarSize();
 * 
 * If you don't set the size of the drawable, it will use the size
 * that is given to him. Note that in an ActionBar, if you don't
 * set the size explicitly it uses 0, so please use actionBarSize().
 */publicclassFontIconDrawableextendsDrawable {

publicstatic int ANDROID_ACTIONBAR_ICON_SIZE_DP = 24;

private final Context context;

private final String icon;

privateTextPaint paint;

private int size = -1;

private int alpha = 255;

/**
 * Create an IconDrawable.
 *
 * @param context Your activity or application context.
 * @param icon    The icon you want this drawable to display.
 */publicFontIconDrawable(Context context, String icon, Typeface typeface) {
    this.context = context;
    this.icon = icon;
    paint = newTextPaint();
    paint.setTypeface(typeface);
    paint.setStyle(Paint.Style.STROKE);
    paint.setTextAlign(Paint.Align.CENTER);
    paint.setUnderlineText(false);
    paint.setColor(Color.WHITE);
    paint.setAntiAlias(true);
}

/**
 * Set the size of this icon to the standard Android ActionBar.
 *
 * @return The current IconDrawable for chaining.
 */publicFontIconDrawableactionBarSize() {
    returnsizeDp(ANDROID_ACTIONBAR_ICON_SIZE_DP);
}

/**
 * Set the size of the drawable.
 *
 * @param dimenRes The dimension resource.
 * @return The current IconDrawable for chaining.
 */publicFontIconDrawablesizeRes(int dimenRes) {
    returnsizePx(context.getResources().getDimensionPixelSize(dimenRes));
}

/**
 * Set the size of the drawable.
 *
 * @param size The size in density-independent pixels (dp).
 * @return The current IconDrawable for chaining.
 */publicFontIconDrawablesizeDp(int size) {
    returnsizePx(dpToPx(context.getResources(), size));
}

/**
 * Dp to px.
 *
 * @param res the res
 * @param dp  the dp
 * @return the int
 */publicstatic int dpToPx(Resources res, int dp) {
    return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,
            res.getDisplayMetrics());
}

/**
 * Set the size of the drawable.
 *
 * @param size The size in pixels (px).
 * @return The current IconDrawable for chaining.
 */publicFontIconDrawablesizePx(int size) {
    this.size = size;
    setBounds(0, 0, size, size);
    invalidateSelf();
    returnthis;
}

/**
 * Set the color of the drawable.
 *
 * @param color The color, usually from android.graphics.Color or 0xFF012345.
 * @return The current IconDrawable for chaining.
 */publicFontIconDrawablecolor(int color) {
    paint.setColor(color);
    invalidateSelf();
    returnthis;
}

/**
 * Set the color of the drawable.
 *
 * @param colorRes The color resource, from your R file.
 * @return The current IconDrawable for chaining.
 */publicFontIconDrawablecolorRes(int colorRes) {
    paint.setColor(context.getResources().getColor(colorRes));
    invalidateSelf();
    returnthis;
}

/**
 * Set the alpha of this drawable.
 *
 * @param alpha The alpha, between 0 (transparent) and 255 (opaque).
 * @return The current IconDrawable for chaining.
 */publicFontIconDrawablealpha(int alpha) {
    setAlpha(alpha);
    invalidateSelf();
    returnthis;
}

@Overridepublic int getIntrinsicHeight() {
    return size;
}

@Overridepublic int getIntrinsicWidth() {
    return size;
}

@Overridepublicvoiddraw(Canvas canvas) {
    paint.setTextSize(getBounds().height());
    Rect textBounds = newRect();
    String textValue = icon;
    paint.getTextBounds(textValue, 0, 1, textBounds);
    float textBottom = (getBounds().height() - textBounds.height()) / 2f + textBounds.height() - textBounds.bottom;
    canvas.drawText(textValue, getBounds().width() / 2f, textBottom, paint);
}

@OverridepublicbooleanisStateful() {
    returntrue;
}

@OverridepublicbooleansetState(int[] stateSet) {
    int oldValue = paint.getAlpha();
    int newValue = isEnabled(stateSet) ? alpha : alpha / 2;
    paint.setAlpha(newValue);
    return oldValue != newValue;
}

/**
 * Checks if is enabled.
 *
 * @param stateSet the state set
 * @return true, if is enabled
 */publicstaticbooleanisEnabled(int[] stateSet) {
    for (int state : stateSet)
        if (state == android.R.attr.state_enabled)
            returntrue;
    returnfalse;
}

@OverridepublicvoidsetAlpha(int alpha) {
    this.alpha = alpha;
    paint.setAlpha(alpha);
}

@OverridepublicvoidsetColorFilter(ColorFilter cf) {
    paint.setColorFilter(cf);
}

@OverridepublicvoidclearColorFilter() {
    paint.setColorFilter(null);
}

@Overridepublic int getOpacity() {
    returnPixelFormat.OPAQUE;
}

/**
 * Sets paint style.
 *
 * @param style to be applied
 */publicvoidsetStyle(Paint.Style style) {
    paint.setStyle(style);
}
}

Solution 2:

I think in your case its not possible to change Home up button icon via xml...you are using an iconFont, so your icon is actually a text and to display those icons you need your CustomTextView or any View, which extends the TextView. android:homeAsUpIndicator requires drawable. Possible solution could be: to have this special icon in drawable folder.

Post a Comment for "How To Use Icon From Font File As A Drawable In Android"