Reputation:
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
.
Upvotes: 10
Views: 28049
Reputation: 967
Use this code
getSupportActionBar().setBackground(new ColorDrawable(getResources().getColor(R.color.white)));
Upvotes: 20
Reputation: 1
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!
Upvotes: 0
Reputation: 124
Here is code for Kotlin
supportActionBar!!.setBackgroundDrawable(ColorDrawable(resources.getColor(R.color.colorPrimary)))
Upvotes: 3
Reputation: 28793
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)
Upvotes: 1
Reputation: 1539
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"));
Upvotes: 1
Reputation: 8058
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.
Upvotes: 0
Reputation: 433
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)
Upvotes: 2
Reputation: 7674
Try it like that:
ActionBar actionBar = getActionBar();
actionBar.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.MAIN_A)));
Upvotes: 2
Reputation: 95
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 :)
Upvotes: 2