Question

I am trying to change the font of the Application label (I am using ActionBarSherlock as a library project in my application) and getting errors.

Following is code snippet where I am trying to set an external font, stored in assets, to the app label after setContentView().

mAppName = (TextView) findViewById(R.id.abs__action_bar_title);
Typeface face = Typeface.createFromAsset(getAssets(), "fonts/handsean.ttf");
mAppName.setTypeface(face);

Following is the error from the logcat. The error seems to be showing up on the line mAppName.setTypeface(face).

E/AndroidRuntime(18762): FATAL EXCEPTION: main
E/AndroidRuntime(18762): java.lang.RuntimeException: Unable to start activity  ComponentInfo{com.cheats/com.cheats.LandingPage}: java.lang.NullPointerException
E/AndroidRuntime(18762):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
E/AndroidRuntime(18762):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
E/AndroidRuntime(18762):    at android.app.ActivityThread.access$600(ActivityThread.java:141)
E/AndroidRuntime(18762):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
E/AndroidRuntime(18762):    at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(18762):    at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime(18762):    at android.app.ActivityThread.main(ActivityThread.java:5039)
E/AndroidRuntime(18762):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(18762):    at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime(18762):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
E/AndroidRuntime(18762):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
E/AndroidRuntime(18762):    at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(18762): Caused by: java.lang.NullPointerException
E/AndroidRuntime(18762):    at com.cheats.LandingPage.onCreate(LandingPage.java:89)
E/AndroidRuntime(18762):    at android.app.Activity.performCreate(Activity.java:5104)
E/AndroidRuntime(18762):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
E/AndroidRuntime(18762):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)

I found the following declaration of the text view for the application label in abs__action_bar_title_item.xml :

<TextView android:id="@+id/abs__action_bar_title"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:singleLine="true"
              android:ellipsize="end" />

Is this the right way to do it OR am I missing something ? Can some one advice ?

Thanks for your time.

Was it helpful?

Solution

After a lot of trials I was able to get the reference of the textView in the below manner and now the font has changed :

int titleId = Resources.getSystem().getIdentifier("action_bar_title", "id", "android");
if(titleId == 0)
    titleId = com.actionbarsherlock.R.id.abs__action_bar_title;

mAppName = (TextView) findViewById(titleId);
Typeface face = Typeface.createFromAsset(getAssets(), "fonts/handsean.ttf");
mAppName.setTypeface(face);

OTHER TIPS

In Xamarin.Android (MonoDroid - C#):

If you have problems getting null on the TextView it could be because the Android version, above code worked for me with Android 4.2 but didn't with 3.1.

This worked for me:

TextView title = FindViewById<TextView>(Resources.GetIdentifier("action_bar_title", "id", "android"));
if (title == null) title = FindViewById<TextView>(Resource.Id.abs__action_bar_title);

if( title != null)
{
   Android.Graphics.Typeface face = Android.Graphics.Typeface.CreateFromAsset(Assets, "fonts/font.otf");
   title.SetTypeface(face, Android.Graphics.TypefaceStyle.Bold);
}

The solution provided by Abhishek Sabbarwal works fine (< 3.x and >= 4.x) but, in a case, Honeycomb, it fails to find the Textview, as charlesbock pointed it out.

After debugging, it seems that this line gives a positive titleId that yet, does not match a TextView.

int titleId = Resources.getSystem().getIdentifier("action_bar_title", "id", "android");

However, using this line, the TextView is successfully found

titleId = R.id.abs__action_bar_title;

Therefore, here is my code that works for any version

int titleId = Resources.getSystem().getIdentifier("action_bar_title", "id", "android");
if(titleId == 0 || (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB && Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH))
{
    titleId = R.id.abs__action_bar_title;
}

final TextView appName = (TextView) findViewById(titleId);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top