Change the background and text colors of the action bar programmatically using AppCompat

I am trying to change the background color and text color of my action bar programmatically using AppCompat. This is the code that I used before using the Holo theme, but it seems that I cannot use the same thing for AppCompat. Does 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

+4
source share
1 answer

getActionBar()use insteadgetSupportActionBar()

EDIT:

You get errors because your import is wrong. Please use the same as below. This works great.

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 a Google bug, it must have a different name. SupportActionBar will be great.

If you cannot fix the import, you can explicitly specify which one

android.support.v7.app.ActionBar actionBar = getSupportActionBar();
+12

Source: https://habr.com/ru/post/1584082/


All Articles