Skip to content Skip to sidebar Skip to footer

Custom Font For Clock Textview

I am new in world of android developement and I want to make a clock such that each digit of time has it's own typeface. Hour digits has it's own typeface and minutes digit has its

Solution 1:

Let's say your font name is DemoFont . Make a Class which extends TextView. And initialize the font for the DemoFont .

Next place the .ttf file of that font in the assets folder.

publicclassDemoFontextendsTextView {


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

    publicDemoFont(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    publicDemoFont(Context context) {
        super(context);
        init();
    }

    privatevoidinit() {
        Typefacetf= Typeface.createFromAsset(getContext().getAssets(),
                "demofont.ttf");
        setTypeface(tf);
    }

}

Now, In your layout file you can use this like this way.

<YOUR_PACKAGE_NAME.DemoFontandroid:layout_width="match_parent"android:layout_height="wrap_content" />

Solution 2:

First copy the fonts to the assets folder in your project.

For Hour Textview

publicclassHourTextViewextendsTextView {

publicHourTextView(Context context) {
    super(context);
    init(null);
}

publicHourTextView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    super(context, attrs, defStyleAttr, defStyleRes);
    init(attrs);
}

publicHourTextView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init(attrs);
}

publicHourTextView(Context context, AttributeSet attrs) {
    super(context, attrs);
    init(attrs);
}

// Initializes any UI properties of the text view.privatevoidinit(AttributeSet attrs) {
    TypefacemyTypeface= Typeface.createFromAsset(getContext().getAssets(), "Hour-font-file.otf");
    setTypeface(myTypeface);
}

}

For Minute Textview

publicclassMinuteTextViewextendsTextView {

publicMinuteTextView(Context context) {
    super(context);
    init(null);
}

publicMinuteTextView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    super(context, attrs, defStyleAttr, defStyleRes);
    init(attrs);
}

publicMinuteTextView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init(attrs);
}

publicMinuteTextView(Context context, AttributeSet attrs) {
    super(context, attrs);
    init(attrs);
}

// Initializes any UI properties of the text view.privatevoidinit(AttributeSet attrs) {
    TypefacemyTypeface= Typeface.createFromAsset(getContext().getAssets(), "Minute-font-file.otf");
    setTypeface(myTypeface);
}

}

For Seconds Textview

publicclassSecondTextViewextendsTextView {

publicSecondTextView(Context context) {
    super(context);
    init(null);
}

publicSecondTextView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    super(context, attrs, defStyleAttr, defStyleRes);
    init(attrs);
}

publicSecondTextView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init(attrs);
}

publicSecondTextView(Context context, AttributeSet attrs) {
    super(context, attrs);
    init(attrs);
}

// Initializes any UI properties of the text view.privatevoidinit(AttributeSet attrs) {
    TypefacemyTypeface= Typeface.createFromAsset(getContext().getAssets(), "Second-font-file.otf");
    setTypeface(myTypeface);
}

}

and in xml file do this,

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="horizontal"android:layout_width="match_parent"android:layout_height="match_parent"android:gravity="center">

<com.yourpackage.HourTextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="10"
    android:id="@+id/hourText" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text=" : " />

<com.yourpackage.MinuteTextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="45 "
    android:id="@+id/minuteText" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text=" : " />

<com.yourpackage.SecondTextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="28"
    android:id="@+id/secondsText" />

</LinearLayout>

Solution 3:

publicclassMainActivityextendsAppCompatActivity {

publicTextView textView;
int countInt;
private int mInterval = 1000; // 1 second by default, can be changed laterprivateHandler mHandler;

@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    textView=(TextView)findViewById(R.id.textView);

    mHandler = newHandler();
    startRepeatingTask();
}

Runnable mStatusChecker = newRunnable() {
    @Overridepublicvoidrun() {
        try {
            countInt=countInt+1;
            textView.setText(String.valueOf(countInt));
        } finally {
            mHandler.postDelayed(mStatusChecker, mInterval);
        }
    }
};

voidstartRepeatingTask() {
    mStatusChecker.run();
}

voidstopRepeatingTask() {
    mHandler.removeCallbacks(mStatusChecker);
}
}

Post a Comment for "Custom Font For Clock Textview"