MacaronLover MacaronLover - 1 year ago 85
Android Question

Change Action bar background and text colours programmatically using AppCompat

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

Answer Source

instead of getActionBar() use getSupportActionBar()


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

import android.os.Bundle;
import android.text.Html;

public class TestActivity extends ActionBarActivity {

    protected void onCreate(Bundle 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 actionBar = getSupportActionBar();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download