27 Kasım 2014 Perşembe

Custom TextView In Android With Styles

TextView is one of the most useful components in Android but at some point, it still cannot meet our requirements. For example, if you want to use a custom TypeFace (aka Font), you need to change it programmatically in the Activity like stated below:

Typeface typeFace = Typeface.createFromAsset(getAssets(), "Roboto-Regular.ttf");
TextView textView = (TextView) findViewById(R.id.myTextView);
textView.setTypeFace(typeFace);

Here, we've read our "Roboto-Regular.ttf" font file from "assets" folder and set it as the TypeFace of textView object. It is a good, simple, working solution but it is not feasible if you want to replace TypeFace for all of your TextViw components in the application. To set a custom font for all of your TextView components in your application, you can extend TextView class with your custom class. There are other methods like creating a custom component with its attributes using xml files but the solution below might be the best for you if you do not need too much feature but changing font family:

RobotoTextView.java
public class RobotoTextView extends TextView {
    public RobotoTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initialize();
    }
    public RobotoTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initialize();
    }
    public RobotoTextView(Context context) {
        super(context);
        initialize();
    }
    private void initialize() {
        if (!isInEditMode()) {
            Typeface currentTypeFace = getTypeface();
            int style = currentTypeFace.getStyle();
            Typeface type = setFontFamily(style);
            setTypeface(type);
        }
    }
    private Typeface setFontFamily(int style){
        Typeface type = null;
        if(style==Typeface.BOLD){
            type = Typeface.createFromAsset(getContext().getAssets(), "Roboto-Bold.ttf");
        }else if(style==Typeface.ITALIC){
            type = Typeface.createFromAsset(getContext().getAssets(), "Roboto-Italic.ttf");
        }else{
            type = Typeface.createFromAsset(getContext().getAssets(), "Roboto-Regular.ttf");
        }
        return type;
    }
    @Override
    public void setTypeface(Typeface tf, int style) {
        Typeface type = setFontFamily(style);
        super.setTypeface(type, style);
    }
}

MyView.xml
<com.test.RobotoTextView     android:id="@+id/myTextView"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="My Custom TypeFace"
     android:textStyle="bold"/>

Here, I extend TextView class with my own custom class and override the necessary constructors with custom ones. One problem I faced during these processes was I cannot make the text bold, italic etc. To overcome this issue, you must find bold and italic versions of your font and put them in your assets folder also. Then you need to check the style attribute in your methods and retrieve the related .ttf file considering the style attribute (bold or italic). By doing this, you can easily use "android:textStyle" attribute in your custom TextView component.

( android, custom textview, typeface, style, bold, italic, andrpid:textStyle )