0

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 }
Community
  • 1
  • 1
KFP
  • 699
  • 3
  • 12
  • 33
  • 1
    Check your logcat for a more detailed error. – Bryan Herbst Nov 20 '13 at 19:26
  • The error messages in the logcat is very long and I honestly can't make anything of it. I can post it, but like I said its long. – KFP Nov 20 '13 at 19:36
  • @KFP I did not give a downvote (and they should have given reason) but will try to answer your question. When posting a problem involving code it's best to try and reduce it down to an [SSCCE](http://sscce.org/) before coming here. The reasoning (as stated at the resource) is that you may find the solution while performing that task. You've given us a large amount of code, and without looking too deeply I can't tell if I can execute it right off the bat without any additions. You should also automatically include any errors that you are getting. These things improve the quality of your answer. – Brandon Buck Nov 20 '13 at 20:30
  • @KFP you need to get used to reading the stack trace from logcat. That's your single best tool for debugging your application. Try reading these two sources: http://stackoverflow.com/a/12688109/772122 http://developer.android.com/tools/debugging/debugging-log.html – twaddington Nov 20 '13 at 21:07
  • Okay, the relevant portion of the stack trace is this: `Caused by: java.lang.ClassNotFoundException: Didn't find class "com.your.package.CustomTextView"`. – twaddington Nov 20 '13 at 21:43

1 Answers1

0

You're trying to use a ".ttf" font file, but the code expects an ".otf" font. If you remove the ".otf" portion of the file path it will work correctly.

Simply change this:

String.format("fonts/%s.otf", typefaceName)

to this:

String.format("fonts/%s", typefaceName)

You really need to post your logcat output though as it should show the actual error message.

Edit: To elaborate, in the original code the application is looking for a font file called assets/fonts/MotorwerkOblique.ttf.otf which probably doesn't exist. It expects you to just pass in the file name, without the extension.

twaddington
  • 11,607
  • 5
  • 35
  • 46
  • I tried fonts/%s and fonts/%s.ttf and I still get the "..stopped working" message. If I remove the Spannable string piece the app works fine. the logcat file is too long to post here. I saved it to a .txt file, but I don't see a way to upload it. I really wish I get this to work. – KFP Nov 20 '13 at 21:29
  • @KFP you need to find the appropriate section of the logcat to post. It should be the last part at the bottom. – twaddington Nov 20 '13 at 21:34
  • @KFP if you need to just copy and paste the entire logcat from the .txt file into a gist at https://gist.github.com/. Then just update your original question with a link to the gist. – twaddington Nov 20 '13 at 21:38
  • Here is the link: anonymous / gist:7571618. Thanks, I really appreciate you looking at this. – KFP Nov 20 '13 at 21:45
  • @KFP looking at your gist I see this error `Caused by: java.lang.ClassNotFoundException: Didn't find class "com.your.package.CustomTextView"`. Do you have a file called `CustomTextView` or are you referencing that file from another file? https://gist.github.com/anonymous/7571618 – twaddington Nov 20 '13 at 21:50
  • 1
    The package name is the giveaway, even if he has that file I doubt he's got it in a package called "com.your.package" – Brandon Buck Nov 20 '13 at 21:51
  • I don't know what that class is. I started over and created a new project. Ran into issues with constructor undefined, etc. Started from scratch again and the app ran with 'My Title', but font didn't change. Finally on the 4th go it worked. So far with the android development environment I'm experiencing a lot of undependability. How this works in one completely new project, but not another is beyond my comprehension. I hate think how I go about changing the font size! – KFP Nov 21 '13 at 16:18