Skip to content Skip to sidebar Skip to footer

Setting Up The Style Or Typeface Of A Custom View Which Contains Text, Like Default Textview?

I`m implementing a custom view which contains text, and the text will be drawn in onDraw. My problem is, I wish my text will looks like those in the TextView objects, but I`m not q

Solution 1:

publicclassCustomTextextendsTextView { 
 publicCustomText(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    // TODO Auto-generated constructor stubTypefacetypeface= Typeface.createFromAsset(context.getAssets(),
    "fonts/your_font.ttf");
    setTypeface(typeface);

}

publicCustomText(Context context, AttributeSet attrs) {
    super(context, attrs);
    // TODO Auto-generated constructor stubTypefacetypeface= Typeface.createFromAsset(context.getAssets(),
    "fonts/fonts/your_font.ttf");
    setTypeface(typeface);

}

publicCustomText(Context context) {
    super(context);
    // TODO Auto-generated constructor stubTypefacetypeface= Typeface.createFromAsset(context.getAssets(),
"fonts/fonts/your_font.ttf");
setTypeface(typeface);
    }

}

Now use the CustomText in your class/xml.

Hope this will help you.

Solution 2:

You can create a custom view by extending TextView as a parent view. by doing this you will get all the default features that textView has and plus you can add other custom features as per your requirements.

Post a Comment for "Setting Up The Style Or Typeface Of A Custom View Which Contains Text, Like Default Textview?"