1

In order to change the font which is inside my actionbar, I implemented a custom TypefaceSpan class and used a SpannableString in my onCreate.

This has what I did: How to Set a Custom Font in the ActionBar Title?

In the comments user, artwork ,said,

In the moment of starting the app, the default title style is visible and about 1 sec later the custom style appears.

Then the guy who answered the question said

"that's because your activity's theme is being applied to the DecorView before your code has spun up. You can work around this by hiding the action bar in your theme, then show it as your activity is being created."

I am not sure how to implement the following. Nor am I sure that is able to be done in my customized action bar.

And this is how I styled my actionbar:

<?xml version="1.0" encoding="utf-8"?>



<resources>

    <style name="Theme.Customaction" parent="@style/android:Theme.Holo">
        <item name="actionBarItemBackground">@drawable/selectable_background_customaction</item>
        <item name="popupMenuStyle">@style/PopupMenu.Customaction</item>
        <item name="dropDownListViewStyle">@style/DropDownListView.Customaction</item>
        <item name="actionBarTabStyle">@style/ActionBarTabStyle.Customaction</item>
        <item name="actionDropDownStyle">@style/DropDownNav.Customaction</item>
        <item name="actionBarStyle">@style/ActionBar.Transparent.Customaction</item>
        <item name="actionModeBackground">@drawable/cab_background_top_customaction</item>
        <item name="actionModeSplitBackground">@drawable/cab_background_bottom_customaction</item>
        <item name="actionModeCloseButtonStyle">@style/ActionButton.CloseMode.Customaction</item>


    </style>

    <style name="ActionBar.Solid.Customaction" parent="@style/Widget.AppCompat.ActionBar.Solid">
        <item name="background">@drawable/ab_solid_customaction</item>
        <item name="backgroundStacked">@drawable/ab_stacked_solid_customaction</item>
        <item name="backgroundSplit">@drawable/ab_bottom_solid_customaction</item>
        <item name="progressBarStyle">@style/ProgressBar.Customaction</item>
    </style>

    <style name="ActionBar.Transparent.Customaction" parent="@style/Widget.AppCompat.ActionBar">
        <item name="background">@drawable/ab_transparent_customaction</item>
        <item name="progressBarStyle">@style/ProgressBar.Customaction</item>
    </style>

    <style name="PopupMenu.Customaction" parent="@style/Widget.AppCompat.PopupMenu">
        <item name="android:popupBackground">@drawable/menu_dropdown_panel_customaction</item>
    </style>

    <style name="DropDownListView.Customaction" parent="@style/Widget.AppCompat.ListView.DropDown">
        <item name="android:listSelector">@drawable/selectable_background_customaction</item>
    </style>

    <style name="ActionBarTabStyle.Customaction" parent="@style/Widget.AppCompat.ActionBar.TabView">
        <item name="android:background">@drawable/tab_indicator_ab_customaction</item>
    </style>

    <style name="DropDownNav.Customaction" parent="@style/Widget.AppCompat.Spinner.DropDown.ActionBar">
        <item name="android:background">@drawable/spinner_background_ab_customaction</item>
        <item name="android:popupBackground">@drawable/menu_dropdown_panel_customaction</item>
        <item name="android:dropDownSelector">@drawable/selectable_background_customaction</item>
    </style>

    <style name="ProgressBar.Customaction" parent="@style/Widget.AppCompat.ProgressBar.Horizontal">
        <item name="android:progressDrawable">@drawable/progress_horizontal_customaction</item>
    </style>

    <style name="ActionButton.CloseMode.Customaction" parent="@style/Widget.AppCompat.ActionButton.CloseMode">
        <item name="android:background">@drawable/btn_cab_done_customaction</item>
    </style>

    <!-- this style is only referenced in a Light.DarkActionBar based theme -->
    <style name="Theme.Customaction.Widget" parent="@style/Theme.AppCompat">
        <item name="popupMenuStyle">@style/PopupMenu.Customaction</item>
        <item name="dropDownListViewStyle">@style/DropDownListView.Customaction</item>
    </style>

</resources>

This is part of what I have in my MainActivity:

  @Override
    protected void onCreate(Bundle savedInstanceState) {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        super.onCreate(savedInstanceState);


        //Make sure you find out why it appears after a whole 1 second after the app appears
        SpannableString s = new SpannableString("GetDisciplined");
        s.setSpan(new TypefaceSpan(this, "roboto-lightitalic.ttf"), 0, s.length(),
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

// Update the action bar title with the TypefaceSpan instance
        ActionBar actionBar = getActionBar();
        actionBar.setTitle(s);
        setContentView(R.layout.merge);

I am just trying to solve the problem of the Custom ActionBar Font showing up after one second by making it show up immediately, but I am unsure to achieve this.

Community
  • 1
  • 1
Rohit Tigga
  • 2,373
  • 9
  • 43
  • 81

1 Answers1

0

One way you could do it (not a very nice way) is to use a custom layout for your titlebar where you use a custom textview that displays the title.

public class CustomTextView extends TextView {
private static Typeface customFont = null;

public CustomTextView(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
}

public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    if (isInEditMode()) {
        return;
    }

    if (customFont == null) {
        customFont = Typeface.createFromAsset(context.getApplicationContext().getAssets(),
                "fonts/custom_font.ttf");
    }
    setTypeface(customFont);
}

}

In your activity:

 requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
 setContentView(R.layout.activities);
 getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title);

And the title in the layout uses that CustomTextView to hold the title.

Once again, pretty messy solution, but will do what you want it to.

s23
  • 43
  • 2