10

I have a color in my colors.xml file which I need to use for toolbar color

<resources>
    <color name="MAIN_A">#f16264</color>
</resources>

Now I need to use MAIN_A as a color for toolbar.

flo
  • 9,713
  • 6
  • 25
  • 41

9 Answers9

20

Use this code

getSupportActionBar().setBackground(new ColorDrawable(getResources().getColor(R.color.white)));
Sreekant Shenoy
  • 1,420
  • 14
  • 23
abozaid
  • 967
  • 8
  • 11
3

Here is code for Kotlin

supportActionBar!!.setBackgroundDrawable(ColorDrawable(resources.getColor(R.color.colorPrimary)))
Shahen Kosyan
  • 124
  • 1
  • 5
2

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 :)

2

Try it like that:

ActionBar actionBar = getActionBar();
actionBar.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.MAIN_A)));
SuperFrog
  • 7,631
  • 9
  • 51
  • 81
2

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)
Hichem Acher
  • 433
  • 2
  • 16
1

In case you have a custom toolbar considering the API 23+.

1 - Toolbar mToolbar = (Toolbar)findViewById(R.id.yourtoolbarId);

2 - mToolbar.setBackgroundColor(Color.parseColor("#004D40"));

Niamatullah Bakhshi
  • 1,445
  • 16
  • 27
1

Toolbar background, text, arrow and three-dots popup menu color.

1) Background:

toolbar.setBackgroundColor(ContextCompat.getColor(this, R.color.toolbar_color));

or (requires API 16):

toolbar.setBackground(new ColorDrawable(ContextCompat.getColor(this, R.color.toolbar_color)));

2) Title:

toolbar.setTitleTextColor(ContextCompat.getColor(this, R.color.gray);

3) Arrow:

toolbar.getNavigationIcon().setColorFilter(ContextCompat.getColor(this, R.color.gray), PorterDuff.Mode.SRC_ATOP);

4) Popup menu three-dots icon (right icon):

toolbar.getOverflowIcon().setColorFilter(ContextCompat.getColor(this, R.color.gray, PorterDuff.Mode.SRC_ATOP);

See https://stackoverflow.com/a/26837072/2914140 and https://stackoverflow.com/a/51908890/2914140.

All in one (in Kotlin):

toolbar.setBackgroundColor(ContextCompat.getColor(this, R.color.blue))
val toolbarTextColor = ContextCompat.getColor(this, R.color.gray)
toolbar.setTitleTextColor(toolbarTextColor)
toolbar.navigationIcon?.setColorFilter(toolbarTextColor, PorterDuff.Mode.SRC_ATOP)
toolbar.overflowIcon?.setColorFilter(toolbarTextColor, PorterDuff.Mode.SRC_ATOP)
CoolMind
  • 26,736
  • 15
  • 188
  • 224
0

By your question you have not used SupportActionBar so you can do like as below:

ActionBar actionBar = getActionBar();
actionBar.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.MAIN_A)));
actionBar.setDisplayShowTitleEnabled(false);  // required to force redraw, without, gray color
actionBar.setDisplayShowTitleEnabled(true);

Actual credit goes to this link of SO.

Community
  • 1
  • 1
Pankaj
  • 7,908
  • 6
  • 42
  • 65
0

This may not be the "best method" but i just tried this and it works fine, even if it should be considered a temporary measure until someone finds a better one, and it works PRE- API23... You can make a new xml layout for each separate use of the toolbar, for example toolbar1.xml, toolbar2.xml etc. Then in the activity code add this to onCreate:

for toolbar1:

getSupportActionBar().setBackgroundDrawable(new colorDrawable(getResources().getColor(R.color.colorAdmin))); 

LayoutInflater inflater_admin = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

@SuppressLint("InflateParams") View action_bar_view_admin = Objects.requireNonNull(inflater_admin).inflate(R.layout.chat_custom_bar_admin, null);

actionBar.setCustomView(action_bar_view_admin);

for toolbar2:

getSupportActionBar().setBackgroundDrawable(new colorDrawable(getResources().getColor(R.color.colorOwner))); 

LayoutInflater inflater_owner = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

@SuppressLint("InflateParams") View action_bar_view_owner = Objects.requireNonNull(inflater_owner).inflate(R.layout.chat_custom_bar_owner, null);

actionBar.setCustomView(action_bar_view_owner);

etc...

Hope this helps someone!

SimplyProgrammer
  • 1,799
  • 2
  • 17
  • 28