I've tried the first two answers in the following post for changing the font of my title: How to Set a Custom Font in the ActionBar Title? and I get the message in my emulator:
Unfortunately, DC Parks has stopped. OK
Going with the second answer here is my MainActivity code:
import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableString;
import android.view.Menu;
import android.view.View;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SpannableString s = new SpannableString("My Title");
s.setSpan(new TypefaceSpan(this, "MotorwerkOblique.ttf"), 0, s.length(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
//Update the action bar title with the TypefaceSpan instance
ActionBar actionBar = getActionBar();
actionBar.setTitle(s);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void test(View view) {
//Intent represents the app's 'intent to do something'
Intent intent = new Intent(this, NewPage.class); //have to create the new_page activity
startActivity(intent);
}
public void textclick(View myview) {
Intent myintent = new Intent(this, NewPage.class);
startActivity(myintent);
}
}
TypefaceSpan.java code:
public class TypefaceSpan extends MetricAffectingSpan {
/** An <code>LruCache</code> for previously loaded typefaces. */
private static LruCache<String, Typeface> sTypefaceCache =
new LruCache<String, Typeface>(12);
private Typeface mTypeface;
/**
* Load the {@link Typeface} and apply to a {@link Spannable}.
*/
public TypefaceSpan(Context context, String typefaceName) {
mTypeface = sTypefaceCache.get(typefaceName);
if (mTypeface == null) {
mTypeface = Typeface.createFromAsset(context.getApplicationContext()
.getAssets(), String.format("fonts/%s.otf", typefaceName));
// Cache the loaded Typeface
sTypefaceCache.put(typefaceName, mTypeface);
}
}
@Override
public void updateMeasureState(TextPaint p) {
p.setTypeface(mTypeface);
// Note: This flag is required for proper typeface rendering
p.setFlags(p.getFlags() | Paint.SUBPIXEL_TEXT_FLAG);
}
@Override
public void updateDrawState(TextPaint tp) {
tp.setTypeface(mTypeface);
// Note: This flag is required for proper typeface rendering
tp.setFlags(tp.getFlags() | Paint.SUBPIXEL_TEXT_FLAG);
}
}
My font is located in assests/fonts/MotorwerkObliqhue.ttf
Any help would be greatly appreciated!
From Console:
[2013-11-20 14:54:58 - DCParks] Dx
trouble writing output: already prepared
[2013-11-20 14:55:00 - DCParks] ------------------------------
[2013-11-20 14:55:00 - DCParks] Android Launch!
[2013-11-20 14:55:00 - DCParks] adb is running normally.
[2013-11-20 14:55:00 - DCParks] Performing com.example.dcparks.MainActivity activity launch
[2013-11-20 14:55:00 - DCParks] Automatic Target Mode: using existing emulator 'emulator-5554' running compatible AVD 'Nexus4'
[2013-11-20 14:55:00 - DCParks] Uploading DCParks.apk onto device 'emulator-5554'
[2013-11-20 14:55:05 - DCParks] Installing DCParks.apk...
[2013-11-20 14:55:13 - DCParks] Success!
[2013-11-20 14:55:13 - DCParks] Starting activity com.example.dcparks.MainActivity on device emulator-5554
[2013-11-20 14:55:16 - DCParks] ActivityManager: Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.example.dcparks/.MainActivity }