Question

I am currently in the process of overhauling the font of my application to use a custom font.

I have a class such as this:

public class TextViewRobotoRegular extends TextView {

    public TextViewRobotoRegular(Context context, AttributeSet attrs) {
        super(context, attrs);

        if (!isInEditMode()) {
            setTypeface(Typefaces.get(context, "Roboto-BlackItalic"));           
        }
    }
}

where Typefaces.get is a method which points to a cached typeface, and if not cached, creates a new instance.

For organization sake, I was wondering if I could make several TextView classes for each font within one large class, something like this

public class TextViews {

    public class RobotoRegular extends TextView {

        public TextViewRobotoRegular(Context context, AttributeSet attrs) {
            super(context, attrs);

            if (!isInEditMode()) {
                setTypeface(Typefaces.get(context, "Roboto-Thin"));           
            }
        }
    }

    public class RobotoThin extends TextView {

        public TextViewRobotoRegular(Context context, AttributeSet attrs) {
            super(context, attrs);

            if (!isInEditMode()) {
                setTypeface(Typefaces.get(context, "Roboto-Thin"));           
            }
        }
    }
}

Can I make these references in the xml layout files? something like:

<com.example.appname.fontPackageName.TextViews.RobotoRegular
    android:id="@+id/profileNameView"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />
Was it helpful?

Solution

Make classes RobotoRegular and RobotoThin public static.

And edit xml like this:

    <view class="com.example.appname.fontPackageName.TextViews$RobotoRegular"
    android:id="@+id/profileNameView"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top