4

I'm trying to change the background colour and text colour of my action bar programmatically using AppCompat. This is the code I used before when using the Holo theme but it seems that I can't use the same thing for AppCompat. Anyone know what I need to change?

ActionBar bar = getActionBar();
    bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#95CDBA")));
    getActionBar().setTitle(Html.fromHtml("<font color='#000099'>Hello World</font>"));

enter image description here

wbk727
  • 8,017
  • 12
  • 61
  • 125

2 Answers2

14

instead of getActionBar() use getSupportActionBar()

EDIT:

You are getting errors, because your imports are wrong. Please use the same as below. This works just fine.

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.text.Html;

public class TestActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActionBar actionBar = getSupportActionBar();
        actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#95CDBA")));
        actionBar.setTitle(Html.fromHtml("<font color='#000099'>Hello World</font>"));
    }
}

Yes, this is Google's mistake, it should have a different name. SupportActionBar would be great.

If you are unable to fix the imports, you can explicitly specify which one like this

android.support.v7.app.ActionBar actionBar = getSupportActionBar();
Bojan Kseneman
  • 15,488
  • 2
  • 54
  • 59
  • I get an error after doing that. Check screenshot above. – wbk727 Apr 22 '15 at 17:42
  • You are using the wrong imports. You have imported the holo ActionBar. Please see my edit. – Bojan Kseneman Apr 22 '15 at 17:50
  • Brilliant. How can I get the back arrow to a dark blue colour rather than white? Check my new screenshot above. – wbk727 Apr 22 '15 at 17:52
  • I have not done that yet, but this will help you http://stackoverflow.com/questions/26788464/how-to-change-color-of-the-back-arrow-in-the-new-material-theme – Bojan Kseneman Apr 22 '15 at 17:57
  • Html.fromHtml is deprecated in Android N so use HtmlCompat.fromHtml("$text", HtmlCompat.FROM_HTML_MODE_LEGACY) – Andrew Apr 15 '20 at 13:18
0

Old question, but I decided to add another way.

If you are using a custom ActionBar you can change the font, text color, text itself and background color by doing the following:

public class MainActivity extends AppCompatActivity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Toolbar toolbar = findViewById(R.id.mCustom_app_bar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setTitle("Custom Title");
        applyToolbarTitleFontTextBackgroundAndTextColor(this);

    }

    public static void applyToolbarTitleFontTextBackgroundAndTextColor(Activity context){
        Toolbar toolbar = context.findViewById(R.id.mCustom_app_bar);
        toolbar.setBackground(new ColorDrawable(Color.parseColor("#95CDBA")));
        for(int i = 0; i < toolbar.getChildCount(); i++){
            View view = toolbar.getChildAt(i);
            if(view instanceof TextView){
                TextView tv = (TextView) view;
                tv.setTextColor(Color.parseColor("#ffffff"));
                Typeface titleFont = Typeface.createFromAsset(context.getAssets(), "fonts/mCoolFont.otf");
                if(tv.getText().equals(toolbar.getTitle())){
                    tv.setTypeface(titleFont);
                    break;
                }
            }
        }
    }

}

By doing it this way you have full control of the look of your ActionBar.

Hope it helps someone.

HB.
  • 4,116
  • 4
  • 29
  • 53