The following code shows how to extend TextView with custom font.
main.xml for layout
<?xml version="1.0" encoding="utf-8"?> <!--/*from w w w .j a v a 2 s. c o m*/ Copyright (c) 2012 Manning See the file license.txt for copying permission. --> <merge xmlns:android="http://schemas.android.com/apk/res/android" > <com.manning.androidhacks.hack011.view.LedTextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="88:88:88" android:textColor="#3300ff00" android:textSize="80sp" /> <com.manning.androidhacks.hack011.view.LedTextView android:id="@+id/main_clock_time" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:shadowColor="#00ff00" android:shadowDx="0" android:shadowDy="0" android:shadowRadius="10" android:textColor="#00ff00" android:textSize="80sp" /> </merge>
Java code
/******************************************************************************* * Copyright (c) 2012 Manning//from w w w.j a va 2 s. c o m * See the file license.txt for copying permission. ******************************************************************************/ package com.manning.androidhacks.hack011; import java.util.Date; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.widget.TextView; public class MainActivity extends Activity { private static final String DATE_FORMAT = "%02d:%02d:%02d"; private static final int REFRESH_DELAY = 500; private final Handler mHandler = new Handler(); private final Runnable mTimeRefresher = new Runnable() { @Override public void run() { final Date d = new Date(); mTextView.setText(String.format(DATE_FORMAT, d.getHours(), d.getMinutes(), d.getSeconds())); mHandler.postDelayed(this, REFRESH_DELAY); } }; private TextView mTextView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mTextView = (TextView) findViewById(R.id.main_clock_time); } @Override protected void onResume() { super.onResume(); mHandler.post(mTimeRefresher); } @Override protected void onStop() { super.onStop(); mHandler.removeCallbacks(mTimeRefresher); } }
Custom TextView
/******************************************************************************* * Copyright (c) 2012 Manning// ww w . j a v a 2s. c om * See the file license.txt for copying permission. ******************************************************************************/ package com.manning.androidhacks.hack011.view; import java.io.File; import android.content.Context; import android.content.res.AssetManager; import android.graphics.Typeface; import android.util.AttributeSet; import android.widget.TextView; public class LedTextView extends TextView { private static final String FONTS_FOLDER = "fonts"; private static final String FONT_DIGITAL_7 = FONTS_FOLDER + File.separator + "digital-7.ttf"; public LedTextView(Context context) { super(context); init(context); } public LedTextView(Context context, AttributeSet attrs) { super(context, attrs); init(context); } public LedTextView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(context); } private void init(Context context) { AssetManager assets = context.getAssets(); final Typeface font = Typeface.createFromAsset(assets, FONT_DIGITAL_7); setTypeface(font); } }