How to set Toolbar background color in an activity to a color inside colors.xml file?

Use this code

getSupportActionBar().setBackground(new ColorDrawable(getResources().getColor(R.color.white)));

Try creating new layout resource toolbar.xml:

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

<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/MAIN_A" />

And then include it in your activity layout like this:

    <include
    android:id="@+id/toolbar"
    layout="@layout/toolbar" />

Then you need to set this toolbar in your activity onCreate method:

    toolbar = (Toolbar) findViewById(R.id.toolbar);
    if (toolbar != null) {
        // set toolbar object as actionbar
        setSupportActionBar(toolbar);
    }

After that you can access your new action bar from getSupportActionBar() method. Tell me if it helps :)


First, ActionBar is depricated, use Toolbar (android.widget.Toolbar) instead. If this is not possible, try the support of ActionBar as follows :

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

actionBar.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.MAIN_A)));

For the toolbar, it's :

toolbar.setBackgroundResource(R.color.MAIN_A)